Skip to content

note': lazy note #36

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
Apr 15, 2018
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
12 changes: 11 additions & 1 deletion src/Data/Either.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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.Maybe (Maybe(..), maybe, maybe')
import Data.Monoid (mempty)
import Data.Ord (class Ord1)
import Data.Traversable (class Traversable)
Expand Down Expand Up @@ -262,6 +262,16 @@ fromRight (Right a) = a
note :: forall a b. a -> Maybe b -> Either a b
note a = maybe (Left a) Right

-- | Similar to `note`, but for use in cases where the default value may be
-- | expensive to compute.
-- |
-- | ```purescript
-- | note' (\_ -> "default") Nothing = Left "default"
-- | note' (\_ -> "default") (Just 1) = Right 1
-- | ```
note' :: forall a b. (Unit -> a) -> Maybe b -> Either a b
note' f = maybe' (Left <<< f) 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.
-- |
Expand Down