First-class functions
Perl 6
<lang perl6>my $cube = * ** 3; &croot = * ** (1/3); # Equivalent to 'sub croot ($x) { $x**(1/3) }'
sub compose ($f, $g) { return { $f($g($^x)) } }
my @functions = &sin, &cos, $cube; my @inverses = &asin, &acos, &croot;
{ say compose($^g, $^f)(.5) } for @functions Z @inverses;</lang>
Python
<lang python>>>> # Some built in functions and their inverses >>> from math import sin, cos, acos, asin >>> # Add a user defined function and its inverse >>> cube = lambda x: x * x * x >>> croot = lambda x: x ** (1/3.0) >>> # First class functions allow run-time creation of functions from functions >>> # return function compose(f,g)(x) == f(g(x)) >>> compose = lambda f1, f2: ( lambda x: f1(f2(x)) ) >>> # first class functions should be able to be members of collection types >>> funclist = [sin, cos, cube] >>> funclisti = [asin, acos, croot] >>> # Apply functions from lists as easily as integers >>> [compose(inversef, f)(.5) for f, inversef in zip(funclist, funclisti)] [0.5, 0.4999999999999999, 0.5] >>> </lang>