Skip to content

Refactor functors and related packages #46

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
Feb 3, 2021
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ Notable changes to this project are documented in this file. The format is based

Breaking changes:
- Added support for PureScript 0.14 and dropped support for all previous versions (#37, #43)
- `Data.Tuple.lookup` has been moved to `Data.Foldable.lookup` in the `purescript-foldable-traversable` package (#46)

New features:
- Added Generic instance for Tuple (#40)
- This package no longer depends on the `purescript-bifunctors`, `purescript-distributive`, `purescript-foldable-traversable`, `purescript-maybe`, `purescript-newtype`, and `purescript-type-equality` packages. Relevant instances have been moved to those packages. (#46)

Bugfixes:

Expand Down
8 changes: 1 addition & 7 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@
"package.json"
],
"dependencies": {
"purescript-bifunctors": "master",
"purescript-control": "master",
"purescript-distributive": "master",
"purescript-foldable-traversable": "master",
"purescript-invariant": "master",
"purescript-maybe": "master",
"purescript-newtype": "master",
"purescript-prelude": "master",
"purescript-type-equality": "master"
"purescript-prelude": "master"
}
}
72 changes: 0 additions & 72 deletions src/Data/Tuple.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,14 @@ module Data.Tuple where

import Prelude

import Control.Biapplicative (class Biapplicative)
import Control.Biapply (class Biapply)
import Control.Comonad (class Comonad)
import Control.Extend (class Extend)
import Control.Lazy (class Lazy, defer)
import Data.Bifoldable (class Bifoldable)
import Data.Bifunctor (class Bifunctor)
import Data.Bitraversable (class Bitraversable)
import Data.Distributive (class Distributive, collectDefault)
import Data.Eq (class Eq1)
import Data.Foldable (class Foldable, foldMap)
import Data.FoldableWithIndex (class FoldableWithIndex)
import Data.Functor.Invariant (class Invariant, imapF)
import Data.FunctorWithIndex (class FunctorWithIndex)
import Data.Generic.Rep (class Generic)
import Data.HeytingAlgebra (implies, ff, tt)
import Data.Maybe (Maybe(..))
import Data.Maybe.First (First(..))
import Data.Newtype (unwrap)
import Data.Ord (class Ord1)
import Data.Semigroup.Foldable (class Foldable1)
import Data.Semigroup.Traversable (class Traversable1)
import Data.Traversable (class Traversable)
import Data.TraversableWithIndex (class TraversableWithIndex)
import Type.Equality (class TypeEquals, from)

-- | A simple product type for wrapping a pair of component values.
data Tuple a b = Tuple a b
Expand Down Expand Up @@ -99,17 +82,11 @@ instance booleanAlgebraTuple :: (BooleanAlgebra a, BooleanAlgebra b) => BooleanA
-- | ````
derive instance functorTuple :: Functor (Tuple a)

instance functorWithIndexTuple :: FunctorWithIndex Unit (Tuple a) where
mapWithIndex f = map $ f unit

derive instance genericTuple :: Generic (Tuple a b) _

instance invariantTuple :: Invariant (Tuple a) where
imap = imapF

instance bifunctorTuple :: Bifunctor Tuple where
bimap f g (Tuple x y) = Tuple (f x) (g y)

-- | The `Apply` instance allows functions to transform the contents of a
-- | `Tuple` with the `<*>` operator whenever there is a `Semigroup` instance
-- | for the `fst` component, so:
Expand All @@ -119,15 +96,9 @@ instance bifunctorTuple :: Bifunctor Tuple where
instance applyTuple :: (Semigroup a) => Apply (Tuple a) where
apply (Tuple a1 f) (Tuple a2 x) = Tuple (a1 <> a2) (f x)

instance biapplyTuple :: Biapply Tuple where
biapply (Tuple f g) (Tuple a b) = Tuple (f a) (g b)

instance applicativeTuple :: (Monoid a) => Applicative (Tuple a) where
pure = Tuple mempty

instance biapplicativeTuple :: Biapplicative Tuple where
bipure = Tuple

instance bindTuple :: (Semigroup a) => Bind (Tuple a) where
bind (Tuple a1 b) f = case f b of
Tuple a2 c -> Tuple (a1 <> a2) c
Expand All @@ -143,45 +114,6 @@ instance comonadTuple :: Comonad (Tuple a) where
instance lazyTuple :: (Lazy a, Lazy b) => Lazy (Tuple a b) where
defer f = Tuple (defer $ \_ -> fst (f unit)) (defer $ \_ -> snd (f unit))

instance foldableTuple :: Foldable (Tuple a) where
foldr f z (Tuple _ x) = f x z
foldl f z (Tuple _ x) = f z x
foldMap f (Tuple _ x) = f x

instance foldable1Tuple :: Foldable1 (Tuple a) where
foldMap1 f (Tuple _ x) = f x
foldr1 _ (Tuple _ x) = x
foldl1 _ (Tuple _ x) = x

instance foldableWithIndexTuple :: FoldableWithIndex Unit (Tuple a) where
foldrWithIndex f z (Tuple _ x) = f unit x z
foldlWithIndex f z (Tuple _ x) = f unit z x
foldMapWithIndex f (Tuple _ x) = f unit x

instance bifoldableTuple :: Bifoldable Tuple where
bifoldMap f g (Tuple a b) = f a <> g b
bifoldr f g z (Tuple a b) = f a (g b z)
bifoldl f g z (Tuple a b) = g (f z a) b

instance traversableTuple :: Traversable (Tuple a) where
traverse f (Tuple x y) = Tuple x <$> f y
sequence (Tuple x y) = Tuple x <$> y

instance traversable1Tuple :: Traversable1 (Tuple a) where
traverse1 f (Tuple x y) = Tuple x <$> f y
sequence1 (Tuple x y) = Tuple x <$> y

instance traversableWithIndexTuple :: TraversableWithIndex Unit (Tuple a) where
traverseWithIndex f (Tuple x y) = Tuple x <$> f unit y

instance bitraversableTuple :: Bitraversable Tuple where
bitraverse f g (Tuple a b) = Tuple <$> f a <*> g b
bisequence (Tuple a b) = Tuple <$> a <*> b

instance distributiveTuple :: TypeEquals a Unit => Distributive (Tuple a) where
collect = collectDefault
distribute = Tuple (from unit) <<< map snd

-- | Returns the first component of a tuple.
fst :: forall a b. Tuple a b -> a
fst (Tuple a _) = a
Expand All @@ -201,7 +133,3 @@ uncurry f (Tuple a b) = f a b
-- | Exchange the first and second components of a tuple.
swap :: forall a b. Tuple a b -> Tuple b a
swap (Tuple a b) = Tuple b a

-- | Lookup a value in a data structure of `Tuple`s, generalizing association lists.
lookup :: forall a b f. Foldable f => Eq a => a -> f (Tuple a b) -> Maybe b
lookup a = unwrap <<< foldMap \(Tuple a' b) -> First (if a == a' then Just b else Nothing)