@@ -125,24 +125,46 @@ instance altEither :: Alt (Either e) where
125125-- | Left x >>= f = Left x
126126-- | Right x >>= f = f x
127127-- | ```
128- instance bindEither :: Bind (Either e ) where
129- bind = either (\e _ -> Left e) (\a f -> f a)
130-
131- -- | The `Monad` instance guarantees that there are both `Applicative` and
132- -- | `Bind` instances for `Either`. This also enables the `do` syntactic sugar:
133128-- |
129+ -- | `Either`'s "do notation" can be understood to work like this:
134130-- | ``` purescript
135- -- | do
131+ -- | x :: forall e a. Either e a
132+ -- | x = --
133+ -- |
134+ -- | y :: forall e b. Either e b
135+ -- | y = --
136+ -- |
137+ -- | foo :: forall e a. (a -> b -> c) -> Either e c
138+ -- | foo f = do
136139-- | x' <- x
137140-- | y' <- y
138141-- | pure (f x' y')
139142-- | ```
140143-- |
141- -- | Which is equivalent to:
144+ -- | ...which is equivalent to...
142145-- |
143146-- | ``` purescript
144147-- | x >>= (\x' -> y >>= (\y' -> pure (f x' y')))
145148-- | ```
149+ -- |
150+ -- | ...and is the same as writing...
151+ -- |
152+ -- | ```
153+ -- | foo :: forall e a. (a -> b -> c) -> Either e c
154+ -- | foo f = case x of
155+ -- | Left e ->
156+ -- | Left e
157+ -- | Right x -> case y of
158+ -- | Left e ->
159+ -- | Left e
160+ -- | Right y ->
161+ -- | Right (f x y)
162+ -- | ```
163+ instance bindEither :: Bind (Either e ) where
164+ bind = either (\e _ -> Left e) (\a f -> f a)
165+
166+ -- | The `Monad` instance guarantees that there are both `Applicative` and
167+ -- | `Bind` instances for `Either`.
146168instance monadEither :: Monad (Either e )
147169
148170-- | The `Extend` instance allows sequencing of `Either` values and functions
@@ -251,15 +273,19 @@ isLeft = either (const true) (const false)
251273isRight :: forall a b . Either a b -> Boolean
252274isRight = either (const false ) (const true )
253275
254- -- | A partial function that extracts the value from the `Left` data constructor.
255- -- | Passing a `Right` to `fromLeft` will throw an error at runtime.
256- fromLeft :: forall a b . Partial => Either a b -> a
257- fromLeft (Left a) = a
258-
259- -- | A partial function that extracts the value from the `Right` data constructor.
260- -- | Passing a `Left` to `fromRight` will throw an error at runtime.
261- fromRight :: forall a b . Partial => Either a b -> b
262- fromRight (Right a) = a
276+ -- | A function that extracts the value from the `Left` data constructor.
277+ -- | The first argument is a default value, which will be returned in the
278+ -- | case where a `Right` is passed to `fromLeft`.
279+ fromLeft :: forall a b . a -> Either a b -> a
280+ fromLeft _ (Left a) = a
281+ fromLeft default _ = default
282+
283+ -- | A function that extracts the value from the `Right` data constructor.
284+ -- | The first argument is a default value, which will be returned in the
285+ -- | case where a `Left` is passed to `fromRight`.
286+ fromRight :: forall a b . b -> Either a b -> b
287+ fromRight _ (Right b) = b
288+ fromRight default _ = default
263289
264290-- | Takes a default and a `Maybe` value, if the value is a `Just`, turn it into
265291-- | a `Right`, if the value is a `Nothing` use the provided default as a `Left`
0 commit comments