Skip to content

Commit 8353b58

Browse files
committed
Add Split
1 parent 9f1fc8b commit 8353b58

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"purescript-contravariant": "^3.0.0",
2626
"purescript-distributive": "^3.0.0",
2727
"purescript-either": "^3.0.0",
28+
"purescript-exists": "^3.0.0",
2829
"purescript-tuples": "^4.0.0"
2930
}
3031
}

src/Data/Profunctor/Split.purs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module Data.Profunctor.Split where
2+
3+
import Prelude
4+
5+
import Data.Exists (Exists, mkExists, runExists)
6+
import Data.Functor.Invariant (class Invariant, imap)
7+
import Data.Profunctor (class Profunctor)
8+
9+
newtype Split f a b = Split (Exists (SplitF f a b))
10+
11+
data SplitF f a b x = SplitF (a -> x) (x -> b) (f x)
12+
13+
instance profunctorSplit :: Profunctor (Split f) where
14+
dimap f g = unSplit \h i -> split (h <<< f) (g <<< i)
15+
16+
split :: forall f a b x. (a -> x) -> (x -> b) -> f x -> Split f a b
17+
split f g fx = Split (mkExists (SplitF f g fx))
18+
19+
unSplit :: forall f a b r. (forall x. (a -> x) -> (x -> b) -> f x -> r) -> Split f a b -> r
20+
unSplit f (Split e) = runExists (\(SplitF g h fx) -> f g h fx) e
21+
22+
liftSplit :: forall f a. f a -> Split f a a
23+
liftSplit = split id id
24+
25+
lowerSplit :: forall f a. Invariant f => Split f a a -> f a
26+
lowerSplit = unSplit (flip imap)
27+
28+
hoistSplit :: forall f g a b. (f ~> g) -> Split f a b -> Split g a b
29+
hoistSplit nat = unSplit (\f g -> split f g <<< nat)

0 commit comments

Comments
 (0)