First-class functions/Use numbers analogously: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 1,163: Line 1,163:
Compared to first class functions, there are (as in my view there should be) significant differences in the treatment of numbers and functions, but as mentioned on that page tagging ctable entries should be quite sufficient.
Compared to first class functions, there are (as in my view there should be) significant differences in the treatment of numbers and functions, but as mentioned on that page tagging ctable entries should be quite sufficient.
<lang Phix>sequence ctable = {}
<lang Phix>sequence ctable = {}

function compose(integer f, integer g)
function compose(integer f, g)
ctable = append(ctable,{f,g})
ctable = append(ctable,{f,g})
return length(ctable)
integer cdx = length(ctable)
return cdx
end function
end function

function call_composite(integer f, atom x)
function call_composite(integer cdx, atom x)
integer g
integer {f,g} = ctable[cdx]
{f,g} = ctable[f]
return f(g(x))
return call_func(f,{call_func(g,{x})})
end function
end function

function plus1(atom x)
function plus1(atom x)
return x+1
return x+1
end function
end function

function halve(atom x)
function halve(atom x)
return x/2
return x/2
end function
end function

constant m = compose(routine_id("halve"),routine_id("plus1"))
constant m = compose(halve,plus1)

?call_composite(m,1) -- displays 1
?call_composite(m,1) -- displays 1
?call_composite(m,4) -- displays 2.5</lang>
?call_composite(m,4) -- displays 2.5</lang>