Jump to content

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>mapfold</code>, <code>filtermap</code>, and <code>foldfilter</code>.
 
<pre> [ stack ] is mapfold.act ( --> s )
protect map.act
 
[ map.act put
[] swap witheach
[ map.act share do
nested join ]
map.act release ] is map ( [ x --> [ )
 
[ stack ] is filter.act ( --> s )
protect filter.act
 
[ filter.act put
[] swap witheach
[ dup
filter.act share do
iff [ nested join ]
else drop ]
filter.act release ] is filter ( [ x --> [ )
 
[ stack ] is fold.act ( --> s )
protect fold.act
 
Line 3,553 ⟶ 3,533:
witheach
[ fold.act share do ] ]
fold.act release ] is fold ( [ x --> x )</pre>
 
[ ' [ [ ] ] 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
iff' [ nested join ] join
fold map.act] release ] is map (is map ( [ x --> [ )
 
[ ' [ [ ] ] rot join swap
<pre>/O> $ "esreveR yreve gnirts ni a tsen fo .sgnirts" nest$
nested ' dup swap join
' [ iff [ nested join ]
else drop ] join
[ stack fold ] is filter.act ( ( [ x --> s [ )
</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, and flattening a deeply nested nest.
 
<pre>/O> ' [ 1 2 [ [ 3 4 ] ] 5 [ 6 [ 7 [ 8 [ 9 ] ] ] ] ]
... [ dup
... ' join fold
... tuck = until ]
... echo
...
[ 1 2 3 4 5 6 7 8 9 ]
Stack empty.
 
<pre>/O> $ "esreveR yreve gnirts ni a tsen fo .sgnirts" nest$
... ' 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 ] ] ] ] ]
... [ dup
... ' join fold
... tuck = until ]
... echo
...
[ 1 2 3 4 5 6 7 8 9 ]
Stack empty.
</pre>
 
=={{header|R}}==
1,462

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.