Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Implement with instance chains #10

Closed
wants to merge 1 commit 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
41 changes: 0 additions & 41 deletions src/Data/Inject.purs

This file was deleted.

23 changes: 23 additions & 0 deletions src/Data/Inject/Coproduct.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Data.Inject.Either where

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be Data.Inject.Coproduct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh yes 😉

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this change is required to compile - it collides with the other Data.Inject.Either module in this project.


import Prelude

import Data.Either (Either(..))
import Data.Functor.Coproduct (Coproduct(..), coproduct)
import Data.Maybe (Maybe(..))

class Inject f g where
inj :: forall a. f a -> g a
prj :: forall a. g a -> Maybe (f a)

instance injectLeft :: Inject f g => Inject f (Coproduct g h) where
inj = Coproduct <<< Left <<< inj
prj = coproduct prj (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 = id
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id => identity

prj = Just
22 changes: 22 additions & 0 deletions src/Data/Inject/Either.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Data.Inject.Either where

import Prelude

import Data.Either (Either(..), either)
import Data.Maybe (Maybe(..))

class Inject a b where
inj :: a -> b
prj :: b -> Maybe a

instance injectLeft :: Inject a b => Inject a (Either b c) where
inj = Left <<< inj
prj = either prj (const Nothing)

else instance injectRight :: Inject a b => Inject a (Either c b) where
inj = Right <<< inj
prj = either (const Nothing) prj

else instance injectReflexive :: Inject a a where
inj = id
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id => identity

prj = Just