This adds a forkv statement which is used to iterate over a map with a body of statements. This is an important data transformation tool which should be used sparingly, but is important to have. An import statement inside of a forkv loop is not currently supported. We have a simple hack to detect the obvious cases, but more deeply nested scenarios probably won't be caught, and you'll get an obscure error message if you try to do this. This was incredibly challenging to get right, and it's all thanks to Sam for his brilliance. Note, I couldn't think of a better keyword that "forkv" but suggestions are welcome if you think you have a better idea. Other ideas were formap and foreach, but neither got me very excited.
42 lines
1002 B
Plaintext
42 lines
1002 B
Plaintext
-- main.mcl --
|
|
import "fmt"
|
|
|
|
$map1 = {0 => "a", 1 => "b", 2 => "c",}
|
|
$map2 = {0 => "x", 1 => "y", 2 => "z",}
|
|
|
|
$word = "hello"
|
|
|
|
forkv $key1, $val1 in $map1 {
|
|
forkv $key2, $val2 in $map2 {
|
|
|
|
class foo($x, $y) {
|
|
$result = "hello " + $x + $y + $val1 + $val2
|
|
$result1 = $x + $val1
|
|
$result2 = $y + $val2
|
|
}
|
|
include foo($val1, $val2) as included
|
|
|
|
$s = fmt.printf("%s is {%d,%d}", $included.result, $key1, $key2)
|
|
$s1 = fmt.printf("one: %s", $included.result1)
|
|
$s2 = fmt.printf("two: %s", $included.result2)
|
|
test [$s, $s1, $s2,] {}
|
|
}
|
|
}
|
|
|
|
-- OUTPUT --
|
|
Vertex: test[hello axax is {0,0}]
|
|
Vertex: test[hello ayay is {0,1}]
|
|
Vertex: test[hello azaz is {0,2}]
|
|
Vertex: test[hello bxbx is {1,0}]
|
|
Vertex: test[hello byby is {1,1}]
|
|
Vertex: test[hello bzbz is {1,2}]
|
|
Vertex: test[hello cxcx is {2,0}]
|
|
Vertex: test[hello cycy is {2,1}]
|
|
Vertex: test[hello czcz is {2,2}]
|
|
Vertex: test[one: aa]
|
|
Vertex: test[one: bb]
|
|
Vertex: test[one: cc]
|
|
Vertex: test[two: xx]
|
|
Vertex: test[two: yy]
|
|
Vertex: test[two: zz]
|