Often I find I want to compose two functions f and g, but g takes multiple arguments. Does Haskell provide a set of operators for that (I know I could write it myself, but it seems fairly common and I don't want to duplicate an operator that already exists in Haskell)
ie something like
(.@) = (.)
(.@@) f g x1 x2 = f $ g x1 x2
(.@@@) f g x1 x2 x3 = f $ g x1 x2 x3
(.@@@@) f g x1 x2 x3 x4 = f $ g x1 x2 x3 x4
...
up to some reasonable number of arguments
I know you got the answer you wanted, but I wanted to point out that these combinators have the following cute implementation:
(.:) = (.) . (.)
(.:.) = (.) . (.) . (.)
(.::) = (.) . (.) . (.) . (.)
(.::.) = (.) . (.) . (.) . (.) . (.)
and if you only need them fully applied:
f .: g = (f .) . g
f .:. g = ((f .) .) . g
f .:: g = (((f .) .) .) . g
f .::. g = ((((f .) .) .) .) . g
It doesn't seem so terrible to use these expressions directly, without defining an operator. At least the first one, (f .) . g
, seems readable enough to me.
From @bheklilr 's comment, the answer I was looking for is in the composition library: http://hackage.haskell.org/package/composition-1.0.1.0/docs/Data-Composition.html
The functions (.:), (.:.), (.::), (.::.) etc. do exactly what I was thinking
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With