@@ -4,10 +4,13 @@ import Prelude
44
55import Control.Monad.Eff (Eff ())
66import Control.Monad.Eff.Console
7- import Data.Foldable
87import Data.Maybe
98import Data.Monoid.Additive
9+ import Data.Foldable
1010import Data.Traversable
11+ import Data.Bifoldable
12+ import Data.Bifunctor
13+ import Data.Bitraversable
1114import Test.Assert
1215
1316foreign import arrayFrom1UpTo :: Int -> Array Int
@@ -16,15 +19,9 @@ main = do
1619 log " Test foldableArray instance"
1720 testFoldableArrayWith 20
1821
19- log " Test traversableArray instance"
20- testTraversableArrayWith 20
21-
2222 log " Test foldableArray instance is stack safe"
2323 testFoldableArrayWith 20000
2424
25- log " Test traversableArray instance is stack safe"
26- testTraversableArrayWith 20000
27-
2825 log " Test foldMapDefaultL"
2926 testFoldableFoldMapDefaultL 20
3027
@@ -37,37 +34,49 @@ main = do
3734 log " Test foldrDefault"
3835 testFoldableFoldlDefault 20
3936
37+ log " Test traversableArray instance"
38+ testTraversableArrayWith 20
39+
40+ log " Test traversableArray instance is stack safe"
41+ testTraversableArrayWith 20000
42+
4043 log " Test traverseDefault"
4144 testTraverseDefault 20
4245
4346 log " Test sequenceDefault"
4447 testSequenceDefault 20
4548
49+ log " Test Bifoldable on `inclusive or`"
50+ testBifoldableIOrWith 10 100 42
51+
52+ log " Test Bitraversable on `inclusive or`"
53+ testBitraversableIOr
54+
4655 log " All done!"
4756
4857
4958testFoldableFWith :: forall f e . (Foldable f , Eq (f Int )) =>
5059 (Int -> f Int ) -> Int -> Eff (assert :: ASSERT | e ) Unit
5160testFoldableFWith f n = do
52- let arr = f n
61+ let dat = f n
5362 let expectedSum = (n / 2 ) * (n + 1 )
5463
55- assert $ foldr (+) 0 arr == expectedSum
56- assert $ foldl (+) 0 arr == expectedSum
57- assert $ foldMap Additive arr == Additive expectedSum
64+ assert $ foldr (+) 0 dat == expectedSum
65+ assert $ foldl (+) 0 dat == expectedSum
66+ assert $ foldMap Additive dat == Additive expectedSum
5867
5968testFoldableArrayWith = testFoldableFWith arrayFrom1UpTo
6069
6170
6271testTraversableFWith :: forall f e . (Traversable f , Eq (f Int )) =>
6372 (Int -> f Int ) -> Int -> Eff (assert :: ASSERT | e ) Unit
6473testTraversableFWith f n = do
65- let arr = f n
74+ let dat = f n
6675
67- assert $ traverse Just arr == Just arr
68- assert $ traverse return arr == [arr ]
69- assert $ traverse (\x -> if x < 10 then Just x else Nothing ) arr == Nothing
70- assert $ sequence (map Just arr ) == traverse Just arr
76+ assert $ traverse Just dat == Just dat
77+ assert $ traverse return dat == [dat ]
78+ assert $ traverse (\x -> if x < 10 then Just x else Nothing ) dat == Nothing
79+ assert $ sequence (map Just dat ) == traverse Just dat
7180
7281testTraversableArrayWith = testTraversableFWith arrayFrom1UpTo
7382
@@ -146,3 +155,64 @@ instance traversableSD :: Traversable SequenceDefault where
146155testTraverseDefault = testTraversableFWith (TD <<< arrayFrom1UpTo)
147156testSequenceDefault = testTraversableFWith (SD <<< arrayFrom1UpTo)
148157
158+
159+ -- structure for testing bifoldable, picked `inclusive or` as it has both products and sums
160+
161+ data IOr l r = Both l r | Fst l | Snd r
162+
163+ instance eqIOr :: (Eq l , Eq r ) => Eq (IOr l r ) where
164+ eq (Both lFst lSnd) (Both rFst rSnd) = (lFst == rFst) && (lSnd == rSnd)
165+ eq (Fst l) (Fst r) = l == r
166+ eq (Snd l) (Snd r) = l == r
167+ eq _ _ = false
168+
169+ instance bifoldableIOr :: Bifoldable IOr where
170+ bifoldr l r u (Both fst snd) = l fst (r snd u)
171+ bifoldr l r u (Fst fst) = l fst u
172+ bifoldr l r u (Snd snd) = r snd u
173+
174+ bifoldl l r u (Both fst snd) = r (l u fst) snd
175+ bifoldl l r u (Fst fst) = l u fst
176+ bifoldl l r u (Snd snd) = r u snd
177+
178+ bifoldMap l r (Both fst snd) = l fst <> r snd
179+ bifoldMap l r (Fst fst) = l fst
180+ bifoldMap l r (Snd snd) = r snd
181+
182+ instance bifunctorIOr :: Bifunctor IOr where
183+ bimap f g (Both fst snd) = Both (f fst) (g snd)
184+ bimap f g (Fst fst) = Fst (f fst)
185+ bimap f g (Snd snd) = Snd (g snd)
186+
187+ instance bitraversableIOr :: Bitraversable IOr where
188+ bitraverse f g (Both fst snd) = Both <$> f fst <*> g snd
189+ bitraverse f g (Fst fst) = Fst <$> f fst
190+ bitraverse f g (Snd snd) = Snd <$> g snd
191+
192+ bisequence (Both fst snd) = Both <$> fst <*> snd
193+ bisequence (Fst fst) = Fst <$> fst
194+ bisequence (Snd snd) = Snd <$> snd
195+
196+ testBifoldableIOrWith :: forall e . Int -> Int -> Int -> Eff (assert :: ASSERT | e ) Unit
197+ testBifoldableIOrWith fst snd u = do
198+ assert $ bifoldr (+) (*) u (Both fst snd) == fst + (snd * u)
199+ assert $ bifoldr (+) (*) u (Fst fst) == fst + u
200+ assert $ bifoldr (+) (*) u (Snd snd) == snd * u
201+
202+ assert $ bifoldl (+) (*) u (Both fst snd) == (u + fst) * snd
203+ assert $ bifoldl (+) (*) u (Fst fst) == u + fst
204+ assert $ bifoldl (+) (*) u (Snd snd) == u * snd
205+
206+ assert $ bifoldMap Additive Additive (Both fst snd) == Additive (fst + snd)
207+ assert $ bifoldMap Additive Additive (Fst fst) == Additive fst
208+ assert $ bifoldMap Additive Additive (Snd snd) == Additive snd
209+
210+ testBitraversableIOr :: forall e . Eff (assert :: ASSERT | e ) Unit
211+ testBitraversableIOr = do
212+ assert $ bisequence (Both (Just true ) (Just false )) == Just (Both true false )
213+ assert $ bisequence (Fst (Just true )) == Just (Fst true :: IOr Boolean Boolean )
214+ assert $ bisequence (Snd (Just false )) == Just (Snd false :: IOr Boolean Boolean )
215+ assert $ bitraverse Just Just (Both true false ) == Just (Both true false )
216+ assert $ bitraverse Just Just (Fst true ) == Just (Fst true :: IOr Boolean Boolean )
217+ assert $ bitraverse Just Just (Snd false ) == Just (Snd false :: IOr Boolean Boolean )
218+
0 commit comments