Created
October 7, 2015 13:59
Function composition in Perl 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub infix:<|\>> ($f, $g) { | |
-> $x { | |
$f($g($x)) | |
} | |
}; | |
my $mult_2 = -> $x { $x * 2 }; | |
my $plus_3 = -> $x { $x + 3 }; | |
my $compound = $mult_2 |> $plus_3; | |
printf("f o g (x) = %d\n", $compound(1));# => (1 + 3) * 2 = 8 | |
printf("f o g (x) = %d\n", $compound(2));# => (2 + 3) * 2 = 10 | |
printf("f o g (x) = %d\n", $compound(3));# => (3 + 3) * 2 = 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FWIW, Perl6 has a function combinator operator ( link ).
If you prefer your infix
|>
over∘
oro
then you can just alias like so:my &infix:«|>» = &infix:«∘»;