@@ -5,18 +5,18 @@ import Prelude
55import Control.Lazy (defer )
66import Control.Monad.Eff (Eff )
77import Control.Monad.Eff.Console (CONSOLE , log )
8-
8+ import Data.FoldableWithIndex (foldMapWithIndex , foldlWithIndex , foldrWithIndex )
9+ import Data.FunctorWithIndex (mapWithIndex )
910import Data.Lazy as Z
1011import Data.List.Lazy (List , nil , stripPrefix , Pattern (..), cons , foldl , foldr , foldMap , singleton , transpose , take , iterate , filter , uncons , foldM , foldrLazy , range , unzip , zip , length , zipWithA , replicate , repeat , zipWith , intersectBy , intersect , deleteBy , delete , unionBy , union , nubBy , nub , groupBy , group , partition , span , dropWhile , drop , takeWhile , slice , catMaybes , mapMaybe , filterM , concat , concatMap , reverse , alterAt , modifyAt , updateAt , deleteAt , insertAt , findLastIndex , findIndex , elemLastIndex , elemIndex , init , tail , last , head , insertBy , insert , snoc , null , replicateM , fromFoldable , (:), (\\), (!!))
1112import Data.List.Lazy.NonEmpty as NEL
1213import Data.Maybe (Maybe (..), isNothing , fromJust )
1314import Data.Monoid.Additive (Additive (..))
1415import Data.NonEmpty ((:|))
1516import Data.Traversable (traverse )
17+ import Data.TraversableWithIndex (traverseWithIndex )
1618import Data.Tuple (Tuple (..))
17-
1819import Partial.Unsafe (unsafePartial )
19-
2020import Test.Assert (ASSERT , assert )
2121
2222testListLazy :: forall eff . Eff (assert :: ASSERT , console :: CONSOLE | eff ) Unit
@@ -48,9 +48,34 @@ testListLazy = do
4848 log " foldMap should be left-to-right"
4949 assert $ foldMap show (range 1 5 ) == " 12345"
5050
51+ log " foldlWithIndex should be correct"
52+ assert $ foldlWithIndex (\i b _ -> i + b) 0 (range 0 10000 ) == 50005000
53+
54+ log " foldlWithIndex should be stack-safe"
55+ void $ pure $ foldlWithIndex (\i b _ -> i + b) 0 $ range 0 100000
56+
57+ log " foldrWithIndex should be correct"
58+ assert $ foldrWithIndex (\i _ b -> i + b) 0 (range 0 10000 ) == 50005000
59+
60+ log " foldrWithIndex should be stack-safe"
61+ void $ pure $ foldrWithIndex (\i _ b -> i + b) 0 $ range 0 100000
62+
63+ log " foldMapWithIndex should be stack-safe"
64+ void $ pure $ foldMapWithIndex (\i _ -> Additive i) $ range 1 100000
65+
66+ log " foldMapWithIndex should be left-to-right"
67+ assert $ foldMapWithIndex (\i _ -> show i) (fromFoldable [0 , 0 , 0 ]) == " 012"
68+
5169 log " traverse should be stack-safe"
5270 assert $ ((traverse Just longList) >>= last) == last longList
5371
72+ log " traverseWithIndex should be stack-safe"
73+ assert $ traverseWithIndex (const Just ) longList == Just longList
74+
75+ log " traverseWithIndex should be correct"
76+ assert $ traverseWithIndex (\i a -> Just $ i + a) (fromFoldable [2 , 2 , 2 ])
77+ == Just (fromFoldable [2 , 3 , 4 ])
78+
5479 log " bind should be stack-safe"
5580 void $ pure $ last $ longList >>= pure
5681
@@ -228,6 +253,9 @@ testListLazy = do
228253 log " catMaybe should take an list of Maybe values and throw out Nothings"
229254 assert $ catMaybes (l [Nothing , Just 2 , Nothing , Just 4 ]) == l [2 , 4 ]
230255
256+ log " mapWithIndex should take a list of values and apply a function which also takes the index into account"
257+ assert $ mapWithIndex (\x ix -> x + ix) (fromFoldable [0 , 1 , 2 , 3 ]) == fromFoldable [0 , 2 , 4 , 6 ]
258+
231259 -- log "sort should reorder a list into ascending order based on the result of compare"
232260 -- assert $ sort (l [1, 3, 2, 5, 6, 4]) == l [1, 2, 3, 4, 5, 6]
233261
0 commit comments