@@ -13,15 +13,29 @@ import Data.Function (on)
1313import Data.Int (toNumber )
1414import Data.Maybe (Maybe (..))
1515import Data.Monoid.Additive (Additive (..))
16+ import Data.Newtype (unwrap )
1617import Data.Traversable (class Traversable , sequenceDefault , traverse , sequence , traverseDefault )
18+ import Unsafe.Coerce (unsafeCoerce )
1719
1820import Math (abs )
1921
2022import Test.Assert (ASSERT , assert , assert' )
2123
2224foreign import arrayFrom1UpTo :: Int -> Array Int
25+ foreign import arrayReplicate :: forall a . Int -> a -> Array a
2326
24- main :: Eff (console :: CONSOLE , assert :: ASSERT ) Unit
27+ foreign import intPow :: Int -> Int -> Int
28+
29+ foldableLength :: forall f a . Foldable f => f a -> Int
30+ foldableLength = unwrap <<< foldMap (const (Additive 1 ))
31+
32+ -- Ensure that a value is evaluated 'lazily' by treating it as an Eff action.
33+ deferEff :: forall e a . (Unit -> a ) -> Eff e a
34+ deferEff = unsafeCoerce
35+
36+ type EffTest = Eff (console :: CONSOLE , assert :: ASSERT )
37+
38+ main :: EffTest Unit
2539main = do
2640 log " Test foldableArray instance"
2741 testFoldableArrayWith 20
@@ -107,8 +121,8 @@ main = do
107121 log " All done!"
108122
109123
110- testFoldableFWith :: forall f e . (Foldable f , Eq (f Int )) =>
111- (Int -> f Int ) -> Int -> Eff ( assert :: ASSERT | e ) Unit
124+ testFoldableFWith :: forall f . (Foldable f , Eq (f Int )) =>
125+ (Int -> f Int ) -> Int -> EffTest Unit
112126testFoldableFWith f n = do
113127 let dat = f n
114128 let expectedSum = (n / 2 ) * (n + 1 )
@@ -117,25 +131,33 @@ testFoldableFWith f n = do
117131 assert $ foldl (+) 0 dat == expectedSum
118132 assert $ foldMap Additive dat == Additive expectedSum
119133
120- testFoldableArrayWith :: forall eff . Int -> Eff ( assert :: ASSERT | eff ) Unit
134+ testFoldableArrayWith :: Int -> EffTest Unit
121135testFoldableArrayWith = testFoldableFWith arrayFrom1UpTo
122136
123137
124- testTraversableFWith :: forall f e . (Traversable f , Eq (f Int )) =>
125- (Int -> f Int ) -> Int -> Eff ( assert :: ASSERT | e ) Unit
138+ testTraversableFWith :: forall f . (Traversable f , Eq (f Int )) =>
139+ (Int -> f Int ) -> Int -> EffTest Unit
126140testTraversableFWith f n = do
127141 let dat = f n
142+ let len = foldableLength dat
128143
129144 assert' " traverse Just == Just" $ traverse Just dat == Just dat
130- assert' " traverse pure == pure" $ traverse pure dat == [dat]
145+ assert' " traverse pure == pure (Array)" $ traverse pure dat == [dat]
146+
147+ when (len <= 10 ) do
148+ result <- deferEff \_ -> traverse (\x -> [x,x]) dat == arrayReplicate (intPow 2 len) dat
149+ assert' " traverse with Array as underlying applicative" result
150+
131151 assert' " traverse (const Nothing) == const Nothing" $
132152 traverse (const Nothing :: Int -> Maybe Int ) dat == Nothing
153+
133154 assert' " sequence <<< map f == traverse f" $
134155 sequence (map Just dat) == traverse Just dat
156+
135157 assert' " underlying applicative" $
136158 (traverse pure dat :: Unit -> f Int ) unit == dat
137159
138- testTraversableArrayWith :: forall eff . Int -> Eff ( assert :: ASSERT | eff ) Unit
160+ testTraversableArrayWith :: Int -> EffTest Unit
139161testTraversableArrayWith = testTraversableFWith arrayFrom1UpTo
140162
141163
@@ -175,16 +197,16 @@ instance foldableDFR :: Foldable FoldrDefault where
175197 foldl f u (FRD a) = foldl f u a
176198 foldr f u = foldrDefault f u
177199
178- testFoldableFoldMapDefaultL :: forall eff . Int -> Eff ( assert :: ASSERT | eff ) Unit
200+ testFoldableFoldMapDefaultL :: Int -> EffTest Unit
179201testFoldableFoldMapDefaultL = testFoldableFWith (FML <<< arrayFrom1UpTo)
180202
181- testFoldableFoldMapDefaultR :: forall eff . Int -> Eff ( assert :: ASSERT | eff ) Unit
203+ testFoldableFoldMapDefaultR :: Int -> EffTest Unit
182204testFoldableFoldMapDefaultR = testFoldableFWith (FMR <<< arrayFrom1UpTo)
183205
184- testFoldableFoldlDefault :: forall eff . Int -> Eff ( assert :: ASSERT | eff ) Unit
206+ testFoldableFoldlDefault :: Int -> EffTest Unit
185207testFoldableFoldlDefault = testFoldableFWith (FLD <<< arrayFrom1UpTo)
186208
187- testFoldableFoldrDefault :: forall eff . Int -> Eff ( assert :: ASSERT | eff ) Unit
209+ testFoldableFoldrDefault :: Int -> EffTest Unit
188210testFoldableFoldrDefault = testFoldableFWith (FRD <<< arrayFrom1UpTo)
189211
190212
@@ -217,10 +239,10 @@ instance traversableSD :: Traversable SequenceDefault where
217239 traverse f (SD a) = map SD (traverse f a)
218240 sequence m = sequenceDefault m
219241
220- testTraverseDefault :: forall eff . Int -> Eff ( assert :: ASSERT | eff ) Unit
242+ testTraverseDefault :: Int -> EffTest Unit
221243testTraverseDefault = testTraversableFWith (TD <<< arrayFrom1UpTo)
222244
223- testSequenceDefault :: forall eff . Int -> Eff ( assert :: ASSERT | eff ) Unit
245+ testSequenceDefault :: Int -> EffTest Unit
224246testSequenceDefault = testTraversableFWith (SD <<< arrayFrom1UpTo)
225247
226248
@@ -261,10 +283,10 @@ instance bitraversableIOr :: Bitraversable IOr where
261283 bisequence (Fst fst) = Fst <$> fst
262284 bisequence (Snd snd) = Snd <$> snd
263285
264- testBifoldableIOrWith :: forall t e . (Bifoldable t , Eq (t Int Int )) =>
286+ testBifoldableIOrWith :: forall t . (Bifoldable t , Eq (t Int Int )) =>
265287 (forall l r . IOr l r -> t l r ) ->
266288 Int -> Int -> Int ->
267- Eff ( assert :: ASSERT | e ) Unit
289+ EffTest Unit
268290testBifoldableIOrWith lift fst snd u = do
269291 assert $ bifoldr (+) (*) u (lift $ Both fst snd) == fst + (snd * u)
270292 assert $ bifoldr (+) (*) u (lift $ Fst fst) == fst + u
@@ -278,9 +300,9 @@ testBifoldableIOrWith lift fst snd u = do
278300 assert $ bifoldMap Additive Additive (lift $ Fst fst) == Additive fst
279301 assert $ bifoldMap Additive Additive (lift $ Snd snd) == Additive snd
280302
281- testBitraversableIOrWith :: forall t e . (Bitraversable t , Eq (t Boolean Boolean )) =>
303+ testBitraversableIOrWith :: forall t . (Bitraversable t , Eq (t Boolean Boolean )) =>
282304 (forall l r . IOr l r -> t l r ) ->
283- Eff ( assert :: ASSERT | e ) Unit
305+ EffTest Unit
284306testBitraversableIOrWith lift = do
285307 let just a = Just (lift a)
286308 assert $ bisequence (lift (Both (Just true ) (Just false ))) == just (Both true false )
0 commit comments