Skip to content

Add lazy versions of fromRight and fromLeft #59

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
Dec 5, 2020
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
18 changes: 18 additions & 0 deletions src/Data/Either.purs
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,31 @@ fromLeft :: forall a b. a -> Either a b -> a
fromLeft _ (Left a) = a
fromLeft default _ = default

-- | Similar to `fromLeft` but for use in cases where the default value may be
-- | expensive to compute. As PureScript is not lazy, the standard `fromLeft`
-- | has to evaluate the default value before returning the result,
-- | whereas here the value is only computed when the `Either` is known
-- | to be `Right`.
fromLeft' :: forall a b. (Unit -> a) -> Either a b -> a
fromLeft' _ (Left a) = a
fromLeft' default _ = default unit

-- | A function that extracts the value from the `Right` data constructor.
-- | The first argument is a default value, which will be returned in the
-- | case where a `Left` is passed to `fromRight`.
fromRight :: forall a b. b -> Either a b -> b
fromRight _ (Right b) = b
fromRight default _ = default

-- | Similar to `fromRight` but for use in cases where the default value may be
-- | expensive to compute. As PureScript is not lazy, the standard `fromRight`
-- | has to evaluate the default value before returning the result,
-- | whereas here the value is only computed when the `Either` is known
-- | to be `Left`.
fromRight' :: forall a b. (Unit -> b) -> Either a b -> b
fromRight' _ (Right b) = b
fromRight' default _ = default unit

-- | 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