diff --git a/bower.json b/bower.json index cdefae5..46ada46 100644 --- a/bower.json +++ b/bower.json @@ -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" } } diff --git a/src/Data/These.purs b/src/Data/These.purs index e7aa2f7..7fe45fc 100644 --- a/src/Data/These.purs +++ b/src/Data/These.purs @@ -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 @@ -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))) \ No newline at end of file