Function composition: Difference between revisions

Add lang example
m (→‎{{header|Kotlin}}: made compose function generic)
(Add lang example)
Line 1,778:
{{f} 3}
-> 80
</syntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
fp.f = ($x) -> return parser.op($x * 3)
fp.g = ($x) -> return parser.op($x + 2)
 
$x = 5
 
# In lang this task can be achieved with the concat operator
parser.op(fp.g ||| fp.f)($x) # Prints 21 [Internal execution: fp.f(fp.g($x))]
 
# Creating a user-defined function doing the same thing is a bit more difficult
fp.compose = (fp.f, fp.g) -> {
fp.innerFunc = (fp.f, fp.g, $x) -> return fp.f(fp.g($x))
return fn.argCnt1(fn.combA3(fp.innerFunc, fp.f, fp.g)) # fn.combA3 must be used, because lang does not support closures
}
 
fp.compose(fp.f, fp.g)($x) # Prints also 21
</syntaxhighlight>
 
168

edits