Open masaeedu opened 6 years ago
fmap
for (a ->)
type is compose right, so compose2 = (.) . (.)
@safareli Right, but if you can write it so it works with all functors instead of just functions, why not?
@masaeedu compose
is a member of Semigroupoid
so it works for more than just functions.
Can you give an example of how you would want to use compose2
with functors that aren't functions?
@gabejohnson Ah, I wasn't aware of that. I guess .
satisfies multiple typeclasses. A compose<N>
function as I have it up there simply fmaps over functors nested N levels deep. E.g. you can compose
a String -> Int
with an [String]
to get an [Int]
, or compose2
a String -> Int
with a [Map String String]
to get [Map String Int]
.
Maybe it should be called map2
etc. in Sanctuary.
The blackbird makes an appearance!
const compose2 = S.compose (S.compose) (S.compose);
const insert = compose2 (S.concat) (S.singleton);
insert ('z') (3) ({x: 1, y: 2}); // => {x: 1, y: 2, z: 3}
Defining S.compose2
and S.compose3
seems reasonable to me. Others may disagree. Feel free to submit a pull request, Asad. :)
It'd be nice to have some utilities to compose functions of 2, 3, etc. arguments with another that transforms the final argument. In Haskell, for example, you can have:
And so on for higher orders with
compose3 = fmap . fmap . fmap
for composing 3 argument functions etc.