Skip to content

Commit ceb6a14

Browse files
authored
Merge pull request #32 from kRITZCREEK/note-and-hush
Adds note and hush
2 parents 7343fd4 + 8c6ca6e commit ceb6a14

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/Data/Either.purs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import Prelude
44

55
import Control.Alt (class Alt, (<|>))
66
import Control.Extend (class Extend)
7-
87
import Data.Bifoldable (class Bifoldable)
98
import Data.Bifunctor (class Bifunctor)
109
import Data.Bitraversable (class Bitraversable)
1110
import Data.Eq (class Eq1)
1211
import Data.Foldable (class Foldable)
1312
import Data.Functor.Invariant (class Invariant, imapF)
13+
import Data.Maybe (Maybe(..), maybe)
1414
import Data.Monoid (mempty)
1515
import Data.Ord (class Ord1)
1616
import Data.Traversable (class Traversable)
@@ -251,3 +251,23 @@ fromLeft (Left a) = a
251251
-- | Passing a `Left` to `fromRight` will throw an error at runtime.
252252
fromRight :: forall a b. Partial => Either a b -> b
253253
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

Comments
 (0)