@@ -7,10 +7,12 @@ import Control.Monad.Eff (Eff)
77import Control.Monad.Eff.Console (CONSOLE , log )
88
99import Data.Lazy as Z
10- import Data.List.Lazy (List , nil , cons , singleton , transpose , take , iterate , filter , uncons , foldM , range , unzip , zip , length , zipWithA , replicate , repeat , zipWith , intersectBy , intersect , deleteBy , delete , unionBy , union , nubBy , nub , groupBy , group , 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 , (:), (\\), (!!))
10+ import Data.List.Lazy (List , nil , cons , foldl , foldr , foldMap , singleton , transpose , take , iterate , filter , uncons , foldM , range , unzip , zip , length , zipWithA , replicate , repeat , zipWith , intersectBy , intersect , deleteBy , delete , unionBy , union , nubBy , nub , groupBy , group , 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 , (:), (\\), (!!))
1111import Data.List.Lazy.NonEmpty as NEL
1212import Data.Maybe (Maybe (..), isNothing , fromJust )
13+ import Data.Monoid.Additive (Additive (..))
1314import Data.NonEmpty ((:|))
15+ import Data.Traversable (traverse )
1416import Data.Tuple (Tuple (..))
1517
1618import Partial.Unsafe (unsafePartial )
@@ -22,6 +24,31 @@ testListLazy = do
2224 let
2325 l = fromFoldable
2426 nel xxs = NEL.NonEmptyList (Z .defer \_ -> xxs)
27+ longList = range 1 100000
28+
29+ log " append should be stack-safe"
30+ assert $ length (longList <> longList) == (2 * length longList)
31+
32+ log " map should be stack-safe"
33+ assert $ (last $ (_ + 1 ) <$> longList) == ((_ + 1 ) <$> last longList)
34+
35+ log " foldl should be stack-safe"
36+ void $ pure $ foldl (+) 0 longList
37+
38+ log " foldr should be stack-safe"
39+ void $ pure $ foldr (+) 0 longList
40+
41+ log " foldMap should be stack-safe"
42+ void $ pure $ foldMap Additive longList
43+
44+ log " foldMap should be left-to-right"
45+ assert $ foldMap show (range 1 5 ) == " 12345"
46+
47+ log " traverse should be stack-safe"
48+ assert $ ((traverse Just longList) >>= last) == last longList
49+
50+ log " bind should be stack-safe"
51+ void $ pure $ last $ longList >>= pure
2552
2653 log " singleton should construct an list with a single value"
2754 assert $ singleton 1 == l [1 ]
@@ -57,6 +84,9 @@ testListLazy = do
5784 log " null should return true for an empty list"
5885 assert $ null nil' == true
5986
87+ log " length should be stack-safe"
88+ assert $ length longList == 100000
89+
6090 log " length should return the number of items in an list"
6191 assert $ length nil' == 0
6292 assert $ length (l [1 ]) == 1
@@ -66,6 +96,9 @@ testListLazy = do
6696 assert $ l [1 , 2 , 3 ] `snoc` 4 == l [1 , 2 , 3 , 4 ]
6797 assert $ nil' `snoc` 1 == l [1 ]
6898
99+ log " insert should be stack-safe"
100+ assert $ last (insert 100001 longList) == Just 100001
101+
69102 log " insert should add an item at the appropriate place in a sorted list"
70103 assert $ insert 1.5 (l [1.0 , 2.0 , 3.0 ]) == l [1.0 , 1.5 , 2.0 , 3.0 ]
71104 assert $ insert 4 (l [1 , 2 , 3 ]) == l [1 , 2 , 3 , 4 ]
@@ -167,6 +200,9 @@ testListLazy = do
167200 assert $ (reverse (l [1 , 2 , 3 ])) == l [3 , 2 , 1 ]
168201 assert $ (reverse nil') == nil'
169202
203+ log " reverse should be stack-safe"
204+ assert $ head (reverse longList) == last longList
205+
170206 log " concat should join an list of lists"
171207 assert $ (concat (l [l [1 , 2 ], l [3 , 4 ]] )) == l [1 , 2 , 3 , 4 ]
172208 assert $ (concat (l [l [1 ], nil'] )) == l [1 ]
0 commit comments