I just wrote functions like this up to map4
just because they seem useful:
map2 :: Functor f => (i -> a) -> (i -> b) -> f i -> f (a,b)
map2 f1 f2 = fmap $ \i -> (f1 i, f2 i)
Before I continue to map8
i thought I'd ask if there is something similar in some standard module. Hayoo doesn't seem to know any function that has the signature above.
Note: I already found Control.Arrow.&&&
which reduces the above to:
map2 f1 f2 = fmap (f1 &&& f2)
But there doesn't seem to be a similar function for a fanout more than two.
(->) i
is an applicative functor, so you can write (&&&)
as
f &&& g = (,) <$> f <*> g
and you could write map3
as
map3 f1 f2 f3 = map ((,,) <$> f1 <*> f2 <*> f3)
except that it isn't shorter than
map3 f1 f2 f3 = map $ \i -> (f1 i, f2 i, f3 i)
But thanks to Gabriel's tip, this is shorter:
map3 f1 f2 f3 = map (liftA3 (,,) f1 f2 f3)
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