In Operators§
See primary documentation in context for infix o, infix ∘
multi infix:<o>() multi infix:<o>(&f) multi infix:<o>(&f, &g --> Block:D)
The function composition operator infix:<∘>
or infix:<o>
combines two functions, so that the left function is called with the return value of the right function. If the .count
of the left function is greater than 1, the return value of the right function will be slipped into the left function.
Both .count
and .arity
of the right-hand side will be maintained, as well as the .of
of the left hand side.
sub f($p){ say 'f'; $p / 2 } sub g($p){ say 'g'; $p * 2 } my &composed = &f ∘ &g; say composed 2; # OUTPUT: «gf2» # equivalent to: say 2.&g.&f; # or to: say f g 2; say &composed.arity; # OUTPUT: «1» say &composed.count; # OUTPUT: «1» say &composed.of; # OUTPUT: «(Mu)»
sub f($a, $b, $c) { [~] $c, $b, $a } sub g($str){ $str.comb } my &composed = &f ∘ &g; say composed 'abc'; # OUTPUT: «cba» # equivalent to: say f |g 'abc';
The single-arg candidate returns the given argument as is. The zero-arg candidate returns an identity routine that simply returns its argument.
my &composed = [∘] &uc; say composed 'foo'; # OUTPUT: «FOO» my &composed = [∘]; say composed 'foo'; # OUTPUT: «foo»