Partial function application: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 1,424: | Line 1,424: | ||
(4 16 36 64)</pre> |
(4 16 36 64)</pre> |
||
The <tt>*+2</tt> is also a form of partial application in Perl 6. In this case we partially apply the <tt>infix:<+></tt> function with a second argument of 2. That is, the star (known as the "whatever" star) indicates which argument <em>not</em> to apply. In contrast to languages that keep some arguments unbound by leaving holes, the explicit star in Perl 6 allows us to avoid syntactic ambiguity in whether to expect a term or an infix operator; such self-clocking code contributes to better error messages when things go wrong. |
The <tt>*+2</tt> is also a form of partial application in Perl 6. In this case we partially apply the <tt>infix:<+></tt> function with a second argument of 2. That is, the star (known as the "whatever" star) indicates which argument <em>not</em> to apply. In contrast to languages that keep some arguments unbound by leaving holes, the explicit star in Perl 6 allows us to avoid syntactic ambiguity in whether to expect a term or an infix operator; such self-clocking code contributes to better error messages when things go wrong. |
||
=={{header|Phix}}== |
|||
Phix does not explicitly this, but you can easily emulate it with routine_id<br> |
|||
<lang Phix>function fs(integer rid, sequence s) |
|||
for i=1 to length(s) do |
|||
s[i] = call_func(rid,{s[i]}) |
|||
end for |
|||
return s |
|||
end function |
|||
function p_apply(sequence f, sequence args) |
|||
return call_func(f[1],{f[2],args}) |
|||
end function |
|||
function f1(integer i) |
|||
return i+i |
|||
end function |
|||
function f2(integer i) |
|||
return i*i |
|||
end function |
|||
constant fsf1 = {routine_id("fs"),routine_id("f1")}, |
|||
fsf2 = {routine_id("fs"),routine_id("f2")} |
|||
?p_apply(fsf1,{0,1,2,3}) |
|||
?p_apply(fsf2,{2,4,6,8})</lang> |
|||
{{out}} |
|||
<pre> |
|||
{0,2,4,6} |
|||
{4,16,36,64} |
|||
</pre> |
|||
Should you want the first few arguments set as part of fsf1/2 [ie as a 3rd sequence element], then obviously p_apply might be more like |
|||
<lang Phix>function p_apply(sequence ffsa, sequence extra_args) |
|||
object {fa,fx,set_args} = ffsa |
|||
return call_func(fa,{fx,set_args&extra_args}) |
|||
end function</lang> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |