Skip to content

Add Comonad, Extract, Monoid, Semigroup, and Semigroupoid instances. #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 8, 2014
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,30 @@

instance bindTuple :: (Semigroup a) => Bind (Tuple a)

instance comonadTuple :: Comonad (Tuple a)

instance eqTuple :: (Eq a, Eq b) => Eq (Tuple a b)

instance extendTuple :: Extend (Tuple a)

instance functorTuple :: Functor (Tuple a)

instance lazyLazy1Tuple :: (Lazy1 l1, Lazy1 l2) => Lazy (Tuple (l1 a) (l2 b))

instance lazyLazy2Tuple :: (Lazy2 l1, Lazy2 l2) => Lazy (Tuple (l1 a b) (l2 c d))

instance lazyTuple :: (Lazy a, Lazy b) => Lazy (Tuple a b)

instance monadTuple :: (Monoid a) => Monad (Tuple a)

instance monoidTuple :: (Monoid a, Monoid b) => Monoid (Tuple a b)

instance ordTuple :: (Ord a, Ord b) => Ord (Tuple a b)

instance semigroupTuple :: (Semigroup a, Semigroup b) => Semigroup (Tuple a b)

instance semigroupoidTuple :: Semigroupoid Tuple

instance showTuple :: (Show a, Show b) => Show (Tuple a b)


Expand Down
20 changes: 19 additions & 1 deletion src/Data/Tuple.purs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module Data.Tuple where

import Control.Comonad
import Control.Extend
import Control.Lazy

import Data.Array
import Data.Monoid
import Control.Lazy

data Tuple a b = Tuple a b

Expand All @@ -18,6 +21,15 @@ instance ordTuple :: (Ord a, Ord b) => Ord (Tuple a b) where
EQ -> compare b1 b2
other -> other

instance semigroupoidTuple :: Semigroupoid Tuple where
(<<<) (Tuple _ c) (Tuple a _) = Tuple a c

instance semigroupTuple :: (Semigroup a, Semigroup b) => Semigroup (Tuple a b) where
(<>) (Tuple a1 b1) (Tuple a2 b2) = Tuple (a1 <> a2) (b1 <> b2)

instance monoidTuple :: (Monoid a, Monoid b) => Monoid (Tuple a b) where
mempty = Tuple mempty mempty

instance functorTuple :: Functor (Tuple a) where
(<$>) f (Tuple x y) = Tuple x (f y)

Expand All @@ -33,6 +45,12 @@ instance bindTuple :: (Semigroup a) => Bind (Tuple a) where

instance monadTuple :: (Monoid a) => Monad (Tuple a)

instance extendTuple :: Extend (Tuple a) where
(<<=) f t@(Tuple a b) = Tuple a (f t)

instance comonadTuple :: Comonad (Tuple a) where
extract = snd

instance lazyTuple :: (Lazy a, Lazy b) => Lazy (Tuple a b) where
defer f = Tuple (defer $ \_ -> fst (f unit)) (defer $ \_ -> snd (f unit))

Expand Down