Combining Binding and Return

Consider:

x `f` y = x >>= (return . y)

This function fseems very similar to <$>and flip liftM, but <$>doesn’t seem to work, and I will need to define the infix operator for flip liftMit to look beautiful, and I Assuming it already exists?

Is there a function like what I described and what is it?

+3
source share
3 answers

This flip liftM, but not <$>. It is also almost exactly the same as flip <$>, but the latter is for Functortypeclass, not Monad. (In the latest standard libraries, the relationship between Functorand Monadis not yet reflected in the type hierarchy, but it will ).

, , FP Complete Hoogle, ,

Functor f => f a -> (a -> b) -> f b

.

+7

x `f` y = x >>= (return . y)

flip fmap, , , import Data.Functor, fmap

y <$> x

( , Functor Monad, .)

, ,

munge = Just . remove bits . add things <$> operation 1 
            >>= increase something <$> operation 2

munge' = do
     thing1 <- operation 1
     let thing2 = Just . remove bits. add things $ thing1
     thing3 <- operation 2
     return . increase something $ thing3

, import Control.Applicative ( <$>), , :

addLine = (+) <$> readLine <*> readLine >>= print

addLine' = do
    one <- readLine
    two <- readLine
    print (one + two)

--, Monads (, , ). .

, pure = return

mf <*> mx = do
   f <- mf
   x <- mx
   return (f x)

Functor,

fmap f mx = do
   x <- mx
   return (f x)
(<*>) = ap fmap = liftM, Control.Monad, , , , .
+2

Data.Generics.Serialization.Standard (>>$), flip liftM. , , , . , . , ​​.

, , , , .

+1

All Articles