Jump to content

First-class functions: Difference between revisions

m
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
m (→‎{{header|Phix}}: tidied up)
Line 2,145:
=={{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, constant m cannot be used the same way as a routine_id, and a standard routine_id cannot be passed to he first argument of call_composite, but tagging ctable entries so that you know exactly what to do with them does not sound difficult to me.
<lang Phix>sequence ctable = {}
 
function compose(integer f, integer g)
ctable = append(ctable,{f,g})
returninteger cdx = length(ctable)
return cdx
end function
 
function call_composite(integer fcdx, atom x)
integer {f,g} = ctable[cdx]
{return f,(g} = ctable[f](x))
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>
7,805

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.