Skip to content
Merged
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
10 changes: 5 additions & 5 deletions src/Data/Functor/Coproduct/Inject.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class Inject f g where
inj :: forall a. f a -> g a
prj :: forall a. g a -> Maybe (f a)

instance injectLeft :: Inject f (Coproduct f g) where
instance injectReflexive :: Inject f f where
inj = identity
prj = Just

else instance injectLeft :: Inject f (Coproduct f g) where
inj = Coproduct <<< Left
prj = coproduct Just (const Nothing)

else instance injectRight :: Inject f g => Inject f (Coproduct h g) where
inj = Coproduct <<< Right <<< inj
prj = coproduct (const Nothing) prj

else instance injectReflexive :: Inject f f where
inj = identity
prj = Just
23 changes: 23 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ module Test.Main where

import Prelude

import Data.Const (Const)
import Data.Functor.Compose (Compose(..))
import Data.Functor.Coproduct (Coproduct, left, right)
import Data.Functor.Coproduct.Inject (inj)
import Data.Identity (Identity(..))
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Test.Assert (assertEqual)

main :: Effect Unit
main = do
testComposeOrdering
testInjections

testComposeOrdering :: Effect Unit
testComposeOrdering = do
assertEqual
{ expected: Compose (Identity (Just true))
, actual: Compose (Identity (Just true))
Expand All @@ -18,3 +26,18 @@ main = do
{ expected: Compose (Identity (Just true)) > Compose (Identity (Just false))
, actual: true
}

testInjections :: Effect Unit
testInjections = do
assertEqual
{ expected: Identity unit
, actual: inj (Identity unit)
}
assertEqual
{ expected: left (Identity unit) :: Coproduct Identity (Const Void) Unit
, actual: inj (Identity unit)
}
assertEqual
{ expected: right (Identity unit) :: Coproduct (Const Void) Identity Unit
, actual: inj (Identity unit)
}