lang: parser, funcs, core, iter: Rename xmap to map
The map function previously existed as "xmap" because this was a reserved word in the parser. This now adds a special case for this identifier name.
This commit is contained in:
@@ -29,8 +29,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
// MapFuncName is the name this function is registered as.
|
// MapFuncName is the name this function is registered as.
|
||||||
// XXX: rename to map once our parser sees a function name and not a type
|
MapFuncName = "map"
|
||||||
MapFuncName = "xmap"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ $fn = func($x) { # notable because concrete type is fn(t1) t2, where t1 != t2
|
|||||||
|
|
||||||
$in1 = ["a", "bb", "ccc", "dddd", "eeeee",]
|
$in1 = ["a", "bb", "ccc", "dddd", "eeeee",]
|
||||||
|
|
||||||
$out1 = iter.xmap($in1, $fn) # XXX: change to map
|
$out1 = iter.map($in1, $fn)
|
||||||
|
|
||||||
$t1 = template("out1: {{ . }}", $out1)
|
$t1 = template("out1: {{ . }}", $out1)
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
-- main.mcl --
|
-- main.mcl --
|
||||||
import "iter"
|
import "iter"
|
||||||
|
|
||||||
func iterxmap($a, $b) { # XXX: change to map
|
func itermap($a, $b) {
|
||||||
iter.xmap($a, $b) # XXX: change to map
|
iter.map($a, $b)
|
||||||
}
|
}
|
||||||
|
|
||||||
$fn = func($x) {
|
$fn = func($x) {
|
||||||
@@ -12,10 +12,10 @@ $fn = func($x) {
|
|||||||
$in1 = [5, 4, 3, 2, 1,]
|
$in1 = [5, 4, 3, 2, 1,]
|
||||||
$in2 = ["a", "b", "c", "d", "e",]
|
$in2 = ["a", "b", "c", "d", "e",]
|
||||||
|
|
||||||
$out1 = iter.xmap($in1, $fn) # XXX: change to map
|
$out1 = iter.map($in1, $fn)
|
||||||
$out2 = iter.xmap($in2, $fn) # XXX: change to map
|
$out2 = iter.map($in2, $fn)
|
||||||
$out3 = iterxmap($in1, $fn) # XXX: change to map
|
$out3 = itermap($in1, $fn)
|
||||||
$out4 = iterxmap($in2, $fn) # XXX: change to map
|
$out4 = itermap($in2, $fn)
|
||||||
|
|
||||||
$t1 = template("out1: {{ . }}", $out1)
|
$t1 = template("out1: {{ . }}", $out1)
|
||||||
$t2 = template("out2: {{ . }}", $out2)
|
$t2 = template("out2: {{ . }}", $out2)
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ $fn = func($x) { # ignore arg
|
|||||||
|
|
||||||
$in = [5, 4, 3, 2, 1,]
|
$in = [5, 4, 3, 2, 1,]
|
||||||
|
|
||||||
$out = iter.xmap($in, $fn) # XXX: change to map
|
$out = iter.map($in, $fn)
|
||||||
|
|
||||||
$t = template("out: {{ . }}", $out)
|
$t = template("out: {{ . }}", $out)
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ $fn = func($x) { # type changes from str to int
|
|||||||
|
|
||||||
$in = ["a", "bb", "ccc", "dddd", "eeeee",]
|
$in = ["a", "bb", "ccc", "dddd", "eeeee",]
|
||||||
|
|
||||||
$out = iter.xmap($in, $fn) # XXX: change to map
|
$out = iter.map($in, $fn)
|
||||||
|
|
||||||
$t = template("out: {{ . }}", $out)
|
$t = template("out: {{ . }}", $out)
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import "iter"
|
|||||||
$in = ["a", "bb", "ccc", "dddd", "eeeee",]
|
$in = ["a", "bb", "ccc", "dddd", "eeeee",]
|
||||||
|
|
||||||
# the inline lambda format is more readable with the func as the second arg
|
# the inline lambda format is more readable with the func as the second arg
|
||||||
$out = iter.xmap($in, func($x) {
|
$out = iter.map($in, func($x) {
|
||||||
len($x)
|
len($x)
|
||||||
|
|
||||||
}) # XXX: change to map
|
})
|
||||||
|
|
||||||
$t = template("out: {{ . }}", $out)
|
$t = template("out: {{ . }}", $out)
|
||||||
|
|
||||||
|
|||||||
@@ -2073,6 +2073,81 @@ func TestLexParse0(t *testing.T) {
|
|||||||
exp: exp,
|
exp: exp,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
fn := &ast.ExprFunc{
|
||||||
|
Args: []*interfaces.Arg{
|
||||||
|
{
|
||||||
|
Name: "x",
|
||||||
|
//Type: types.TypeInt,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
//Return: types.TypeInt,
|
||||||
|
Body: &ast.ExprCall{
|
||||||
|
Name: funcs.OperatorFuncName,
|
||||||
|
Args: []interfaces.Expr{
|
||||||
|
&ast.ExprStr{
|
||||||
|
V: "*",
|
||||||
|
},
|
||||||
|
&ast.ExprVar{
|
||||||
|
Name: "x",
|
||||||
|
},
|
||||||
|
&ast.ExprVar{
|
||||||
|
Name: "x",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
//if err := fn.SetType(types.NewType("func(x int) int")); err != nil {
|
||||||
|
// t.Fatal("could not build type")
|
||||||
|
//}
|
||||||
|
|
||||||
|
exp := &ast.StmtProg{
|
||||||
|
Body: []interfaces.Stmt{
|
||||||
|
&ast.StmtImport{
|
||||||
|
Name: "iter",
|
||||||
|
},
|
||||||
|
&ast.StmtBind{
|
||||||
|
Ident: "fn",
|
||||||
|
Value: fn,
|
||||||
|
},
|
||||||
|
&ast.StmtBind{
|
||||||
|
Ident: "out",
|
||||||
|
Value: &ast.ExprCall{
|
||||||
|
Name: "iter.map", // does this name lex/parse correctly?
|
||||||
|
Args: []interfaces.Expr{
|
||||||
|
&ast.ExprList{
|
||||||
|
Elements: []interfaces.Expr{
|
||||||
|
&ast.ExprInt{
|
||||||
|
V: 1,
|
||||||
|
},
|
||||||
|
&ast.ExprInt{
|
||||||
|
V: 2,
|
||||||
|
},
|
||||||
|
&ast.ExprInt{
|
||||||
|
V: 3,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&ast.ExprVar{
|
||||||
|
Name: "fn",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases = append(testCases, test{
|
||||||
|
name: "iter.map",
|
||||||
|
code: `
|
||||||
|
import "iter"
|
||||||
|
$fn = func($x) { $x * $x }
|
||||||
|
$out = iter.map([1,2,3,], $fn)
|
||||||
|
`,
|
||||||
|
fail: false,
|
||||||
|
exp: exp,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Logf("available tests:")
|
t.Logf("available tests:")
|
||||||
|
|||||||
@@ -499,6 +499,7 @@ struct_field:
|
|||||||
;
|
;
|
||||||
call:
|
call:
|
||||||
// fmt.printf(...)
|
// fmt.printf(...)
|
||||||
|
// iter.map(...)
|
||||||
dotted_identifier OPEN_PAREN call_args CLOSE_PAREN
|
dotted_identifier OPEN_PAREN call_args CLOSE_PAREN
|
||||||
{
|
{
|
||||||
posLast(yylex, yyDollar) // our pos
|
posLast(yylex, yyDollar) // our pos
|
||||||
@@ -1303,13 +1304,26 @@ type_func_arg:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
dotted_identifier:
|
undotted_identifier:
|
||||||
IDENTIFIER
|
IDENTIFIER
|
||||||
{
|
{
|
||||||
posLast(yylex, yyDollar) // our pos
|
posLast(yylex, yyDollar) // our pos
|
||||||
$$.str = $1.str
|
$$.str = $1.str
|
||||||
}
|
}
|
||||||
| dotted_identifier DOT IDENTIFIER
|
// a function could be named map()!
|
||||||
|
| MAP_IDENTIFIER
|
||||||
|
{
|
||||||
|
posLast(yylex, yyDollar) // our pos
|
||||||
|
$$.str = $1.str
|
||||||
|
}
|
||||||
|
;
|
||||||
|
dotted_identifier:
|
||||||
|
undotted_identifier
|
||||||
|
{
|
||||||
|
posLast(yylex, yyDollar) // our pos
|
||||||
|
$$.str = $1.str
|
||||||
|
}
|
||||||
|
| dotted_identifier DOT undotted_identifier
|
||||||
{
|
{
|
||||||
posLast(yylex, yyDollar) // our pos
|
posLast(yylex, yyDollar) // our pos
|
||||||
$$.str = $1.str + interfaces.ModuleSep + $3.str
|
$$.str = $1.str + interfaces.ModuleSep + $3.str
|
||||||
|
|||||||
Reference in New Issue
Block a user