You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For more information about the link type, see #227.
Dyon has a link loop that makes repeated patterns of code generation or templates easier and faster.
The link items inside loop body is evaluated directly as if it was inside a link { ... } block.
For example:
gen_struct(name, fields:[{}]) = link {"pub struct "name" {\n"link i {" pub "fields[i].name":"fields[i].type",\n"
}"}\n\n"
}
Void expressions, continue and break
You can put expressions that do not return a value inside a link loop, and also use continue and break like in other loops. This is useful for filtering:
fnmain(){
list := sift i 10{i+1}
x := link i {// The `else` case returns no value, but that is OK.
if (list[i] > 2) && (list[i] <= 7){continue}
list[i]", "
}// prints `1, 2, 8, 9, 10,`println(x)}
The continue keyword skips the rest of the link items for the current iteration, but link items that already are evaluated will be inserted.
The break keyword skips the rest of the link items and exits the loop.
Here is an example where you print out a list without a trailing comma:
fnmain(){
list := sift i 10{i+1}
last := len(list)-1
x := link i {list[i]if i == last {break}", "}// prints `1, 2, 3, 4, 5, 6, 7, 8, 9, 10`println(x)}
When using a link { ... } inside the body, you get all-or-nothing case. Notice how 10 disappeared compared to the last example:
fnmain(){
list := sift i 10{i+1}
last := len(list)-1
x := link i {link {list[i]if i == last {break}", "}}// prints `1, 2, 3, 4, 5, 6, 7, 8, 9, `println(x)}
You can use packed loop notation with the link loop as if it was a flat loop. Here is an advanced example:
fnmain(){
n := 3
list := sift i n, j n, k n{floor(random()*9)+1}
last := n-1
x := link {"["'out: link i, j, k { if (i != 0) && (j == 0) && (k == 0) {""} list[i][j][k] if (i == last) && (j == last) && (k == last) {break 'out} else if (j == last) && (k == last) {";\n"}elseif(k == last){", "}else{" "}}"]"}// prints something like:// ```// [1 6 7, 1 7 7, 6 1 3;// 8 1 7, 5 7 9, 7 1 9;// 8 6 1, 7 9 8, 8 7 9]// ```println(x)}
The text was updated successfully, but these errors were encountered:
For more information about the link type, see #227.
Dyon has a link loop that makes repeated patterns of code generation or templates easier and faster.
The link items inside loop body is evaluated directly as if it was inside a
link { ... }
block.For example:
Void expressions,
continue
andbreak
You can put expressions that do not return a value inside a link loop, and also use
continue
andbreak
like in other loops. This is useful for filtering:The
continue
keyword skips the rest of the link items for the current iteration, but link items that already are evaluated will be inserted.The
break
keyword skips the rest of the link items and exits the loop.Here is an example where you print out a list without a trailing comma:
When using a
link { ... }
inside the body, you get all-or-nothing case. Notice how10
disappeared compared to the last example:You can use packed loop notation with the link loop as if it was a flat loop. Here is an advanced example:
The text was updated successfully, but these errors were encountered: