Partial function application: Difference between revisions

→‎Tcl: Added implementation
(New task and Python solution.)
 
(→‎Tcl: Added implementation)
Line 43:
 
The program runs without triggering the assertions.
 
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<lang tcl>package require Tcl 8.6
proc fs {f {s {}}} {
variable ctr
set c [coroutine __curry[incr ctr] apply {f {
set r [info coroutine]
while 1 {
set s [yield $r]
set r {}
foreach n $s {lappend r [{*}$f $n]}
}
}} $f]
if {[llength [info level 0]] == 3} {
return [$c $s][rename $c {}]
} else {
return $c
}
}</lang>
Demonstration:
<lang tcl>proc f1 x {expr {$x * 2}}
proc f2 x {expr {$x ** 2}}
set fsf1 [fs f1]
set fsf2 [fs f2]
foreach s {{0 1 2 3} {2 4 6 8}} {
puts "$s ==f1==> [$fsf1 $s]"
puts "$s ==f2==> [$fsf2 $s]"
}</lang>
Output:
<pre>
0 1 2 3 ==f1==> 0 2 4 6
0 1 2 3 ==f2==> 0 1 4 9
2 4 6 8 ==f1==> 4 8 12 16
2 4 6 8 ==f2==> 4 16 36 64
</pre>
Anonymous user