Skip to content

Instantly share code, notes, and snippets.

@franckverrot
Created October 7, 2015 13:59
Show Gist options
  • Save franckverrot/1c255a825895861df975 to your computer and use it in GitHub Desktop.
Save franckverrot/1c255a825895861df975 to your computer and use it in GitHub Desktop.
Function composition in Perl 6
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
@0racle
Copy link

0racle commented Jun 20, 2017

FWIW, Perl6 has a function combinator operator ( link ).

If you prefer your infix |> over or o then you can just alias like so: my &infix:«|>» = &infix:«∘»;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment