Skip to content

Commit 6b549b3

Browse files
authored
Merge pull request #23 from matthewleon/foldable-instances
foldable instances
2 parents 0c14b63 + ad0cd41 commit 6b549b3

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"package.json"
2222
],
2323
"dependencies": {
24-
"purescript-monoid": "^3.0.0"
24+
"purescript-monoid": "^3.0.0",
25+
"purescript-foldable-traversable": "^3.7.1"
2526
}
2627
}

src/Data/Lazy.purs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ import Prelude
55
import Control.Comonad (class Comonad)
66
import Control.Extend (class Extend)
77
import Control.Lazy as CL
8-
8+
import Data.Foldable (class Foldable, foldMap, foldl, foldr)
9+
import Data.FoldableWithIndex (class FoldableWithIndex)
910
import Data.Functor.Invariant (class Invariant, imapF)
11+
import Data.FunctorWithIndex (class FunctorWithIndex)
1012
import Data.HeytingAlgebra (implies, ff, tt)
1113
import Data.Monoid (class Monoid, mempty)
14+
import Data.Semigroup.Foldable (class Foldable1, fold1Default)
15+
import Data.Semigroup.Traversable (class Traversable1)
16+
import Data.Traversable (class Traversable, traverse)
17+
import Data.TraversableWithIndex (class TraversableWithIndex)
1218

1319
-- | `Lazy a` represents lazily-computed values of type `a`.
1420
-- |
@@ -75,6 +81,34 @@ instance booleanAlgebraLazy :: BooleanAlgebra a => BooleanAlgebra (Lazy a)
7581
instance functorLazy :: Functor Lazy where
7682
map f l = defer \_ -> f (force l)
7783

84+
instance functorWithIndexLazy :: FunctorWithIndex Unit Lazy where
85+
mapWithIndex f = map $ f unit
86+
87+
instance foldableLazy :: Foldable Lazy where
88+
foldr f z l = f (force l) z
89+
foldl f z l = f z (force l)
90+
foldMap f l = f (force l)
91+
92+
instance foldableWithIndexLazy :: FoldableWithIndex Unit Lazy where
93+
foldrWithIndex f = foldr $ f unit
94+
foldlWithIndex f = foldl $ f unit
95+
foldMapWithIndex f = foldMap $ f unit
96+
97+
instance foldable1Lazy :: Foldable1 Lazy where
98+
foldMap1 f l = f (force l)
99+
fold1 = fold1Default
100+
101+
instance traversableLazy :: Traversable Lazy where
102+
traverse f l = defer <<< const <$> f (force l)
103+
sequence l = defer <<< const <$> force l
104+
105+
instance traversableWithIndexLazy :: TraversableWithIndex Unit Lazy where
106+
traverseWithIndex f = traverse $ f unit
107+
108+
instance traversable1Lazy :: Traversable1 Lazy where
109+
traverse1 f l = defer <<< const <$> f (force l)
110+
sequence1 l = defer <<< const <$> force l
111+
78112
instance invariantLazy :: Invariant Lazy where
79113
imap = imapF
80114

0 commit comments

Comments
 (0)