From 32799ce82c4efbd425afc97ef8c12b0d10ea4e5e Mon Sep 17 00:00:00 2001 From: Harry Garrood Date: Mon, 14 Dec 2015 15:27:09 +0100 Subject: [PATCH] Improve names for toList, fromList. Fixes #50 toList and fromList are still exported, but they are now deprecated. --- docs/Data/List.md | 60 ++++++---- docs/Data/List/Lazy.md | 62 ++++++---- docs/Data/List/ZipList.md | 26 ++--- src/Data/List.purs | 25 +++- src/Data/List/Lazy.purs | 26 +++-- test/Test/Data/List.purs | 199 ++++++++++++++++---------------- test/Test/Data/List/Lazy.purs | 199 ++++++++++++++++---------------- test/Test/Data/List/Unsafe.purs | 11 +- 8 files changed, 334 insertions(+), 274 deletions(-) diff --git a/docs/Data/List.md b/docs/Data/List.md index 3663136..8ef9063 100644 --- a/docs/Data/List.md +++ b/docs/Data/List.md @@ -25,39 +25,39 @@ which case it consists of a head element, and another list (represented by the ##### Instances ``` purescript -instance showList :: (Show a) => Show (List a) -instance eqList :: (Eq a) => Eq (List a) -instance ordList :: (Ord a) => Ord (List a) -instance semigroupList :: Semigroup (List a) -instance monoidList :: Monoid (List a) -instance functorList :: Functor List -instance foldableList :: Foldable List -instance unfoldableList :: Unfoldable List -instance traversableList :: Traversable List -instance applyList :: Apply List -instance applicativeList :: Applicative List -instance bindList :: Bind List -instance monadList :: Monad List -instance altList :: Alt List -instance plusList :: Plus List -instance alternativeList :: Alternative List -instance monadPlusList :: MonadPlus List +(Show a) => Show (List a) +(Eq a) => Eq (List a) +(Ord a) => Ord (List a) +Semigroup (List a) +Monoid (List a) +Functor List +Foldable List +Unfoldable List +Traversable List +Apply List +Applicative List +Bind List +Monad List +Alt List +Plus List +Alternative List +MonadPlus List ``` -#### `fromList` +#### `toUnfoldable` ``` purescript -fromList :: forall f a. (Unfoldable f) => List a -> f a +toUnfoldable :: forall f a. (Unfoldable f) => List a -> f a ``` Convert a list into any unfoldable structure. Running time: `O(n)` -#### `toList` +#### `fromFoldable` ``` purescript -toList :: forall f a. (Foldable f) => f a -> List a +fromFoldable :: forall f a. (Foldable f) => f a -> List a ``` Construct a list from a foldable structure. @@ -662,4 +662,22 @@ second components. foldM :: forall m a b. (Monad m) => (a -> b -> m a) -> a -> List b -> m a ``` +#### `toList` + +``` purescript +toList :: forall f a. (Foldable f) => f a -> List a +``` + +*Deprecated.* Use `fromFoldable` instead. `toList` will be removed in a +later version. + +#### `fromList` + +``` purescript +fromList :: forall f a. (Unfoldable f) => List a -> f a +``` + +*Deprecated.* Use `toUnfoldable` instead. `fromList` will be removed in a +later version. + diff --git a/docs/Data/List/Lazy.md b/docs/Data/List/Lazy.md index eee7c65..433835d 100644 --- a/docs/Data/List/Lazy.md +++ b/docs/Data/List/Lazy.md @@ -20,24 +20,24 @@ A lazy linked list. ##### Instances ``` purescript -instance showList :: (Show a) => Show (List a) -instance eqList :: (Eq a) => Eq (List a) -instance ordList :: (Ord a) => Ord (List a) -instance lazyList :: Lazy (List a) -instance semigroupList :: Semigroup (List a) -instance monoidList :: Monoid (List a) -instance functorList :: Functor List -instance foldableList :: Foldable List -instance unfoldableList :: Unfoldable List -instance traversableList :: Traversable List -instance applyList :: Apply List -instance applicativeList :: Applicative List -instance bindList :: Bind List -instance monadList :: Monad List -instance altList :: Alt List -instance plusList :: Plus List -instance alternativeList :: Alternative List -instance monadPlusList :: MonadPlus List +(Show a) => Show (List a) +(Eq a) => Eq (List a) +(Ord a) => Ord (List a) +Lazy (List a) +Semigroup (List a) +Monoid (List a) +Functor List +Foldable List +Unfoldable List +Traversable List +Apply List +Applicative List +Bind List +Monad List +Alt List +Plus List +Alternative List +MonadPlus List ``` #### `runList` @@ -48,20 +48,20 @@ runList :: forall a. List a -> Lazy (Step a) Unwrap a lazy linked list -#### `fromList` +#### `toUnfoldable` ``` purescript -fromList :: forall f a. (Unfoldable f) => List a -> f a +toUnfoldable :: forall f a. (Unfoldable f) => List a -> f a ``` Convert a list into any unfoldable structure. Running time: `O(n)` -#### `toList` +#### `fromFoldable` ``` purescript -toList :: forall f a. (Foldable f) => f a -> List a +fromFoldable :: forall f a. (Foldable f) => f a -> List a ``` Construct a list from a foldable structure. @@ -584,4 +584,22 @@ Collect pairs of elements at the same positions in two lists. Running time: `O(min(m, n))` +#### `toList` + +``` purescript +toList :: forall f a. (Foldable f) => f a -> List a +``` + +*Deprecated.* Use `fromFoldable` instead. `toList` will be removed in a +later version. + +#### `fromList` + +``` purescript +fromList :: forall f a. (Unfoldable f) => List a -> f a +``` + +*Deprecated.* Use `toUnfoldable` instead. `fromList` will be removed in a +later version. + diff --git a/docs/Data/List/ZipList.md b/docs/Data/List/ZipList.md index 0acb0f6..4c62960 100644 --- a/docs/Data/List/ZipList.md +++ b/docs/Data/List/ZipList.md @@ -15,19 +15,19 @@ newtype ZipList a ##### Instances ``` purescript -instance showZipList :: (Show a) => Show (ZipList a) -instance eqZipList :: (Eq a) => Eq (ZipList a) -instance ordZipList :: (Ord a) => Ord (ZipList a) -instance semigroupZipList :: Semigroup (ZipList a) -instance monoidZipList :: Monoid (ZipList a) -instance foldableZipList :: Foldable ZipList -instance traversableZipList :: Traversable ZipList -instance functorZipList :: Functor ZipList -instance applyZipList :: Apply ZipList -instance applicativeZipList :: Applicative ZipList -instance altZipList :: Alt ZipList -instance plusZipList :: Plus ZipList -instance alternativeZipList :: Alternative ZipList +(Show a) => Show (ZipList a) +(Eq a) => Eq (ZipList a) +(Ord a) => Ord (ZipList a) +Semigroup (ZipList a) +Monoid (ZipList a) +Foldable ZipList +Traversable ZipList +Functor ZipList +Apply ZipList +Applicative ZipList +Alt ZipList +Plus ZipList +Alternative ZipList ``` #### `runZipList` diff --git a/src/Data/List.purs b/src/Data/List.purs index c5c2450..b6a8db7 100644 --- a/src/Data/List.purs +++ b/src/Data/List.purs @@ -9,8 +9,8 @@ module Data.List ( List(..) - , fromList - , toList + , toUnfoldable + , fromFoldable , singleton , (..), range @@ -81,6 +81,9 @@ module Data.List , unzip , foldM + + , toList + , fromList ) where import Prelude @@ -108,14 +111,14 @@ data List a = Nil | Cons a (List a) -- | Convert a list into any unfoldable structure. -- | -- | Running time: `O(n)` -fromList :: forall f a. (Unfoldable f) => List a -> f a -fromList = unfoldr (\xs -> (\rec -> Tuple rec.head rec.tail) <$> uncons xs) +toUnfoldable :: forall f a. (Unfoldable f) => List a -> f a +toUnfoldable = unfoldr (\xs -> (\rec -> Tuple rec.head rec.tail) <$> uncons xs) -- | Construct a list from a foldable structure. -- | -- | Running time: `O(n)` -toList :: forall f a. (Foldable f) => f a -> List a -toList = foldr Cons Nil +fromFoldable :: forall f a. (Foldable f) => f a -> List a +fromFoldable = foldr Cons Nil -------------------------------------------------------------------------------- -- List creation --------------------------------------------------------------- @@ -680,6 +683,16 @@ foldM :: forall m a b. (Monad m) => (a -> b -> m a) -> a -> List b -> m a foldM _ a Nil = return a foldM f a (Cons b bs) = f a b >>= \a' -> foldM f a' bs +-- | *Deprecated.* Use `fromFoldable` instead. `toList` will be removed in a +-- | later version. +toList :: forall f a. (Foldable f) => f a -> List a +toList = fromFoldable + +-- | *Deprecated.* Use `toUnfoldable` instead. `fromList` will be removed in a +-- | later version. +fromList :: forall f a. (Unfoldable f) => List a -> f a +fromList = toUnfoldable + -------------------------------------------------------------------------------- -- Instances ------------------------------------------------------------------- -------------------------------------------------------------------------------- diff --git a/src/Data/List/Lazy.purs b/src/Data/List/Lazy.purs index cf92a97..4986ddb 100644 --- a/src/Data/List/Lazy.purs +++ b/src/Data/List/Lazy.purs @@ -10,8 +10,8 @@ module Data.List.Lazy ( List(..) , runList - , fromList - , toList + , toUnfoldable + , fromFoldable , Step(..) , step , nil @@ -88,6 +88,8 @@ module Data.List.Lazy -- , unzip -- , foldM + , toList + , fromList ) where import Prelude @@ -117,14 +119,14 @@ runList (List l) = l -- | Convert a list into any unfoldable structure. -- | -- | Running time: `O(n)` -fromList :: forall f a. (Unfoldable f) => List a -> f a -fromList = unfoldr (\xs -> (\rec -> Tuple rec.head rec.tail) <$> uncons xs) +toUnfoldable :: forall f a. (Unfoldable f) => List a -> f a +toUnfoldable = unfoldr (\xs -> (\rec -> Tuple rec.head rec.tail) <$> uncons xs) -- | Construct a list from a foldable structure. -- | -- | Running time: `O(n)` -toList :: forall f a. (Foldable f) => f a -> List a -toList = foldr cons nil +fromFoldable :: forall f a. (Foldable f) => f a -> List a +fromFoldable = foldr cons nil -- | A list is either empty (represented by the `Nil` constructor) or non-empty, in -- | which case it consists of a head element, and another list (represented by the @@ -621,9 +623,15 @@ zipWith f xs ys = List (go <$> runList xs <*> runList ys) zip :: forall a b. List a -> List b -> List (Tuple a b) zip = zipWith Tuple --------------------------------------------------------------------------------- --- Folding --------------------------------------------------------------------- --------------------------------------------------------------------------------- +-- | *Deprecated.* Use `fromFoldable` instead. `toList` will be removed in a +-- | later version. +toList :: forall f a. (Foldable f) => f a -> List a +toList = fromFoldable + +-- | *Deprecated.* Use `toUnfoldable` instead. `fromList` will be removed in a +-- | later version. +fromList :: forall f a. (Unfoldable f) => List a -> f a +fromList = toUnfoldable -------------------------------------------------------------------------------- -- Instances ------------------------------------------------------------------- diff --git a/test/Test/Data/List.purs b/test/Test/Data/List.purs index 83e4dff..5e7dadf 100644 --- a/test/Test/Data/List.purs +++ b/test/Test/Data/List.purs @@ -13,79 +13,80 @@ import Test.Assert (ASSERT(), assert) testList :: forall eff. Eff (assert :: ASSERT, console :: CONSOLE | eff) Unit testList = do + let l = fromFoldable log "singleton should construct an list with a single value" - assert $ singleton 1 == toList [1] - assert $ singleton "foo" == toList ["foo"] - assert $ singleton nil == toList [toList []] + assert $ singleton 1 == l [1] + assert $ singleton "foo" == l ["foo"] + assert $ singleton nil == l [l []] log "range should create an inclusive list of integers for the specified start and end" - assert $ (range 0 5) == toList [0, 1, 2, 3, 4, 5] - assert $ (range 2 (-3)) == toList [2, 1, 0, -1, -2, -3] + assert $ (range 0 5) == l [0, 1, 2, 3, 4, 5] + assert $ (range 2 (-3)) == l [2, 1, 0, -1, -2, -3] log "replicate should produce an list containg an item a specified number of times" - assert $ replicate 3 true == toList [true, true, true] - assert $ replicate 1 "foo" == toList ["foo"] - assert $ replicate 0 "foo" == toList [] - assert $ replicate (-1) "foo" == toList [] + assert $ replicate 3 true == l [true, true, true] + assert $ replicate 1 "foo" == l ["foo"] + assert $ replicate 0 "foo" == l [] + assert $ replicate (-1) "foo" == l [] log "replicateM should perform the monadic action the correct number of times" - assert $ replicateM 3 (Just 1) == Just (toList [1, 1, 1]) - assert $ replicateM 1 (Just 1) == Just (toList [1]) - assert $ replicateM 0 (Just 1) == Just (toList []) - assert $ replicateM (-1) (Just 1) == Just (toList []) + assert $ replicateM 3 (Just 1) == Just (l [1, 1, 1]) + assert $ replicateM 1 (Just 1) == Just (l [1]) + assert $ replicateM 0 (Just 1) == Just (l []) + assert $ replicateM (-1) (Just 1) == Just (l []) -- some -- many log "null should return false for non-empty lists" - assert $ null (toList [1]) == false - assert $ null (toList [1, 2, 3]) == false + assert $ null (l [1]) == false + assert $ null (l [1, 2, 3]) == false log "null should return true for an empty list" assert $ null nil == true log "length should return the number of items in an list" assert $ length nil == 0 - assert $ length (toList [1]) == 1 - assert $ length (toList [1, 2, 3, 4, 5]) == 5 + assert $ length (l [1]) == 1 + assert $ length (l [1, 2, 3, 4, 5]) == 5 log "length should be stack-safe" void $ pure $ length (range 1 100000) log "snoc should add an item to the end of an list" - assert $ toList [1, 2, 3] `snoc` 4 == toList [1, 2, 3, 4] - assert $ nil `snoc` 1 == toList [1] + assert $ l [1, 2, 3] `snoc` 4 == l [1, 2, 3, 4] + assert $ nil `snoc` 1 == l [1] log "insert should add an item at the appropriate place in a sorted list" - assert $ insert 1.5 (toList [1.0, 2.0, 3.0]) == toList [1.0, 1.5, 2.0, 3.0] - assert $ insert 4 (toList [1, 2, 3]) == toList [1, 2, 3, 4] - assert $ insert 0 (toList [1, 2, 3]) == toList [0, 1, 2, 3] + assert $ insert 1.5 (l [1.0, 2.0, 3.0]) == l [1.0, 1.5, 2.0, 3.0] + assert $ insert 4 (l [1, 2, 3]) == l [1, 2, 3, 4] + assert $ insert 0 (l [1, 2, 3]) == l [0, 1, 2, 3] log "insertBy should add an item at the appropriate place in a sorted list using the specified comparison" - assert $ insertBy (flip compare) 4 (toList [1, 2, 3]) == toList [4, 1, 2, 3] - assert $ insertBy (flip compare) 0 (toList [1, 2, 3]) == toList [1, 2, 3, 0] + assert $ insertBy (flip compare) 4 (l [1, 2, 3]) == l [4, 1, 2, 3] + assert $ insertBy (flip compare) 0 (l [1, 2, 3]) == l [1, 2, 3, 0] log "head should return a Just-wrapped first value of a non-empty list" - assert $ head (toList ["foo", "bar"]) == Just "foo" + assert $ head (l ["foo", "bar"]) == Just "foo" log "head should return Nothing for an empty list" assert $ head nil == Nothing log "last should return a Just-wrapped last value of a non-empty list" - assert $ last (toList ["foo", "bar"]) == Just "bar" + assert $ last (l ["foo", "bar"]) == Just "bar" log "last should return Nothing for an empty list" assert $ last nil == Nothing log "tail should return a Just-wrapped list containing all the items in an list apart from the first for a non-empty list" - assert $ tail (toList ["foo", "bar", "baz"]) == Just (toList ["bar", "baz"]) + assert $ tail (l ["foo", "bar", "baz"]) == Just (l ["bar", "baz"]) log "tail should return Nothing for an empty list" assert $ tail nil == Nothing log "init should return a Just-wrapped list containing all the items in an list apart from the first for a non-empty list" - assert $ init (toList ["foo", "bar", "baz"]) == Just (toList ["foo", "bar"]) + assert $ init (l ["foo", "bar", "baz"]) == Just (l ["foo", "bar"]) log "init should return Nothing for an empty list" assert $ init nil == Nothing @@ -94,185 +95,185 @@ testList = do assert $ isNothing (uncons nil) log "uncons should split an list into a head and tail record when there is at least one item" - let u1 = uncons (toList [1]) + let u1 = uncons (l [1]) assert $ (fromJust u1).head == 1 - assert $ (fromJust u1).tail == toList [] - let u2 = uncons (toList [1, 2, 3]) + assert $ (fromJust u1).tail == l [] + let u2 = uncons (l [1, 2, 3]) assert $ (fromJust u2).head == 1 - assert $ (fromJust u2).tail == toList [2, 3] + assert $ (fromJust u2).tail == l [2, 3] log "(!!) should return Just x when the index is within the bounds of the list" - assert $ toList [1, 2, 3] !! 0 == (Just 1) - assert $ toList [1, 2, 3] !! 1 == (Just 2) - assert $ toList [1, 2, 3] !! 2 == (Just 3) + assert $ l [1, 2, 3] !! 0 == (Just 1) + assert $ l [1, 2, 3] !! 1 == (Just 2) + assert $ l [1, 2, 3] !! 2 == (Just 3) log "(!!) should return Nothing when the index is outside of the bounds of the list" - assert $ toList [1, 2, 3] !! 6 == Nothing - assert $ toList [1, 2, 3] !! (-1) == Nothing + assert $ l [1, 2, 3] !! 6 == Nothing + assert $ l [1, 2, 3] !! (-1) == Nothing log "elemIndex should return the index of an item that a predicate returns true for in an list" - assert $ elemIndex 1 (toList [1, 2, 1]) == Just 0 - assert $ elemIndex 4 (toList [1, 2, 1]) == Nothing + assert $ elemIndex 1 (l [1, 2, 1]) == Just 0 + assert $ elemIndex 4 (l [1, 2, 1]) == Nothing log "elemLastIndex should return the last index of an item in an list" - assert $ elemLastIndex 1 (toList [1, 2, 1]) == Just 2 - assert $ elemLastIndex 4 (toList [1, 2, 1]) == Nothing + assert $ elemLastIndex 1 (l [1, 2, 1]) == Just 2 + assert $ elemLastIndex 4 (l [1, 2, 1]) == Nothing log "findIndex should return the index of an item that a predicate returns true for in an list" - assert $ findIndex (/= 1) (toList [1, 2, 1]) == Just 1 - assert $ findIndex (== 3) (toList [1, 2, 1]) == Nothing + assert $ findIndex (/= 1) (l [1, 2, 1]) == Just 1 + assert $ findIndex (== 3) (l [1, 2, 1]) == Nothing log "findLastIndex should return the last index of an item in an list" - assert $ findLastIndex (/= 1) (toList [2, 1, 2]) == Just 2 - assert $ findLastIndex (== 3) (toList [2, 1, 2]) == Nothing + assert $ findLastIndex (/= 1) (l [2, 1, 2]) == Just 2 + assert $ findLastIndex (== 3) (l [2, 1, 2]) == Nothing log "insertAt should add an item at the specified index" - assert $ (insertAt 0 1 (toList [2, 3])) == Just (toList [1, 2, 3]) - assert $ (insertAt 1 1 (toList [2, 3])) == Just (toList [2, 1, 3]) - assert $ (insertAt 2 1 (toList [2, 3])) == Just (toList [2, 3, 1]) + assert $ (insertAt 0 1 (l [2, 3])) == Just (l [1, 2, 3]) + assert $ (insertAt 1 1 (l [2, 3])) == Just (l [2, 1, 3]) + assert $ (insertAt 2 1 (l [2, 3])) == Just (l [2, 3, 1]) log "insertAt should return Nothing if the index is out of range" assert $ (insertAt 2 1 nil) == Nothing log "deleteAt should remove an item at the specified index" - assert $ (deleteAt 0 (toList [1, 2, 3])) == Just (toList [2, 3]) - assert $ (deleteAt 1 (toList [1, 2, 3])) == Just (toList [1, 3]) + assert $ (deleteAt 0 (l [1, 2, 3])) == Just (l [2, 3]) + assert $ (deleteAt 1 (l [1, 2, 3])) == Just (l [1, 3]) log "deleteAt should return Nothing if the index is out of range" assert $ (deleteAt 1 nil) == Nothing log "updateAt should replace an item at the specified index" - assert $ (updateAt 0 9 (toList [1, 2, 3])) == Just (toList [9, 2, 3]) - assert $ (updateAt 1 9 (toList [1, 2, 3])) == Just (toList [1, 9, 3]) + assert $ (updateAt 0 9 (l [1, 2, 3])) == Just (l [9, 2, 3]) + assert $ (updateAt 1 9 (l [1, 2, 3])) == Just (l [1, 9, 3]) log "updateAt should return Nothing if the index is out of range" assert $ (updateAt 1 9 nil) == Nothing log "modifyAt should update an item at the specified index" - assert $ (modifyAt 0 (+ 1) (toList [1, 2, 3])) == Just (toList [2, 2, 3]) - assert $ (modifyAt 1 (+ 1) (toList [1, 2, 3])) == Just (toList [1, 3, 3]) + assert $ (modifyAt 0 (+ 1) (l [1, 2, 3])) == Just (l [2, 2, 3]) + assert $ (modifyAt 1 (+ 1) (l [1, 2, 3])) == Just (l [1, 3, 3]) log "modifyAt should return Nothing if the index is out of range" assert $ (modifyAt 1 (+ 1) nil) == Nothing log "alterAt should update an item at the specified index when the function returns Just" - assert $ (alterAt 0 (Just <<< (+ 1)) (toList [1, 2, 3])) == Just (toList [2, 2, 3]) - assert $ (alterAt 1 (Just <<< (+ 1)) (toList [1, 2, 3])) == Just (toList [1, 3, 3]) + assert $ (alterAt 0 (Just <<< (+ 1)) (l [1, 2, 3])) == Just (l [2, 2, 3]) + assert $ (alterAt 1 (Just <<< (+ 1)) (l [1, 2, 3])) == Just (l [1, 3, 3]) log "alterAt should drop an item at the specified index when the function returns Nothing" - assert $ (alterAt 0 (const Nothing) (toList [1, 2, 3])) == Just (toList [2, 3]) - assert $ (alterAt 1 (const Nothing) (toList [1, 2, 3])) == Just (toList [1, 3]) + assert $ (alterAt 0 (const Nothing) (l [1, 2, 3])) == Just (l [2, 3]) + assert $ (alterAt 1 (const Nothing) (l [1, 2, 3])) == Just (l [1, 3]) log "alterAt should return Nothing if the index is out of range" assert $ (alterAt 1 (Just <<< (+ 1)) nil) == Nothing log "reverse should reverse the order of items in an list" - assert $ (reverse (toList [1, 2, 3])) == toList [3, 2, 1] + assert $ (reverse (l [1, 2, 3])) == l [3, 2, 1] assert $ (reverse nil) == nil log "concat should join an list of lists" - assert $ (concat (toList [toList [1, 2], toList [3, 4]])) == toList [1, 2, 3, 4] - assert $ (concat (toList [toList [1], nil])) == toList [1] - assert $ (concat (toList [nil, nil])) == nil + assert $ (concat (l [l [1, 2], l [3, 4]])) == l [1, 2, 3, 4] + assert $ (concat (l [l [1], nil])) == l [1] + assert $ (concat (l [nil, nil])) == nil log "concatMap should be equivalent to (concat <<< map)" - assert $ concatMap doubleAndOrig (toList [1, 2, 3]) == concat (map doubleAndOrig (toList [1, 2, 3])) + assert $ concatMap doubleAndOrig (l [1, 2, 3]) == concat (map doubleAndOrig (l [1, 2, 3])) log "filter should remove items that don't match a predicate" - assert $ filter odd (range 0 10) == toList [1, 3, 5, 7, 9] + assert $ filter odd (range 0 10) == l [1, 3, 5, 7, 9] log "filterM should remove items that don't match a predicate while using a monadic behaviour" - assert $ filterM (Just <<< odd) (range 0 10) == Just (toList [1, 3, 5, 7, 9]) + assert $ filterM (Just <<< odd) (range 0 10) == Just (l [1, 3, 5, 7, 9]) assert $ filterM (const Nothing) (range 0 10) == Nothing log "mapMaybe should transform every item in an list, throwing out Nothing values" - assert $ mapMaybe (\x -> if x /= 0 then Just x else Nothing) (toList [0, 1, 0, 0, 2, 3]) == toList [1, 2, 3] + assert $ mapMaybe (\x -> if x /= 0 then Just x else Nothing) (l [0, 1, 0, 0, 2, 3]) == l [1, 2, 3] log "catMaybe should take an list of Maybe values and throw out Nothings" - assert $ catMaybes (toList [Nothing, Just 2, Nothing, Just 4]) == toList [2, 4] + assert $ catMaybes (l [Nothing, Just 2, Nothing, Just 4]) == l [2, 4] log "sort should reorder a list into ascending order based on the result of compare" - assert $ sort (toList [1, 3, 2, 5, 6, 4]) == toList [1, 2, 3, 4, 5, 6] + assert $ sort (l [1, 3, 2, 5, 6, 4]) == l [1, 2, 3, 4, 5, 6] log "sortBy should reorder a list into ascending order based on the result of a comparison function" - assert $ sortBy (flip compare) (toList [1, 3, 2, 5, 6, 4]) == toList [6, 5, 4, 3, 2, 1] + assert $ sortBy (flip compare) (l [1, 3, 2, 5, 6, 4]) == l [6, 5, 4, 3, 2, 1] log "take should keep the specified number of items from the front of an list, discarding the rest" - assert $ (take 1 (toList [1, 2, 3])) == toList [1] - assert $ (take 2 (toList [1, 2, 3])) == toList [1, 2] + assert $ (take 1 (l [1, 2, 3])) == l [1] + assert $ (take 2 (l [1, 2, 3])) == l [1, 2] assert $ (take 1 nil) == nil log "takeWhile should keep all values that match a predicate from the front of an list" - assert $ (takeWhile (/= 2) (toList [1, 2, 3])) == toList [1] - assert $ (takeWhile (/= 3) (toList [1, 2, 3])) == toList [1, 2] + assert $ (takeWhile (/= 2) (l [1, 2, 3])) == l [1] + assert $ (takeWhile (/= 3) (l [1, 2, 3])) == l [1, 2] assert $ (takeWhile (/= 1) nil) == nil log "drop should remove the specified number of items from the front of an list" - assert $ (drop 1 (toList [1, 2, 3])) == toList [2, 3] - assert $ (drop 2 (toList [1, 2, 3])) == toList [3] + assert $ (drop 1 (l [1, 2, 3])) == l [2, 3] + assert $ (drop 2 (l [1, 2, 3])) == l [3] assert $ (drop 1 nil) == nil log "dropWhile should remove all values that match a predicate from the front of an list" - assert $ (dropWhile (/= 1) (toList [1, 2, 3])) == toList [1, 2, 3] - assert $ (dropWhile (/= 2) (toList [1, 2, 3])) == toList [2, 3] + assert $ (dropWhile (/= 1) (l [1, 2, 3])) == l [1, 2, 3] + assert $ (dropWhile (/= 2) (l [1, 2, 3])) == l [2, 3] assert $ (dropWhile (/= 1) nil) == nil log "span should split an list in two based on a predicate" - let spanResult = span (< 4) (toList [1, 2, 3, 4, 5, 6, 7]) - assert $ spanResult.init == toList [1, 2, 3] - assert $ spanResult.rest == toList [4, 5, 6, 7] + let spanResult = span (< 4) (l [1, 2, 3, 4, 5, 6, 7]) + assert $ spanResult.init == l [1, 2, 3] + assert $ spanResult.rest == l [4, 5, 6, 7] log "group should group consecutive equal elements into lists" - assert $ group (toList [1, 2, 2, 3, 3, 3, 1]) == toList [toList [1], toList [2, 2], toList [3, 3, 3], toList [1]] + assert $ group (l [1, 2, 2, 3, 3, 3, 1]) == l [l [1], l [2, 2], l [3, 3, 3], l [1]] log "group' should sort then group consecutive equal elements into lists" - assert $ group' (toList [1, 2, 2, 3, 3, 3, 1]) == toList [toList [1, 1], toList [2, 2], toList [3, 3, 3]] + assert $ group' (l [1, 2, 2, 3, 3, 3, 1]) == l [l [1, 1], l [2, 2], l [3, 3, 3]] log "groupBy should group consecutive equal elements into lists based on an equivalence relation" - assert $ groupBy (\x y -> odd x && odd y) (toList [1, 1, 2, 2, 3, 3]) == toList [toList [1, 1], toList [2], toList [2], toList [3, 3]] + assert $ groupBy (\x y -> odd x && odd y) (l [1, 1, 2, 2, 3, 3]) == l [l [1, 1], l [2], l [2], l [3, 3]] log "nub should remove duplicate elements from the list, keeping the first occurence" - assert $ nub (toList [1, 2, 2, 3, 4, 1]) == toList [1, 2, 3, 4] + assert $ nub (l [1, 2, 2, 3, 4, 1]) == l [1, 2, 3, 4] log "nubBy should remove duplicate items from the list using a supplied predicate" let nubPred = \x y -> if odd x then false else x == y - assert $ nubBy nubPred (toList [1, 2, 2, 3, 3, 4, 4, 1]) == toList [1, 2, 3, 3, 4, 1] + assert $ nubBy nubPred (l [1, 2, 2, 3, 3, 4, 4, 1]) == l [1, 2, 3, 3, 4, 1] log "union should produce the union of two lists" - assert $ union (toList [1, 2, 3]) (toList [2, 3, 4]) == toList [1, 2, 3, 4] - assert $ union (toList [1, 1, 2, 3]) (toList [2, 3, 4]) == toList [1, 1, 2, 3, 4] + assert $ union (l [1, 2, 3]) (l [2, 3, 4]) == l [1, 2, 3, 4] + assert $ union (l [1, 1, 2, 3]) (l [2, 3, 4]) == l [1, 1, 2, 3, 4] log "unionBy should produce the union of two lists using the specified equality relation" - assert $ unionBy (\_ y -> y < 5) (toList [1, 2, 3]) (toList [2, 3, 4, 5, 6]) == toList [1, 2, 3, 5, 6] + assert $ unionBy (\_ y -> y < 5) (l [1, 2, 3]) (l [2, 3, 4, 5, 6]) == l [1, 2, 3, 5, 6] log "delete should remove the first matching item from an list" - assert $ delete 1 (toList [1, 2, 1]) == toList [2, 1] - assert $ delete 2 (toList [1, 2, 1]) == toList [1, 1] + assert $ delete 1 (l [1, 2, 1]) == l [2, 1] + assert $ delete 2 (l [1, 2, 1]) == l [1, 1] log "deleteBy should remove the first equality-relation-matching item from an list" - assert $ deleteBy (/=) 2 (toList [1, 2, 1]) == toList [2, 1] - assert $ deleteBy (/=) 1 (toList [1, 2, 1]) == toList [1, 1] + assert $ deleteBy (/=) 2 (l [1, 2, 1]) == l [2, 1] + assert $ deleteBy (/=) 1 (l [1, 2, 1]) == l [1, 1] log "(\\\\) should return the difference between two lists" - assert $ toList [1, 2, 3, 4, 3, 2, 1] \\ toList [1, 1, 2, 3] == toList [4, 3, 2] + assert $ l [1, 2, 3, 4, 3, 2, 1] \\ l [1, 1, 2, 3] == l [4, 3, 2] log "intersect should return the intersection of two lists" - assert $ intersect (toList [1, 2, 3, 4, 3, 2, 1]) (toList [1, 1, 2, 3]) == toList [1, 2, 3, 3, 2, 1] + assert $ intersect (l [1, 2, 3, 4, 3, 2, 1]) (l [1, 1, 2, 3]) == l [1, 2, 3, 3, 2, 1] log "intersectBy should return the intersection of two lists using the specified equivalence relation" - assert $ intersectBy (\x y -> (x * 2) == y) (toList [1, 2, 3]) (toList [2, 6]) == toList [1, 3] + assert $ intersectBy (\x y -> (x * 2) == y) (l [1, 2, 3]) (l [2, 6]) == l [1, 3] log "zipWith should use the specified function to zip two lists together" - assert $ zipWith (\x y -> toList [show x, y]) (toList [1, 2, 3]) (toList ["a", "b", "c"]) == toList [toList ["1", "a"], toList ["2", "b"], toList ["3", "c"]] + assert $ zipWith (\x y -> l [show x, y]) (l [1, 2, 3]) (l ["a", "b", "c"]) == l [l ["1", "a"], l ["2", "b"], l ["3", "c"]] log "zipWithA should use the specified function to zip two lists together" - assert $ zipWithA (\x y -> Just $ Tuple x y) (toList [1, 2, 3]) (toList ["a", "b", "c"]) == Just (toList [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"]) + assert $ zipWithA (\x y -> Just $ Tuple x y) (l [1, 2, 3]) (l ["a", "b", "c"]) == Just (l [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"]) log "zip should use the specified function to zip two lists together" - assert $ zip (toList [1, 2, 3]) (toList ["a", "b", "c"]) == toList [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"] + assert $ zip (l [1, 2, 3]) (l ["a", "b", "c"]) == l [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"] log "unzip should deconstruct a list of tuples into a tuple of lists" - assert $ unzip (toList [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"]) == Tuple (toList [1, 2, 3]) (toList ["a", "b", "c"]) + assert $ unzip (l [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"]) == Tuple (l [1, 2, 3]) (l ["a", "b", "c"]) log "foldM should perform a fold using a monadic step function" assert $ foldM (\x y -> Just (x + y)) 0 (range 1 10) == Just 55 diff --git a/test/Test/Data/List/Lazy.purs b/test/Test/Data/List/Lazy.purs index 492dfe3..2589b48 100644 --- a/test/Test/Data/List/Lazy.purs +++ b/test/Test/Data/List/Lazy.purs @@ -11,76 +11,77 @@ import Test.Assert (ASSERT(), assert) testListLazy :: forall eff. Eff (assert :: ASSERT, console :: CONSOLE | eff) Unit testListLazy = do + let l = fromFoldable log "singleton should construct an list with a single value" - assert $ singleton 1 == toList [1] - assert $ singleton "foo" == toList ["foo"] - assert $ singleton nil' == toList [toList []] + assert $ singleton 1 == l [1] + assert $ singleton "foo" == l ["foo"] + assert $ singleton nil' == l [l []] log "range should create an inclusive list of integers for the specified start and end" - assert $ (range 0 5) == toList [0, 1, 2, 3, 4, 5] - assert $ (range 2 (-3)) == toList [2, 1, 0, -1, -2, -3] + assert $ (range 0 5) == l [0, 1, 2, 3, 4, 5] + assert $ (range 2 (-3)) == l [2, 1, 0, -1, -2, -3] -- log "replicate should produce an list containg an item a specified number of times" - -- assert $ replicate 3 true == toList [true, true, true] - -- assert $ replicate 1 "foo" == toList ["foo"] - -- assert $ replicate 0 "foo" == toList [] - -- assert $ replicate (-1) "foo" == toList [] + -- assert $ replicate 3 true == l [true, true, true] + -- assert $ replicate 1 "foo" == l ["foo"] + -- assert $ replicate 0 "foo" == l [] + -- assert $ replicate (-1) "foo" == l [] -- log "replicateM should perform the monadic action the correct number of times" - -- assert $ replicateM 3 (Just 1) == Just (toList [1, 1, 1]) - -- assert $ replicateM 1 (Just 1) == Just (toList [1]) - -- assert $ replicateM 0 (Just 1) == Just (toList []) - -- assert $ replicateM (-1) (Just 1) == Just (toList []) + -- assert $ replicateM 3 (Just 1) == Just (l [1, 1, 1]) + -- assert $ replicateM 1 (Just 1) == Just (l [1]) + -- assert $ replicateM 0 (Just 1) == Just (l []) + -- assert $ replicateM (-1) (Just 1) == Just (l []) -- some -- many log "null should return false for non-empty lists" - assert $ null (toList [1]) == false - assert $ null (toList [1, 2, 3]) == false + assert $ null (l [1]) == false + assert $ null (l [1, 2, 3]) == false log "null should return true for an empty list" assert $ null nil' == true log "length should return the number of items in an list" assert $ length nil' == 0 - assert $ length (toList [1]) == 1 - assert $ length (toList [1, 2, 3, 4, 5]) == 5 + assert $ length (l [1]) == 1 + assert $ length (l [1, 2, 3, 4, 5]) == 5 -- log "snoc should add an item to the end of an list" - -- assert $ toList [1, 2, 3] `snoc` 4 == toList [1, 2, 3, 4] - -- assert $ nil' `snoc` 1 == toList [1] + -- assert $ l [1, 2, 3] `snoc` 4 == l [1, 2, 3, 4] + -- assert $ nil' `snoc` 1 == l [1] log "insert should add an item at the appropriate place in a sorted list" - assert $ insert 1.5 (toList [1.0, 2.0, 3.0]) == toList [1.0, 1.5, 2.0, 3.0] - assert $ insert 4 (toList [1, 2, 3]) == toList [1, 2, 3, 4] - assert $ insert 0 (toList [1, 2, 3]) == toList [0, 1, 2, 3] + assert $ insert 1.5 (l [1.0, 2.0, 3.0]) == l [1.0, 1.5, 2.0, 3.0] + assert $ insert 4 (l [1, 2, 3]) == l [1, 2, 3, 4] + assert $ insert 0 (l [1, 2, 3]) == l [0, 1, 2, 3] log "insertBy should add an item at the appropriate place in a sorted list using the specified comparison" - assert $ insertBy (flip compare) 4 (toList [1, 2, 3]) == toList [4, 1, 2, 3] - assert $ insertBy (flip compare) 0 (toList [1, 2, 3]) == toList [1, 2, 3, 0] + assert $ insertBy (flip compare) 4 (l [1, 2, 3]) == l [4, 1, 2, 3] + assert $ insertBy (flip compare) 0 (l [1, 2, 3]) == l [1, 2, 3, 0] log "head should return a Just-wrapped first value of a non-empty list" - assert $ head (toList ["foo", "bar"]) == Just "foo" + assert $ head (l ["foo", "bar"]) == Just "foo" log "head should return Nothing for an empty list" assert $ head nil' == Nothing log "last should return a Just-wrapped last value of a non-empty list" - assert $ last (toList ["foo", "bar"]) == Just "bar" + assert $ last (l ["foo", "bar"]) == Just "bar" log "last should return Nothing for an empty list" assert $ last nil' == Nothing log "tail should return a Just-wrapped list containing all the items in an list apart from the first for a non-empty list" - assert $ tail (toList ["foo", "bar", "baz"]) == Just (toList ["bar", "baz"]) + assert $ tail (l ["foo", "bar", "baz"]) == Just (l ["bar", "baz"]) log "tail should return Nothing for an empty list" assert $ tail nil' == Nothing log "init should return a Just-wrapped list containing all the items in an list apart from the first for a non-empty list" - assert $ init (toList ["foo", "bar", "baz"]) == Just (toList ["foo", "bar"]) + assert $ init (l ["foo", "bar", "baz"]) == Just (l ["foo", "bar"]) log "init should return Nothing for an empty list" assert $ init nil' == Nothing @@ -89,170 +90,170 @@ testListLazy = do assert $ isNothing (uncons nil') log "uncons should split an list into a head and tail record when there is at least one item" - let u1 = uncons (toList [1]) + let u1 = uncons (l [1]) assert $ (fromJust u1).head == 1 - assert $ (fromJust u1).tail == toList [] - let u2 = uncons (toList [1, 2, 3]) + assert $ (fromJust u1).tail == l [] + let u2 = uncons (l [1, 2, 3]) assert $ (fromJust u2).head == 1 - assert $ (fromJust u2).tail == toList [2, 3] + assert $ (fromJust u2).tail == l [2, 3] log "(!!) should return Just x when the index is within the bounds of the list" - assert $ toList [1, 2, 3] !! 0 == (Just 1) - assert $ toList [1, 2, 3] !! 1 == (Just 2) - assert $ toList [1, 2, 3] !! 2 == (Just 3) + assert $ l [1, 2, 3] !! 0 == (Just 1) + assert $ l [1, 2, 3] !! 1 == (Just 2) + assert $ l [1, 2, 3] !! 2 == (Just 3) log "(!!) should return Nothing when the index is outside of the bounds of the list" - assert $ toList [1, 2, 3] !! 6 == Nothing - assert $ toList [1, 2, 3] !! (-1) == Nothing + assert $ l [1, 2, 3] !! 6 == Nothing + assert $ l [1, 2, 3] !! (-1) == Nothing -- log "elemIndex should return the index of an item that a predicate returns true for in an list" - -- assert $ elemIndex 1 (toList [1, 2, 1]) == Just 0 - -- assert $ elemIndex 4 (toList [1, 2, 1]) == Nothing + -- assert $ elemIndex 1 (l [1, 2, 1]) == Just 0 + -- assert $ elemIndex 4 (l [1, 2, 1]) == Nothing -- log "elemLastIndex should return the last index of an item in an list" - -- assert $ elemLastIndex 1 (toList [1, 2, 1]) == Just 2 - -- assert $ elemLastIndex 4 (toList [1, 2, 1]) == Nothing + -- assert $ elemLastIndex 1 (l [1, 2, 1]) == Just 2 + -- assert $ elemLastIndex 4 (l [1, 2, 1]) == Nothing -- log "findIndex should return the index of an item that a predicate returns true for in an list" - -- assert $ findIndex (/= 1) (toList [1, 2, 1]) == Just 1 - -- assert $ findIndex (== 3) (toList [1, 2, 1]) == Nothing + -- assert $ findIndex (/= 1) (l [1, 2, 1]) == Just 1 + -- assert $ findIndex (== 3) (l [1, 2, 1]) == Nothing -- log "findLastIndex should return the last index of an item in an list" - -- assert $ findLastIndex (/= 1) (toList [2, 1, 2]) == Just 2 - -- assert $ findLastIndex (== 3) (toList [2, 1, 2]) == Nothing + -- assert $ findLastIndex (/= 1) (l [2, 1, 2]) == Just 2 + -- assert $ findLastIndex (== 3) (l [2, 1, 2]) == Nothing log "insertAt should add an item at the specified index" - assert $ (insertAt 0 1 (toList [2, 3])) == (toList [1, 2, 3]) - assert $ (insertAt 1 1 (toList [2, 3])) == (toList [2, 1, 3]) - assert $ (insertAt 2 1 (toList [2, 3])) == (toList [2, 3, 1]) + assert $ (insertAt 0 1 (l [2, 3])) == (l [1, 2, 3]) + assert $ (insertAt 1 1 (l [2, 3])) == (l [2, 1, 3]) + assert $ (insertAt 2 1 (l [2, 3])) == (l [2, 3, 1]) log "deleteAt should remove an item at the specified index" - assert $ (deleteAt 0 (toList [1, 2, 3])) == (toList [2, 3]) - assert $ (deleteAt 1 (toList [1, 2, 3])) == (toList [1, 3]) + assert $ (deleteAt 0 (l [1, 2, 3])) == (l [2, 3]) + assert $ (deleteAt 1 (l [1, 2, 3])) == (l [1, 3]) log "updateAt should replace an item at the specified index" - assert $ (updateAt 0 9 (toList [1, 2, 3])) == (toList [9, 2, 3]) - assert $ (updateAt 1 9 (toList [1, 2, 3])) == (toList [1, 9, 3]) + assert $ (updateAt 0 9 (l [1, 2, 3])) == (l [9, 2, 3]) + assert $ (updateAt 1 9 (l [1, 2, 3])) == (l [1, 9, 3]) log "modifyAt should update an item at the specified index" - assert $ (modifyAt 0 (+ 1) (toList [1, 2, 3])) == (toList [2, 2, 3]) - assert $ (modifyAt 1 (+ 1) (toList [1, 2, 3])) == (toList [1, 3, 3]) + assert $ (modifyAt 0 (+ 1) (l [1, 2, 3])) == (l [2, 2, 3]) + assert $ (modifyAt 1 (+ 1) (l [1, 2, 3])) == (l [1, 3, 3]) log "alterAt should update an item at the specified index when the function returns Just" - assert $ (alterAt 0 (Just <<< (+ 1)) (toList [1, 2, 3])) == (toList [2, 2, 3]) - assert $ (alterAt 1 (Just <<< (+ 1)) (toList [1, 2, 3])) == (toList [1, 3, 3]) + assert $ (alterAt 0 (Just <<< (+ 1)) (l [1, 2, 3])) == (l [2, 2, 3]) + assert $ (alterAt 1 (Just <<< (+ 1)) (l [1, 2, 3])) == (l [1, 3, 3]) log "alterAt should drop an item at the specified index when the function returns Nothing" - assert $ (alterAt 0 (const Nothing) (toList [1, 2, 3])) == (toList [2, 3]) - assert $ (alterAt 1 (const Nothing) (toList [1, 2, 3])) == (toList [1, 3]) + assert $ (alterAt 0 (const Nothing) (l [1, 2, 3])) == (l [2, 3]) + assert $ (alterAt 1 (const Nothing) (l [1, 2, 3])) == (l [1, 3]) log "reverse should reverse the order of items in an list" - assert $ (reverse (toList [1, 2, 3])) == toList [3, 2, 1] + assert $ (reverse (l [1, 2, 3])) == l [3, 2, 1] assert $ (reverse nil') == nil' log "concat should join an list of lists" - assert $ (concat (toList [toList [1, 2], toList [3, 4]])) == toList [1, 2, 3, 4] - assert $ (concat (toList [toList [1], nil'])) == toList [1] - assert $ (concat (toList [nil', nil'])) == nil' + assert $ (concat (l [l [1, 2], l [3, 4]])) == l [1, 2, 3, 4] + assert $ (concat (l [l [1], nil'])) == l [1] + assert $ (concat (l [nil', nil'])) == nil' log "concatMap should be equivalent to (concat <<< map)" - assert $ concatMap doubleAndOrig (toList [1, 2, 3]) == concat (map doubleAndOrig (toList [1, 2, 3])) + assert $ concatMap doubleAndOrig (l [1, 2, 3]) == concat (map doubleAndOrig (l [1, 2, 3])) log "filter should remove items that don't match a predicate" - assert $ filter odd (range 0 10) == toList [1, 3, 5, 7, 9] + assert $ filter odd (range 0 10) == l [1, 3, 5, 7, 9] -- log "filterM should remove items that don't match a predicate while using a monadic behaviour" - -- assert $ filterM (Just <<< odd) (range 0 10) == Just (toList [1, 3, 5, 7, 9]) + -- assert $ filterM (Just <<< odd) (range 0 10) == Just (l [1, 3, 5, 7, 9]) -- assert $ filterM (const Nothing) (range 0 10) == Nothing log "mapMaybe should transform every item in an list, throwing out Nothing values" - assert $ mapMaybe (\x -> if x /= 0 then Just x else Nothing) (toList [0, 1, 0, 0, 2, 3]) == toList [1, 2, 3] + assert $ mapMaybe (\x -> if x /= 0 then Just x else Nothing) (l [0, 1, 0, 0, 2, 3]) == l [1, 2, 3] log "catMaybe should take an list of Maybe values and throw out Nothings" - assert $ catMaybes (toList [Nothing, Just 2, Nothing, Just 4]) == toList [2, 4] + assert $ catMaybes (l [Nothing, Just 2, Nothing, Just 4]) == l [2, 4] -- log "sort should reorder a list into ascending order based on the result of compare" - -- assert $ sort (toList [1, 3, 2, 5, 6, 4]) == toList [1, 2, 3, 4, 5, 6] + -- assert $ sort (l [1, 3, 2, 5, 6, 4]) == l [1, 2, 3, 4, 5, 6] -- log "sortBy should reorder a list into ascending order based on the result of a comparison function" - -- assert $ sortBy (flip compare) (toList [1, 3, 2, 5, 6, 4]) == toList [6, 5, 4, 3, 2, 1] + -- assert $ sortBy (flip compare) (l [1, 3, 2, 5, 6, 4]) == l [6, 5, 4, 3, 2, 1] log "take should keep the specified number of items from the front of an list, discarding the rest" - assert $ (take 1 (toList [1, 2, 3])) == toList [1] - assert $ (take 2 (toList [1, 2, 3])) == toList [1, 2] + assert $ (take 1 (l [1, 2, 3])) == l [1] + assert $ (take 2 (l [1, 2, 3])) == l [1, 2] assert $ (take 1 nil') == nil' log "takeWhile should keep all values that match a predicate from the front of an list" - assert $ (takeWhile (/= 2) (toList [1, 2, 3])) == toList [1] - assert $ (takeWhile (/= 3) (toList [1, 2, 3])) == toList [1, 2] + assert $ (takeWhile (/= 2) (l [1, 2, 3])) == l [1] + assert $ (takeWhile (/= 3) (l [1, 2, 3])) == l [1, 2] assert $ (takeWhile (/= 1) nil') == nil' log "drop should remove the specified number of items from the front of an list" - assert $ (drop 1 (toList [1, 2, 3])) == toList [2, 3] - assert $ (drop 2 (toList [1, 2, 3])) == toList [3] + assert $ (drop 1 (l [1, 2, 3])) == l [2, 3] + assert $ (drop 2 (l [1, 2, 3])) == l [3] assert $ (drop 1 nil') == nil' log "dropWhile should remove all values that match a predicate from the front of an list" - assert $ (dropWhile (/= 1) (toList [1, 2, 3])) == toList [1, 2, 3] - assert $ (dropWhile (/= 2) (toList [1, 2, 3])) == toList [2, 3] + assert $ (dropWhile (/= 1) (l [1, 2, 3])) == l [1, 2, 3] + assert $ (dropWhile (/= 2) (l [1, 2, 3])) == l [2, 3] assert $ (dropWhile (/= 1) nil') == nil' log "span should split an list in two based on a predicate" - let spanResult = span (< 4) (toList [1, 2, 3, 4, 5, 6, 7]) - assert $ spanResult.init == toList [1, 2, 3] - assert $ spanResult.rest == toList [4, 5, 6, 7] + let spanResult = span (< 4) (l [1, 2, 3, 4, 5, 6, 7]) + assert $ spanResult.init == l [1, 2, 3] + assert $ spanResult.rest == l [4, 5, 6, 7] log "group should group consecutive equal elements into lists" - assert $ group (toList [1, 2, 2, 3, 3, 3, 1]) == toList [toList [1], toList [2, 2], toList [3, 3, 3], toList [1]] + assert $ group (l [1, 2, 2, 3, 3, 3, 1]) == l [l [1], l [2, 2], l [3, 3, 3], l [1]] -- log "group' should sort then group consecutive equal elements into lists" - -- assert $ group' (toList [1, 2, 2, 3, 3, 3, 1]) == toList [toList [1, 1], toList [2, 2], toList [3, 3, 3]] + -- assert $ group' (l [1, 2, 2, 3, 3, 3, 1]) == l [l [1, 1], l [2, 2], l [3, 3, 3]] log "groupBy should group consecutive equal elements into lists based on an equivalence relation" - assert $ groupBy (\x y -> odd x && odd y) (toList [1, 1, 2, 2, 3, 3]) == toList [toList [1, 1], toList [2], toList [2], toList [3, 3]] + assert $ groupBy (\x y -> odd x && odd y) (l [1, 1, 2, 2, 3, 3]) == l [l [1, 1], l [2], l [2], l [3, 3]] log "nub should remove duplicate elements from the list, keeping the first occurence" - assert $ nub (toList [1, 2, 2, 3, 4, 1]) == toList [1, 2, 3, 4] + assert $ nub (l [1, 2, 2, 3, 4, 1]) == l [1, 2, 3, 4] log "nubBy should remove duplicate items from the list using a supplied predicate" let nubPred = \x y -> if odd x then false else x == y - assert $ nubBy nubPred (toList [1, 2, 2, 3, 3, 4, 4, 1]) == toList [1, 2, 3, 3, 4, 1] + assert $ nubBy nubPred (l [1, 2, 2, 3, 3, 4, 4, 1]) == l [1, 2, 3, 3, 4, 1] log "union should produce the union of two lists" - assert $ union (toList [1, 2, 3]) (toList [2, 3, 4]) == toList [1, 2, 3, 4] - assert $ union (toList [1, 1, 2, 3]) (toList [2, 3, 4]) == toList [1, 1, 2, 3, 4] + assert $ union (l [1, 2, 3]) (l [2, 3, 4]) == l [1, 2, 3, 4] + assert $ union (l [1, 1, 2, 3]) (l [2, 3, 4]) == l [1, 1, 2, 3, 4] log "unionBy should produce the union of two lists using the specified equality relation" - assert $ unionBy (\_ y -> y < 5) (toList [1, 2, 3]) (toList [2, 3, 4, 5, 6]) == toList [1, 2, 3, 5, 6] + assert $ unionBy (\_ y -> y < 5) (l [1, 2, 3]) (l [2, 3, 4, 5, 6]) == l [1, 2, 3, 5, 6] log "delete should remove the first matching item from an list" - assert $ delete 1 (toList [1, 2, 1]) == toList [2, 1] - assert $ delete 2 (toList [1, 2, 1]) == toList [1, 1] + assert $ delete 1 (l [1, 2, 1]) == l [2, 1] + assert $ delete 2 (l [1, 2, 1]) == l [1, 1] log "deleteBy should remove the first equality-relation-matching item from an list" - assert $ deleteBy (/=) 2 (toList [1, 2, 1]) == toList [2, 1] - assert $ deleteBy (/=) 1 (toList [1, 2, 1]) == toList [1, 1] + assert $ deleteBy (/=) 2 (l [1, 2, 1]) == l [2, 1] + assert $ deleteBy (/=) 1 (l [1, 2, 1]) == l [1, 1] log "(\\\\) should return the difference between two lists" - assert $ toList [1, 2, 3, 4, 3, 2, 1] \\ toList [1, 1, 2, 3] == toList [4, 3, 2] + assert $ l [1, 2, 3, 4, 3, 2, 1] \\ l [1, 1, 2, 3] == l [4, 3, 2] log "intersect should return the intersection of two lists" - assert $ intersect (toList [1, 2, 3, 4, 3, 2, 1]) (toList [1, 1, 2, 3]) == toList [1, 2, 3, 3, 2, 1] + assert $ intersect (l [1, 2, 3, 4, 3, 2, 1]) (l [1, 1, 2, 3]) == l [1, 2, 3, 3, 2, 1] log "intersectBy should return the intersection of two lists using the specified equivalence relation" - assert $ intersectBy (\x y -> (x * 2) == y) (toList [1, 2, 3]) (toList [2, 6]) == toList [1, 3] + assert $ intersectBy (\x y -> (x * 2) == y) (l [1, 2, 3]) (l [2, 6]) == l [1, 3] log "zipWith should use the specified function to zip two lists together" - assert $ zipWith (\x y -> toList [show x, y]) (toList [1, 2, 3]) (toList ["a", "b", "c"]) == toList [toList ["1", "a"], toList ["2", "b"], toList ["3", "c"]] + assert $ zipWith (\x y -> l [show x, y]) (l [1, 2, 3]) (l ["a", "b", "c"]) == l [l ["1", "a"], l ["2", "b"], l ["3", "c"]] -- log "zipWithA should use the specified function to zip two lists together" - -- assert $ zipWithA (\x y -> Just $ Tuple x y) (toList [1, 2, 3]) (toList ["a", "b", "c"]) == Just (toList [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"]) + -- assert $ zipWithA (\x y -> Just $ Tuple x y) (l [1, 2, 3]) (l ["a", "b", "c"]) == Just (l [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"]) log "zip should use the specified function to zip two lists together" - assert $ zip (toList [1, 2, 3]) (toList ["a", "b", "c"]) == toList [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"] + assert $ zip (l [1, 2, 3]) (l ["a", "b", "c"]) == l [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"] -- log "unzip should deconstruct a list of tuples into a tuple of lists" - -- assert $ unzip (toList [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"]) == Tuple (toList [1, 2, 3]) (toList ["a", "b", "c"]) + -- assert $ unzip (l [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"]) == Tuple (l [1, 2, 3]) (l ["a", "b", "c"]) -- log "foldM should perform a fold using a monadic step function" -- assert $ foldM (\x y -> Just (x + y)) 0 (range 1 10) == Just 55 diff --git a/test/Test/Data/List/Unsafe.purs b/test/Test/Data/List/Unsafe.purs index 66864f8..12ea12d 100644 --- a/test/Test/Data/List/Unsafe.purs +++ b/test/Test/Data/List/Unsafe.purs @@ -3,33 +3,34 @@ module Test.Data.List.Unsafe (testListUnsafe) where import Prelude import Control.Monad.Eff (Eff()) import Control.Monad.Eff.Console (CONSOLE(), log) -import Data.List (List(..), toList) +import Data.List (List(..), fromFoldable) import Data.List.Unsafe import Test.Assert (ASSERT(), assert, assertThrows) testListUnsafe :: forall eff. Eff (assert :: ASSERT, console :: CONSOLE | eff) Unit testListUnsafe = do + let l = fromFoldable log "head should return a Just-wrapped first value of a non-empty list" - assert $ head (toList ["foo", "bar"]) == "foo" + assert $ head (l ["foo", "bar"]) == "foo" log "head should throw an error for an empty list" assertThrows \_ -> head Nil log "last should return a Just-wrapped last value of a non-empty list" - assert $ last (toList ["foo", "bar"]) == "bar" + assert $ last (l ["foo", "bar"]) == "bar" log "last should throw an error for an empty list" assertThrows \_ -> last Nil log "tail should return a Just-wrapped list containing all the items in an list apart from the first for a non-empty list" - assert $ tail (toList ["foo", "bar", "baz"]) == toList ["bar", "baz"] + assert $ tail (l ["foo", "bar", "baz"]) == l ["bar", "baz"] log "tail should throw an error for an empty list" assertThrows \_ -> tail Nil log "init should return a Just-wrapped list containing all the items in an list apart from the first for a non-empty list" - assert $ init (toList ["foo", "bar", "baz"]) == toList ["foo", "bar"] + assert $ init (l ["foo", "bar", "baz"]) == l ["foo", "bar"] log "init should throw an error for an empty list" assertThrows \_ -> init Nil