Skip to content

Adds note and hush #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2017
Merged
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
22 changes: 21 additions & 1 deletion src/Data/Either.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import Prelude

import Control.Alt (class Alt, (<|>))
import Control.Extend (class Extend)

import Data.Bifoldable (class Bifoldable)
import Data.Bifunctor (class Bifunctor)
import Data.Bitraversable (class Bitraversable)
import Data.Eq (class Eq1)
import Data.Foldable (class Foldable)
import Data.Functor.Invariant (class Invariant, imapF)
import Data.Maybe (Maybe(..), maybe)
import Data.Monoid (mempty)
import Data.Ord (class Ord1)
import Data.Traversable (class Traversable)
Expand Down Expand Up @@ -251,3 +251,23 @@ fromLeft (Left a) = a
-- | Passing a `Left` to `fromRight` will throw an error at runtime.
fromRight :: forall a b. Partial => Either a b -> b
fromRight (Right a) = a

-- | 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`
-- |
-- | ```purescript
-- | note "default" Nothing = Left "default"
-- | note "default" (Just 1) = Right 1
-- | ```
note :: forall a b. a -> Maybe b -> Either a b
note a = maybe (Left a) Right

-- | Turns an `Either` into a `Maybe`, by throwing eventual `Left` values away and converting
-- | them into `Nothing`. `Right` values get turned into `Just`s.
-- |
-- | ```purescript
-- | hush (Left "ParseError") = Nothing
-- | hush (Right 42) = Just 42
-- | ```
hush :: forall a b. Either a b -> Maybe b
hush = either (const Nothing) Just