Skip to content

Added several methods #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
],
"dependencies": {
"purescript-tuples": "^5.0.0",
"purescript-gen": "^2.0.0"
"purescript-gen": "^2.0.0",
"purescript-filterable": "^3.0.1",
"purescript-profunctor-lenses": "^4.0.0"
}
}
47 changes: 44 additions & 3 deletions src/Data/These.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ module Data.These where
import Prelude

import Control.Extend (class Extend)
import Data.Bifunctor (class Bifunctor)
import Data.Bifunctor (class Bifunctor, bimap)
import Data.Bitraversable (class Bitraversable, class Bifoldable, bitraverse)
import Data.Either (Either(..))
import Data.Filterable (class Filterable, filterMap)
import Data.Functor.Invariant (class Invariant, imapF)
import Data.Maybe (Maybe(..))
import Data.Lens (Prism, prism)
import Data.Maybe (Maybe(..), isJust)
import Data.Traversable (class Traversable, class Foldable, foldMap, foldl, foldr)
import Data.Tuple (Tuple(..))
import Data.Tuple (Tuple(..), uncurry)

data These a b
= This a
Expand Down Expand Up @@ -147,3 +150,41 @@ that :: forall a b. These a b -> Maybe b
that = case _ of
That x -> Just x
_ -> Nothing

both :: forall a b. These a b -> Maybe (Tuple a b)
both = case _ of
Both a x -> Just (Tuple a x)
_ -> Nothing

mergeThese :: forall a. (a -> a -> a) -> These a a -> a
mergeThese = these identity identity

mergeTheseWith :: forall a b c. (a -> c) -> (b -> c) -> (c -> c -> c) -> These a b -> c
mergeTheseWith f g op t = mergeThese op $ bimap f g t

isThis :: forall a b. These a b -> Boolean
isThis = isJust <<< this

isThat :: forall a b. These a b -> Boolean
isThat = isJust <<< that

isBoth :: forall a b. These a b -> Boolean
isBoth = isJust <<< both

catThis :: forall t a b. Filterable t => Functor t => t (These a b) -> t a
catThis = filterMap this

catThat :: forall t a b. Filterable t => Functor t => t (These a b) -> t b
catThat = filterMap that

catThese :: forall t a b. Filterable t => Functor t => t (These a b) -> t (Tuple a b)
catThese = filterMap both

_This :: forall a b. Prism (These a b) (These a b) a a
_This = prism This (these Right (Left <<< That) (\x y -> Left $ Both x y))

_That :: forall a b. Prism (These a b) (These a b) b b
_That = prism That (these (Left <<< This) Right (\x y -> Left $ Both x y))

_Both :: forall a b. Prism (These a b) (These a b) (Tuple a b) (Tuple a b)
_Both = prism (uncurry Both) (these (Left <<< This) (Left <<< That) (\x y -> Right (Tuple x y)))