Skip to content

Commit f126106

Browse files
authored
Merge pull request #17 from purescript-contrib/additions
A few additions
2 parents 9c53006 + a9e8b5e commit f126106

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"package.json"
1818
],
1919
"dependencies": {
20-
"purescript-tuples": "^4.0.0"
20+
"purescript-tuples": "^4.0.0",
21+
"purescript-gen": "^1.3.1"
2122
}
2223
}

src/Data/These.purs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module Data.These where
33
import Prelude
44

55
import Control.Extend (class Extend)
6-
76
import Data.Bifunctor (class Bifunctor)
87
import Data.Bitraversable (class Bitraversable, class Bifoldable, bitraverse)
98
import Data.Functor.Invariant (class Invariant, imapF)
@@ -112,17 +111,39 @@ thatOrBoth :: forall a b. b -> Maybe a -> These a b
112111
thatOrBoth b Nothing = That b
113112
thatOrBoth b (Just a) = Both a b
114113

114+
-- | Takes a pair of `Maybe`s and attempts to create a `These` from them.
115+
maybeThese :: forall a b. Maybe a -> Maybe b -> Maybe (These a b)
116+
maybeThese = case _, _ of
117+
Just a, Nothing -> Just (This a)
118+
Nothing, Just b -> Just (That b)
119+
Just a, Just b -> Just (Both a b)
120+
Nothing, Nothing -> Nothing
121+
115122
fromThese :: forall a b. a -> b -> These a b -> Tuple a b
116123
fromThese _ x (This a) = Tuple a x
117124
fromThese a _ (That x) = Tuple a x
118125
fromThese _ _ (Both a x) = Tuple a x
119126

127+
-- | Returns an `a` value if possible.
120128
theseLeft :: forall a b. These a b -> Maybe a
121129
theseLeft (Both x _) = Just x
122130
theseLeft (This x) = Just x
123131
theseLeft _ = Nothing
124132

133+
-- | Returns a `b` value if possible.
125134
theseRight :: forall a b. These a b -> Maybe b
126135
theseRight (Both _ x) = Just x
127136
theseRight (That x) = Just x
128137
theseRight _ = Nothing
138+
139+
-- | Returns the `a` value if and only if the value is constructed with `This`.
140+
this :: forall a b. These a b -> Maybe a
141+
this = case _ of
142+
This x -> Just x
143+
_ -> Nothing
144+
145+
-- | Returns the `b` value if and only if the value is constructed with `That`.
146+
that :: forall a b. These a b -> Maybe b
147+
that = case _ of
148+
That x -> Just x
149+
_ -> Nothing

src/Data/These/Gen.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Data.These.Gen where
2+
3+
import Prelude
4+
5+
import Control.Monad.Gen (class MonadGen, filtered)
6+
import Control.Monad.Gen.Common (genMaybe)
7+
import Control.Monad.Rec.Class (class MonadRec)
8+
import Data.These (These, maybeThese)
9+
10+
genThese forall m a b. MonadGen m => MonadRec m => m a -> m b -> m (These a b)
11+
genThese ga gb = filtered (maybeThese <$> genMaybe ga <*> genMaybe gb)

0 commit comments

Comments
 (0)