diff --git a/src/Data/Either.purs b/src/Data/Either.purs index ab4167f..65cc805 100644 --- a/src/Data/Either.purs +++ b/src/Data/Either.purs @@ -125,24 +125,46 @@ instance altEither :: Alt (Either e) where -- | Left x >>= f = Left x -- | Right x >>= f = f x -- | ``` -instance bindEither :: Bind (Either e) where - bind = either (\e _ -> Left e) (\a f -> f a) - --- | The `Monad` instance guarantees that there are both `Applicative` and --- | `Bind` instances for `Either`. This also enables the `do` syntactic sugar: -- | +-- | `Either`'s "do notation" can be understood to work like this: -- | ``` purescript --- | do +-- | x :: forall e a. Either e a +-- | x = -- +-- | +-- | y :: forall e b. Either e b +-- | y = -- +-- | +-- | foo :: forall e a. (a -> b -> c) -> Either e c +-- | foo f = do -- | x' <- x -- | y' <- y -- | pure (f x' y') -- | ``` -- | --- | Which is equivalent to: +-- | ...which is equivalent to... -- | -- | ``` purescript -- | x >>= (\x' -> y >>= (\y' -> pure (f x' y'))) -- | ``` +-- | +-- | ...and is the same as writing... +-- | +-- | ``` +-- | foo :: forall e a. (a -> b -> c) -> Either e c +-- | foo f = case x of +-- | Left e -> +-- | Left e +-- | Right x -> case y of +-- | Left e -> +-- | Left e +-- | Right y -> +-- | Right (f x y) +-- | ``` +instance bindEither :: Bind (Either e) where + bind = either (\e _ -> Left e) (\a f -> f a) + +-- | The `Monad` instance guarantees that there are both `Applicative` and +-- | `Bind` instances for `Either`. instance monadEither :: Monad (Either e) -- | The `Extend` instance allows sequencing of `Either` values and functions