Partial function application: Difference between revisions

Line 1,297:
=={{header|Oforth}}==
 
<lang Oforth>func: partialfs(fs, pf) { #[ p f performs ]map };
Oforth does not return functions, just blocks. So it can't return partial functions. One way is to create a function that returns a closure : a block that will act like a partial function.
: f1 2 * ;
: f2 sq ;
 
#f1 #fs curry => fsf1
<lang Oforth>func: partial(f, p) { #[ p f perform ] }
#f2 #fs curry => fsf2
 
}</lang>
func: fs(f, s) { s map(f) }
func: f1(n) { n 2 * }
func: f2(n) { n sq }
 
func: testPartial
{
| fsf1 fsf2 |
 
partial(#fs, #f1) ->fsf1
partial(#fs, #f2) ->fsf2
 
[ 0, 1, 2, 3 ] fsf1 perform println
[ 0, 1, 2, 3 ] fsf2 perform println
[ 2, 4, 6, 8 ] fsf1 perform println
[ 2, 4, 6, 8 ] fsf2 perform println
}</lang>
 
{{out}}
<pre>
[0, 2, 4, 6]
>[ 0, 1, 42, 93 ] fsf1 .
[40, 82, 124, 166] ok
>[4 0, 161, 362, 643 ] fsf2 .
[0, 1, 4, 9] ok
>[ 02, 14, 26, 38 ] fsf1 perform println.
[4, 8, 12, 16] ok
>[ 02, 14, 26, 38 ] fsf2 perform println.
[4, 16, 36, 64] ok
</pre>
 
1,015

edits