23 lines
358 B
Text
23 lines
358 B
Text
\ take a list (array) and flatten it:
|
|
|
|
: (flatten) \ a -- a
|
|
(
|
|
\ is it a number?
|
|
dup >kind ns:n n:= if
|
|
\ yes. so add to the list
|
|
r> swap a:push >r
|
|
else
|
|
\ it is not, so flatten it
|
|
(flatten)
|
|
then
|
|
drop
|
|
) a:each drop ;
|
|
|
|
: flatten \ a -- a
|
|
[] >r (flatten) r> ;
|
|
|
|
[[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
|
|
dup . cr
|
|
flatten
|
|
. cr
|
|
bye
|