@@ -3,23 +3,25 @@ module Test.Data.List.Lazy (testListLazy) where
33import Prelude
44
55import Control.Lazy (defer )
6- import Control.Monad.Eff (Eff )
7- import Control.Monad.Eff.Console (CONSOLE , log )
86import Data.FoldableWithIndex (foldMapWithIndex , foldlWithIndex , foldrWithIndex )
97import Data.FunctorWithIndex (mapWithIndex )
108import Data.Lazy as Z
11- import 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 , (:), (\\), (!! ))
9+ import Data.List.Lazy (List , Pattern (..), alterAt , catMaybes , concat , concatMap , cons , delete , deleteAt , deleteBy , drop , dropWhile , elemIndex , elemLastIndex , filter , filterM , findIndex , findLastIndex , foldM , foldMap , foldl , foldr , foldrLazy , fromFoldable , group , groupBy , head , init , insert , insertAt , insertBy , intersect , intersectBy , iterate , last , length , mapMaybe , modifyAt , nil , nub , nubBy , null , partition , range , repeat , replicate , replicateM , reverse , singleton , slice , snoc , span , stripPrefix , tail , take , takeWhile , transpose , uncons , union , unionBy , unzip , updateAt , zip , zipWith , zipWithA , (!!), (..) , (:), (\\))
1210import Data.List.Lazy.NonEmpty as NEL
1311import Data.Maybe (Maybe (..), isNothing , fromJust )
1412import Data.Monoid.Additive (Additive (..))
1513import Data.NonEmpty ((:|))
1614import Data.Traversable (traverse )
1715import Data.TraversableWithIndex (traverseWithIndex )
1816import Data.Tuple (Tuple (..))
17+ import Data.Unfoldable (unfoldr )
18+ import Data.Unfoldable1 (unfoldr1 )
19+ import Effect (Effect )
20+ import Effect.Console (log )
1921import Partial.Unsafe (unsafePartial )
20- import Test.Assert (ASSERT , assert )
22+ import Test.Assert (assert )
2123
22- testListLazy :: forall eff . Eff ( assert :: ASSERT , console :: CONSOLE | eff ) Unit
24+ testListLazy :: Effect Unit
2325testListLazy = do
2426 let
2527 l = fromFoldable
@@ -270,6 +272,12 @@ testListLazy = do
270272 assert $ (take 2 (l [1 , 2 , 3 ])) == l [1 , 2 ]
271273 assert $ (take 1 nil') == nil'
272274
275+ log " take should evaluate exactly n items which we needed"
276+ assert let oops x = 0 : oops x
277+ xs = 1 : defer oops
278+ in take 1 xs == l [1 ]
279+ -- If `take` evaluate more than once, it would crash with a stack overflow
280+
273281 log " takeWhile should keep all values that match a predicate from the front of an list"
274282 assert $ (takeWhile (_ /= 2 ) (l [1 , 2 , 3 ])) == l [1 ]
275283 assert $ (takeWhile (_ /= 3 ) (l [1 , 2 , 3 ])) == l [1 , 2 ]
@@ -396,9 +404,26 @@ testListLazy = do
396404 ((10 :20 :30 :nil) : (11 :31 :nil) : (32 :nil) : nil)
397405 log " transpose nil == nil"
398406 assert $ transpose nil == (nil :: List (List Int ))
407+
399408 log " transpose (singleton nil) == nil"
400409 assert $ transpose (singleton nil) == (nil :: List (List Int ))
401410
411+ log " unfoldr should maintain order"
412+ assert $ (1 ..5 ) == unfoldr step 1
413+
414+ log " unfoldr1 should maintain order"
415+ assert $ (1 ..5 ) == unfoldr1 step1 1
416+
417+ log " map should maintain order"
418+ assert $ (1 ..5 ) == map identity (1 ..5 )
419+
420+ step :: Int -> Maybe (Tuple Int Int )
421+ step 6 = Nothing
422+ step n = Just (Tuple n (n + 1 ))
423+
424+ step1 :: Int -> Tuple Int (Maybe Int )
425+ step1 n = Tuple n (if n >= 5 then Nothing else Just (n + 1 ))
426+
402427nil' :: List Int
403428nil' = nil
404429
0 commit comments