Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put two monadic values into a pair and return it

I am playing with Parsec and I want to combine two parsers into one with the result put in a pair, and then feed it another function to operate on the parse result to write something like this:

try (pFactor <&> (char '*' *> pTerm) `using` (*))

So I wrote this:

(<&>) :: (Monad m) => m a -> m b -> m (a, b)
pa <&> pb = do
  a <- pa
  b <- pb
  return (a, b)

And

using :: (Functor f) => f (a, b) -> (a -> b -> c) -> f c
p `using` f = (uncurry f) <$> p

Is there anything similar to (<&>) which has been implemented somewhere? Or could this be written pointfree? I tried fmap (,) but it seems hard to match the type.

like image 200
Lin Jen-Shin Avatar asked Nov 28 '22 14:11

Lin Jen-Shin


1 Answers

Better than <&> or liftM2 would be

(,) <$> a <*> b

since Applicative style seems to be gaining popularity and is very succinct. Using applicative style for things like this will eliminate the need for <&> itself, since it is much clearer than (,) <$> a <*> b.

Also this doesn't even require a monad - it will work for Applicatives too.

like image 173
alternative Avatar answered Dec 05 '22 03:12

alternative