Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

New features:
- Add `lefts` and `rights` functions (#71 by @l-monnier)

Bugfixes:

Expand Down
33 changes: 33 additions & 0 deletions src/Data/Either.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Data.Either where

import Prelude

import Control.Bind (bindFlipped)
import Control.Alt (class Alt, (<|>))
import Control.Extend (class Extend)
import Data.Eq (class Eq1)
Expand Down Expand Up @@ -253,6 +254,38 @@ fromRight' :: forall a b. (Unit -> b) -> Either a b -> b
fromRight' _ (Right b) = b
fromRight' default _ = default unit

-- | Keep from a `Monad` (typically an `Array`) of `Either` all
-- | the `Left` elements.
-- |
-- | For `Monad` with an order (such as `Array`),
-- | the initial order of the `Left` elements is preserved.
-- |
-- | A possible use case is to collect all errors of multiple `Either`.
-- |
-- | The type signature being more general than `Array`, it is possible to
-- | use it with other types such as `List`.
-- |
-- | ```purescript
-- | lefts [Left "Error 1", Right 42, Left "Error 2"] = ["Error 1", "Error 2"]
-- | ```
lefts :: forall a b m. Monad m => Monoid (m a) => m (Either a b) -> m a
lefts = bindFlipped $ either pure (const mempty)

-- | Keep from a `Monad` (typically an `Array`) of `Either` all
-- | the `Right` elements.
-- |
-- | For `Monad` with an order (such as `Array`),
-- | the initial order of the `Right` elements is preserved.
-- |
-- | The type signature being more general than `Array`, it is possible to
-- | use it with other types such as `List`.
-- |
-- | ```purescript
-- | rights [Left "Error 1", Right 42, Left "Error 2"] = [42]
-- | ```
rights :: forall a b m. Monad m => Monoid (m b) => m (Either a b) -> m b
rights = bindFlipped $ either (const mempty) pure

-- | Takes a default and a `Maybe` value, if the value is a `Just`, turn it into
-- | a `Right`, if the value is a `Nothing` use the provided default as a `Left`
-- |
Expand Down
27 changes: 27 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Test.Main where

import Prelude

import Data.Either (Either(..), lefts, rights)
import Data.Either.Inject (inj, prj)
import Data.Either.Nested (Either3, in1, in2, in3)
import Data.Maybe (Maybe(..))
Expand Down Expand Up @@ -64,3 +65,29 @@ main = do
{ actual: prj (in3 100 :: MySum)
, expected: Nothing :: Maybe Boolean
}
log "Test lefts"
assertEqual
{ actual: lefts [Left 1, Right 2, Left 3, Right 4]
, expected: [1, 3]
}
assertEqual
{ actual: lefts [Right 2, Right 4]
, expected: [] :: Array Int
}
assertEqual
{ actual: lefts []
, expected: [] :: Array Int
}
log "Test rights"
assertEqual
{ actual: rights [Left 1, Right 2, Left 3, Right 4]
, expected: [2, 4]
}
assertEqual
{ actual: rights [Left 1, Left 3]
, expected: [] :: Array Int
}
assertEqual
{ actual: rights []
, expected: [] :: Array Int
}