We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8178535 commit 7a588eaCopy full SHA for 7a588ea
src/Data/Functor/Coproduct/Inject.purs
@@ -0,0 +1,23 @@
1
+module Data.Functor.Coproduct.Inject where
2
+
3
+import Prelude
4
5
+import Data.Either (Either(..))
6
+import Data.Functor.Coproduct (Coproduct(..), coproduct)
7
+import Data.Maybe (Maybe(..))
8
9
+class Inject f g where
10
+ inj :: forall a. f a -> g a
11
+ prj :: forall a. g a -> Maybe (f a)
12
13
+instance injectLeft :: Inject f g => Inject f (Coproduct g h) where
14
+ inj = Coproduct <<< Left <<< inj
15
+ prj = coproduct prj (const Nothing)
16
17
+else instance injectRight :: Inject f g => Inject f (Coproduct h g) where
18
+ inj = Coproduct <<< Right <<< inj
19
+ prj = coproduct (const Nothing) prj
20
21
+else instance injectReflexive :: Inject f f where
22
+ inj = identity
23
+ prj = Just
0 commit comments