Partial function application: Difference between revisions

(→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details)
Line 258:
 
=={{header|Bracmat}}==
This task is hard to solve if we use imperative/procedural style Bracmat functions. Instead, we use lambda expressions throughout the solution given below.
The body of the function fs consists of two macros. The first macro merely optimizes the second one by replacing the name of the argument function by the definition of the function. The second macro inserts this function in a function body that implements an iteration over a list.
The the function <code>fs</code> consists of a lambda abstraction inside a lambda abstraction. In that way <code>fs</code> can take two arguments. Similarly, the function <code>partial</code>, which also needs to take two arguments, is defined using lambda abstractions.
The function called partial uses a lambda abstraction to construct a new functions from two functions.
Currying takes place by applying a two-argument function to its first argument. This happens in <code>($x)$($y)</code>.
<lang bracmat>( ( fs
<lang bracmat>( & (fs=/('(x./('(y.map'($x)$(.$y)))$!g))$!f)
=
& (f1=/('(x.2$x*!arg2)))
. '$!arg:?arg
& (f2=/('(x.!arg$x^2)))
&
& (partial=/('(x./('(y.($x)$($y))))))
' ( first r
& (!partial$(!fs.)$!f1):(=?fsf1)
. :?r
& (!partial$(!fs.)$!f2):(=?fsf2)
& whl
& out$(!fsf1$(0 1 2 3))
' ( !arg:%?first ?arg
& out$(!fsf2$(0 1 2 3))
& !r ($arg)$!first:?r
& out$(!fsf1$(2 4 6 8))
)
& out$(!fsf2$(2 4 6 8))
& !r
)
)
& ( partial
=
. !arg:(?f.?g)
& /('(x./('(y.($x)$($y)))$!g))$!f
)
& (f1=.2*!arg)
& (f2=.!arg^2)
& partial$(fs.f1):(=?fsf1)
& partial$(fs.f2):(=?fsf2)
& out$(fsf1$(0 1 2 3))
& out$(fsf2$(0 1 2 3))
& out$(fsf1$(2 4 6 8))
& out$(fsf2$(2 4 6 8))
);</lang>
Output:
483

edits