Function composition: Difference between revisions

m (added a ;Related task: (bold) header.)
Line 1,603:
my &f = &triple ∘ &prefix:<-> ∘ { $^n + 2 };
say &f(5); # Prints "-21".</lang>
 
=={{header|Phix}}==
There is not really any direct support for this sort of thing in Phix, but it is all pretty trivial to manage explicitly.<br>
In the following, as it stands, you cannot use constant m in the same way as a routine_id, or pass a standard routine_id to call_composite(), but tagging the ctable entries so that you know precisely what to do with each entry does not sound the least bit difficult to me.
<lang Phix>sequence ctable = {}
 
function compose(integer f, integer g)
ctable = append(ctable,{f,g})
return length(ctable)
end function
 
function call_composite(integer f, atom x)
integer g
{f,g} = ctable[f]
return call_func(f,{call_func(g,{x})})
end function
 
function plus1(atom x)
return x+1
end function
 
function halve(atom x)
return x/2
end function
 
constant m = compose(routine_id("halve"),routine_id("plus1"))
 
?call_composite(m,1) -- displays 1
?call_composite(m,4) -- displays 2.5</lang>
 
=={{header|PHP}}==
7,806

edits