Skip to content

Commit 0036bf9

Browse files
authored
Merge pull request #31 from MonoidMusician/master
More Foldable/Traversable instances (Semigroup and WithIndex)
2 parents 8f87b7d + b8d7ada commit 0036bf9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/Data/Tuple.purs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ import Data.Bitraversable (class Bitraversable)
1414
import Data.Distributive (class Distributive, collectDefault)
1515
import Data.Eq (class Eq1)
1616
import Data.Foldable (class Foldable, foldMap)
17+
import Data.FoldableWithIndex (class FoldableWithIndex)
1718
import Data.Functor.Invariant (class Invariant, imapF)
19+
import Data.FunctorWithIndex (class FunctorWithIndex)
1820
import Data.HeytingAlgebra (implies, ff, tt)
1921
import Data.Maybe (Maybe(..))
2022
import Data.Maybe.First (First(..))
2123
import Data.Newtype (unwrap)
2224
import Data.Ord (class Ord1)
25+
import Data.Semigroup.Foldable (class Foldable1)
26+
import Data.Semigroup.Traversable (class Traversable1)
2327
import Data.Traversable (class Traversable)
28+
import Data.TraversableWithIndex (class TraversableWithIndex)
2429
import Type.Equality (class TypeEquals, from)
2530

2631
-- | A simple product type for wrapping a pair of component values.
@@ -93,6 +98,9 @@ instance booleanAlgebraTuple :: (BooleanAlgebra a, BooleanAlgebra b) => BooleanA
9398
-- | ````
9499
derive instance functorTuple :: Functor (Tuple a)
95100

101+
instance functorWithIndexTuple :: FunctorWithIndex Unit (Tuple a) where
102+
mapWithIndex f = map $ f unit
103+
96104
instance invariantTuple :: Invariant (Tuple a) where
97105
imap = imapF
98106

@@ -137,6 +145,15 @@ instance foldableTuple :: Foldable (Tuple a) where
137145
foldl f z (Tuple _ x) = f z x
138146
foldMap f (Tuple _ x) = f x
139147

148+
instance foldable1Tuple :: Foldable1 (Tuple a) where
149+
foldMap1 f (Tuple _ x) = f x
150+
fold1 (Tuple _ x) = x
151+
152+
instance foldableWithIndexTuple :: FoldableWithIndex Unit (Tuple a) where
153+
foldrWithIndex f z (Tuple _ x) = f unit x z
154+
foldlWithIndex f z (Tuple _ x) = f unit z x
155+
foldMapWithIndex f (Tuple _ x) = f unit x
156+
140157
instance bifoldableTuple :: Bifoldable Tuple where
141158
bifoldMap f g (Tuple a b) = f a <> g b
142159
bifoldr f g z (Tuple a b) = f a (g b z)
@@ -146,6 +163,13 @@ instance traversableTuple :: Traversable (Tuple a) where
146163
traverse f (Tuple x y) = Tuple x <$> f y
147164
sequence (Tuple x y) = Tuple x <$> y
148165

166+
instance traversable1Tuple :: Traversable1 (Tuple a) where
167+
traverse1 f (Tuple x y) = Tuple x <$> f y
168+
sequence1 (Tuple x y) = Tuple x <$> y
169+
170+
instance traversableWithIndexTuple :: TraversableWithIndex Unit (Tuple a) where
171+
traverseWithIndex f (Tuple x y) = Tuple x <$> f unit y
172+
149173
instance bitraversableTuple :: Bitraversable Tuple where
150174
bitraverse f g (Tuple a b) = Tuple <$> f a <*> g b
151175
bisequence (Tuple a b) = Tuple <$> a <*> b

0 commit comments

Comments
 (0)