Anonymous user
Flatten a list: Difference between revisions
→{{header|Nim}}: simplify code
Drkameleon (talk | contribs) (added Arturo) |
(→{{header|Nim}}: simplify code) |
||
Line 2,546:
=={{header|Nim}}==
Nim is statically-typed, so we need to use an object variant
<lang nim>type
TreeList[T] =
case isLeaf: bool
of true:
of false: list: seq[TreeList[T]]
proc L[T](list: varargs[TreeList[T]]): TreeList[T] =
proc N[T](data: T): TreeList[T] =
TreeList[T](isLeaf: true, data: data)
▲ for i, x in n.list:
proc flatten[T](n: TreeList[T]): seq[T] =
if n.isLeaf: result = @[n.data]
else:
for x in n.list:
result.add flatten x
var x = L(L(N 1), N 2, L(L(N 3, N 4), N 5), L(L(L[int]())), L(L(L(N 6))), N 7, N 8, L[int]())
echo flatten(x)</lang>
{{out}}
<pre>
=={{header|Objective-C}}==
|