@@ -4,13 +4,13 @@ import Prelude
44
55import Control.Alt (class Alt , (<|>))
66import Control.Extend (class Extend )
7-
87import Data.Bifoldable (class Bifoldable )
98import Data.Bifunctor (class Bifunctor )
109import Data.Bitraversable (class Bitraversable )
1110import Data.Eq (class Eq1 )
1211import Data.Foldable (class Foldable )
1312import Data.Functor.Invariant (class Invariant , imapF )
13+ import Data.Maybe (Maybe (..), maybe )
1414import Data.Monoid (mempty )
1515import Data.Ord (class Ord1 )
1616import Data.Traversable (class Traversable )
@@ -251,3 +251,23 @@ fromLeft (Left a) = a
251251-- | Passing a `Left` to `fromRight` will throw an error at runtime.
252252fromRight :: forall a b . Partial => Either a b -> b
253253fromRight (Right a) = a
254+
255+ -- | Takes a default and a `Maybe` value, if the value is a `Just`, turn it into
256+ -- | a `Right`, if the value is a `Nothing` use the provided default as a `Left`
257+ -- |
258+ -- | ```purescript
259+ -- | note "default" Nothing = Left "default"
260+ -- | note "default" (Just 1) = Right 1
261+ -- | ```
262+ note :: forall a b . a -> Maybe b -> Either a b
263+ note a = maybe (Left a) Right
264+
265+ -- | Turns an `Either` into a `Maybe`, by throwing eventual `Left` values away and converting
266+ -- | them into `Nothing`. `Right` values get turned into `Just`s.
267+ -- |
268+ -- | ```purescript
269+ -- | hush (Left "ParseError") = Nothing
270+ -- | hush (Right 42) = Just 42
271+ -- | ```
272+ hush :: forall a b . Either a b -> Maybe b
273+ hush = either (const Nothing ) Just
0 commit comments