@@ -6,17 +6,20 @@ import Control.Monad.Eff (Eff)
6
6
import Control.Monad.Eff.Console (log , CONSOLE )
7
7
import Control.Monad.Eff.Exception (EXCEPTION )
8
8
import Control.Monad.Eff.Random (RANDOM )
9
+ import Control.Monad.Writer (runWriter , tell )
9
10
import Data.Array as A
10
- import Data.Foldable (foldl )
11
+ import Data.Foldable (foldl , foldr )
12
+ import Data.FoldableWithIndex (foldlWithIndex , foldrWithIndex , foldMapWithIndex )
11
13
import Data.Function (on )
12
14
import Data.List as L
13
15
import Data.List.NonEmpty as NEL
14
16
import Data.Maybe (Maybe (..))
15
17
import Data.NonEmpty ((:|))
16
18
import Data.StrMap as M
17
19
import Data.StrMap.Gen (genStrMap )
18
- import Data.Traversable (sequence )
19
- import Data.Tuple (Tuple (..), fst , uncurry )
20
+ import Data.Traversable (sequence , traverse )
21
+ import Data.TraversableWithIndex (traverseWithIndex )
22
+ import Data.Tuple (Tuple (..), fst , snd , uncurry )
20
23
import Partial.Unsafe (unsafePartial )
21
24
import Test.QuickCheck ((<?>), quickCheck , quickCheck' , (===))
22
25
import Test.QuickCheck.Arbitrary (class Arbitrary , arbitrary )
@@ -198,6 +201,34 @@ strMapTests = do
198
201
resultViaLists = m # M .toUnfoldable # map (\(Tuple k v) → Tuple k (f k v)) # (M .fromFoldable :: forall a . L.List (Tuple String a ) -> M.StrMap a )
199
202
in resultViaMapWithKey === resultViaLists
200
203
204
+ log " foldl = foldlWithIndex <<< const"
205
+ quickCheck \(TestStrMap m :: TestStrMap String ) ->
206
+ let f z v = z <> " ," <> v
207
+ in foldl f " " m === foldlWithIndex (const f) " " m
208
+
209
+ log " foldr = foldrWithIndex <<< const"
210
+ quickCheck \(TestStrMap m :: TestStrMap String ) ->
211
+ let f v z = v <> " ," <> z
212
+ in foldr f " " m === foldrWithIndex (const f) " " m
213
+
214
+ log " foldlWithIndex = foldrWithIndex with flipped operation"
215
+ quickCheck \(TestStrMap m :: TestStrMap String ) ->
216
+ let f k z v = z <> " ," <> k <> " :" <> v
217
+ g k v z = k <> " :" <> v <> " ," <> z
218
+ in foldlWithIndex f " " m <> " ," === " ," <> foldrWithIndex g " " m
219
+
220
+ log " foldMapWithIndex f ~ traverseWithIndex (\\ k v -> tell (f k v))"
221
+ quickCheck \(TestStrMap m :: TestStrMap Int ) ->
222
+ let f k v = " (" <> " k" <> " ," <> show v <> " )"
223
+ resultA = foldMapWithIndex f m
224
+ resultB = snd (runWriter (traverseWithIndex (\k v -> tell (f k v)) m))
225
+ in resultA === resultB
226
+
227
+ log " traverse = traverseWithIndex <<< const (for m = Writer)"
228
+ quickCheck \(TestStrMap m :: TestStrMap String ) ->
229
+ runWriter (traverse tell m) ===
230
+ runWriter (traverseWithIndex (const tell) m)
231
+
201
232
log " sequence works (for m = Array)"
202
233
quickCheck \(TestStrMap mOfSmallArrays :: TestStrMap (SmallArray Int )) ->
203
234
let m = (\(SmallArray a) -> a) <$> mOfSmallArrays
0 commit comments