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.
32 lines
626 B
Plaintext
32 lines
626 B
Plaintext
-- main.mcl --
|
|
import "fmt"
|
|
|
|
$map0 = {0 => "a", 1 => "b", 2 => "c",}
|
|
$map1 = {0 => "d", 1 => "e", 2 => "f",}
|
|
$map2 = {0 => "g", 1 => "h", 2 => "i",}
|
|
$map3 = {0 => "j", 1 => "k", 2 => "l",}
|
|
|
|
$list = [$map0, $map1, $map2, $map3,]
|
|
|
|
for $index, $value in $list {
|
|
forkv $key, $val in $value {
|
|
|
|
$s = fmt.printf("%s is %d", $val, $key+$index)
|
|
test [$s,] {}
|
|
}
|
|
}
|
|
|
|
-- OUTPUT --
|
|
Vertex: test[a is 0]
|
|
Vertex: test[b is 1]
|
|
Vertex: test[c is 2]
|
|
Vertex: test[d is 1]
|
|
Vertex: test[e is 2]
|
|
Vertex: test[f is 3]
|
|
Vertex: test[g is 2]
|
|
Vertex: test[h is 3]
|
|
Vertex: test[i is 4]
|
|
Vertex: test[j is 3]
|
|
Vertex: test[k is 4]
|
|
Vertex: test[l is 5]
|