In Perl 5, we can apply functional programming techniques (using closures, higher order functions like map, grep, etc.). But how about function composition? Let’s say, in Haskell it can be done very easily with (.) function:
map (negate . abs) [-3, 2, 4, -1, 5]
What would be the equivalent of such “dot function” in Perl?
Sadly, I don’t know Haskell.
But function composition essentially is putting the output of one function into the next function as an argument.
output = (negate . abs)(input)is the same asoutput = negate(abs(input)). In Perl, parens are often optional, and the input is implicit in themapfunction, so we can just sayNow just translate this to Perl syntax, and we have
for the mathematical/algebraic negation, and
for the logical negation (which is the same as
map {! $_} (1,2,3), of course).