@@ -4,13 +4,13 @@ import Prelude
4
4
5
5
import Control.Alt (class Alt , (<|>))
6
6
import Control.Extend (class Extend )
7
-
8
7
import Data.Bifoldable (class Bifoldable )
9
8
import Data.Bifunctor (class Bifunctor )
10
9
import Data.Bitraversable (class Bitraversable )
11
10
import Data.Eq (class Eq1 )
12
11
import Data.Foldable (class Foldable )
13
12
import Data.Functor.Invariant (class Invariant , imapF )
13
+ import Data.Maybe (Maybe (..), maybe )
14
14
import Data.Monoid (mempty )
15
15
import Data.Ord (class Ord1 )
16
16
import Data.Traversable (class Traversable )
@@ -251,3 +251,23 @@ fromLeft (Left a) = a
251
251
-- | Passing a `Left` to `fromRight` will throw an error at runtime.
252
252
fromRight :: forall a b . Partial => Either a b -> b
253
253
fromRight (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