Anonymous user
List comprehensions: Difference between revisions
Updates example with macros to compile with Nim version 1.4.
(→{{header|Nim}}: Add example using collect()) |
(Updates example with macros to compile with Nim version 1.4.) |
||
Line 1,586:
var lc*: ListComprehension
macro `[]`*(lc: ListComprehension, x, t):
expectLen(x, 3)
expectKind(x, nnkInfix)
expectKind(x[0], nnkIdent)
assert($x[0].
result = newCall(
Line 1,602:
expectKind(y, nnkInfix)
expectMinLen(y, 1)
if y[0].kind == nnkIdent and $y[0].
expectLen(y, 3)
result = newNimNode(nnkForStmt).add(y[1], y[2], result)
Line 1,629:
const n = 20
echo lc[(x,y,z) | (x <- 1..n, y <- x..n, z <- y..n, x*x + y*y == z*z), tuple[a,b,c: int]]</lang>
Output:
<pre>@[(a: 3, b: 4, c: 5), (a: 5, b: 12, c: 13), (a: 6, b: 8, c: 10), (a: 8, b: 15, c: 17), (a: 9, b: 12, c: 15), (a: 12, b: 16, c: 20)]</pre>
|