Bind is an Apply that also has bind method. Scalaz defines Bind[F[_]] trait with bind abstract method.
Having F[A] and mapping A to F[A] it returns F[B].
Since Bind is Apply it inherits all the methods that Apply offers plus it needs to provide implementation for map and ap methods. Note that Bind implements ap using bind and map methods:
Having bind method we can derive join method which will take F[F[A]] and turn it into F[A]:
We can also define monadic if, having F[Boolean], F[B] and F[B] we return first F[B] or last.
Scalaz offers following syntax/derived functions:
- ∗, >>= and flatMap. All aliases for bind method.
- μ and join. When there is implicit Liskov A to F[B] it will join F[A] to F[B].
- >>. Converts to F[B] regardeless of the value of a.
- ifM. If there is implicit to convert A to boolean it returns true F[B] or false F[B] based on boolean value.
Since Option is Bind I’m gonna demonstrate those methods on Option with short examples:
Output
1.some.flatMap(a => (a + 1).some) Some(2)
1.some >>= (a => (a + 1).some) Some(2)
1.some ∗ (a => (a + 1).some) Some(2)
none[Int] ∗ (a => (a + 1).some) None
1.some >> 5.some Some(5)
none[Int] >> 5.some None
true.some.ifM(1.some, 2.some) Some(1)
false.some.ifM(1.some, 2.some) Some(2)