lucidbrot
lucidbrot

Reputation: 6188

How to read the bind operator ">>=" in words?

When reading code, I like to be able to form sentences in my head. For example, x <- getChar could be something like "x slurps getChar". Or a . b would be "b applied to a".

But when it comes to the Monadic Bind operator f >>= g, I just leave a mental gap in the sentence in my head, because I don't know how to read it. I thought about "f binds g", but that feels wrong. What suggestions do you have?

The proposed duplicate link contains some really nice answers for other operators, but for the bind operator the answers just say "bind". However, "f bind g" doesn't seem meaningful to me.

Upvotes: 1

Views: 211

Answers (3)

William Casarin
William Casarin

Reputation: 2582

Elm uses andThen which is equivalent to >>=

makeThingA >>= (\a -> consumeThing a)

aka

makeThingA `andThen` (\a -> consumeThing a)

Upvotes: 1

liminalisht
liminalisht

Reputation: 1253

I would suggest thinking of the word 'bind' in the sense of 'attach': binding g to f means attaching a monadic action g to the monad f. To flesh this out a bit, think of f, a monad, as a computation which, when run, will return some value (call this value x). Binding a monadic action g to a monad f expresses a new computation in which the monadic action g is now attached to the monad f, in the sense that the result (x) of the monadic computation f is to be passed along as an argument to the action g, which in turn returns a new (monadic) computation.

Upvotes: 1

danidiaz
danidiaz

Reputation: 27766

Names of >>= for monad-like constructs of other languages:

  • For Javascript promises, it is then. (Not a perfect correspondence though, because then does more things.)
  • For Java Optionals and Streams, it is flatMap (They are similarly named functions, but they don't come from the same interface.)
  • For Java's CompletableFuture, it is thenComposeAsync.

>>= is equivalent to first mapping with an effectful function

fmap putStrLn getLine :: IO (IO ())

and then "fusing" the two nested layers of the type constructor with join :: Monad m => m (m a) -> m a:

join (fmap putStrLn getLine) :: IO ()

So how about calling it "map 'n fuse"?

Upvotes: 2

Related Questions