Skip to content

Commit c8a4c71

Browse files
authored
Merge pull request #36 from matthewleon/lazy-note
note': lazy note
2 parents 5b2c7af + c1e8c0f commit c8a4c71

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/Data/Either.purs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Data.Bitraversable (class Bitraversable)
1010
import Data.Eq (class Eq1)
1111
import Data.Foldable (class Foldable)
1212
import Data.Functor.Invariant (class Invariant, imapF)
13-
import Data.Maybe (Maybe(..), maybe)
13+
import Data.Maybe (Maybe(..), maybe, maybe')
1414
import Data.Monoid (mempty)
1515
import Data.Ord (class Ord1)
1616
import Data.Traversable (class Traversable)
@@ -262,6 +262,16 @@ fromRight (Right a) = a
262262
note :: forall a b. a -> Maybe b -> Either a b
263263
note a = maybe (Left a) Right
264264

265+
-- | Similar to `note`, but for use in cases where the default value may be
266+
-- | expensive to compute.
267+
-- |
268+
-- | ```purescript
269+
-- | note' (\_ -> "default") Nothing = Left "default"
270+
-- | note' (\_ -> "default") (Just 1) = Right 1
271+
-- | ```
272+
note' :: forall a b. (Unit -> a) -> Maybe b -> Either a b
273+
note' f = maybe' (Left <<< f) Right
274+
265275
-- | Turns an `Either` into a `Maybe`, by throwing eventual `Left` values away and converting
266276
-- | them into `Nothing`. `Right` values get turned into `Just`s.
267277
-- |

0 commit comments

Comments
 (0)