Higher-order functions: Difference between revisions
→{{header|Quackery}}: replaced my previous solution with one that defines map and filter in terms of fold
m (→{{header|Quackery}}: clarification) |
(→{{header|Quackery}}: replaced my previous solution with one that defines map and filter in terms of fold) |
||
Line 3,523:
=={{header|Quackery}}==
First define the higher order functions <code>
<pre> [ stack ]
nested join ]▼
map.act release ] is map ( [ x --> [ )▼
[ stack ] is filter.act ( --> s )▼
[ dup▼
iff [ nested join ]▼
else drop ]▼
protect fold.act
Line 3,553 ⟶ 3,533:
witheach
[ fold.act share do ] ]
fold.act release ] is fold
[ ' [ [ ] ] rot join swap
Then test them in the Quackery shell by reversing every string in a nest of strings, removing all the negative numbers from a nest of numbers, and flattening a deeply nested nest.▼
nested
[ ' [ [ ] ] rot join swap
<pre>/O> $ "esreveR yreve gnirts ni a tsen fo .sgnirts" nest$▼
nested ' dup swap join
▲ ' [ iff [ nested join ]
▲ else drop ] join
</pre>▼
▲Then test them in the Quackery shell by flattening a deeply nested nest, reversing every string in a nest of strings, and removing all the negative numbers from a nest of numbers
<pre>/O> ' [ 1 2 [ [ 3 4 ] ] 5 [ 6 [ 7 [ 8 [ 9 ] ] ] ] ]▼
... ' join fold▼
... tuck = until ]▼
... echo▼
... ▼
[ 1 2 3 4 5 6 7 8 9 ]▼
Stack empty.▼
... ' reverse map
... witheach [ echo$ sp ]
Line 3,569 ⟶ 3,570:
...
[ 42 23 ]
Stack empty.</pre>
▲/O> ' [ 1 2 [ [ 3 4 ] ] 5 [ 6 [ 7 [ 8 [ 9 ] ] ] ] ]
▲... ' join fold
▲... tuck = until ]
▲... echo
▲...
▲[ 1 2 3 4 5 6 7 8 9 ]
▲Stack empty.
▲</pre>
=={{header|R}}==
|