Skip to content

Instantly share code, notes, and snippets.

@einarwh
Last active May 9, 2021 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarwh/1fa56357ab259e45391994bc20b85897 to your computer and use it in GitHub Desktop.
Save einarwh/1fa56357ab259e45391994bc20b85897 to your computer and use it in GitHub Desktop.
%%% Some: [v]
%%% None: []
/some { % v
[ exch ] % [v]
} def
/none {
[]
} def
/map { % A {op} %%% {op} is operation to be called for each element in A
[ 3 1 roll % [ A {op}
exch % [ {op} A
{ % [ {op} e
1 index % [ {op} e {op}
exec % [ {op} r %%% r is result of executing {op} on e
exch % [ r {op}
} forall % [ ... {op}
pop % [ ...
] % [ r1 r2 ... ] => [ r1 r2 ... ]
} def
/flatten { % A[B] %%% A is an array containing arrays.
[ exch % A[B] |
{ % B |
{} forall % ... |
} forall % ... |
] % [ ... ]
} def
/flatmap {
map flatten
} def
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment