From cb96d7132eb8d4e713861953176833a28fcc80f6 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 15 Sep 2015 15:38:51 +0200 Subject: [PATCH] Update nub / nubBy documentation and unit test to consider element order --- docs/Data/List.md | 9 +++++---- docs/Data/List/Lazy.md | 9 +++++---- src/Data/List.purs | 16 ++++++++-------- src/Data/List/Lazy.purs | 2 +- test/Test/Data/List.purs | 2 +- test/Test/Data/List/Lazy.purs | 2 +- 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/docs/Data/List.md b/docs/Data/List.md index 3663136..71a405b 100644 --- a/docs/Data/List.md +++ b/docs/Data/List.md @@ -536,14 +536,17 @@ Running time: `O(n)` nub :: forall a. (Eq a) => List a -> List a ``` +Special case of `nubBy`: `nubBy eq` + #### `nubBy` ``` purescript nubBy :: forall a. (a -> a -> Boolean) -> List a -> List a ``` -Remove duplicate elements from a list, using the specified -function to determine equality of elements. +Remove duplicate elements from a list, using the specified function to +determine equality of elements. The first occurence of an element is always +the one that is kept. Running time: `O(n^2)` @@ -661,5 +664,3 @@ second components. ``` purescript foldM :: forall m a b. (Monad m) => (a -> b -> m a) -> a -> List b -> m a ``` - - diff --git a/docs/Data/List/Lazy.md b/docs/Data/List/Lazy.md index eee7c65..13432b2 100644 --- a/docs/Data/List/Lazy.md +++ b/docs/Data/List/Lazy.md @@ -482,14 +482,17 @@ Running time: `O(n)` nub :: forall a. (Eq a) => List a -> List a ``` +Special case of `nubBy`: `nubBy eq` + #### `nubBy` ``` purescript nubBy :: forall a. (a -> a -> Boolean) -> List a -> List a ``` -Remove duplicate elements from a list, using the specified -function to determine equality of elements. +Remove duplicate elements from a list, using the specified function to +determine equality of elements. The first occurence of an element is always +the one that is kept. Running time: `O(n^2)` @@ -583,5 +586,3 @@ zip :: forall a b. List a -> List b -> List (Tuple a b) Collect pairs of elements at the same positions in two lists. Running time: `O(min(m, n))` - - diff --git a/src/Data/List.purs b/src/Data/List.purs index c2a6ad2..29b78fc 100644 --- a/src/Data/List.purs +++ b/src/Data/List.purs @@ -256,8 +256,8 @@ init :: forall a. List a -> Maybe (List a) init Nil = Nothing init lst = Just $ reverse $ go lst Nil where - go (Cons x Nil) acc = acc - go (Cons x xs) acc = go xs $ Cons x acc + go (Cons x Nil) acc = acc + go (Cons x xs) acc = go xs $ Cons x acc -- | Break a list into its first element, and the remaining elements, -- | or `Nothing` if the list is empty. @@ -570,7 +570,7 @@ groupBy eq (Cons x xs) = case span (eq x) xs of -- | -- | Running time: `O(n^2)` nub :: forall a. (Eq a) => List a -> List a -nub = nubBy (==) +nub = nubBy eq -- | Remove duplicate elements from a list, using the specified -- | function to determine equality of elements. @@ -652,7 +652,7 @@ zipWith f xs ys = reverse $ go xs ys Nil where go Nil _ acc = acc go _ Nil acc = acc - go (Cons a as) (Cons b bs) acc = go as bs $ Cons (f a b) acc + go (Cons a as) (Cons b bs) acc = go as bs $ Cons (f a b) acc -- | A generalization of `zipWith` which accumulates results in some `Applicative` -- | functor. @@ -690,10 +690,10 @@ instance showList :: (Show a) => Show (List a) where instance eqList :: (Eq a) => Eq (List a) where eq xs ys = go xs ys true where - go _ _ false = false + go _ _ false = false go Nil Nil acc = acc go (Cons x xs) (Cons y ys) acc = go xs ys $ acc && (y == x) - go _ _ _ = false + go _ _ _ = false instance ordList :: (Ord a) => Ord (List a) where @@ -705,7 +705,7 @@ instance ordList :: (Ord a) => Ord (List a) where go (Cons x xs) (Cons y ys) = case compare x y of EQ -> go xs ys - other -> other + other -> other instance semigroupList :: Semigroup (List a) where append Nil ys = ys @@ -718,7 +718,7 @@ instance functorList :: Functor List where map f lst = reverse $ go lst Nil where go Nil acc = acc - go (Cons x xs) acc = go xs $ Cons (f x) acc + go (Cons x xs) acc = go xs $ Cons (f x) acc instance foldableList :: Foldable List where diff --git a/src/Data/List/Lazy.purs b/src/Data/List/Lazy.purs index 0167152..ca7421e 100644 --- a/src/Data/List/Lazy.purs +++ b/src/Data/List/Lazy.purs @@ -527,7 +527,7 @@ groupBy eq xs = List (go <$> runList xs) -- | -- | Running time: `O(n^2)` nub :: forall a. (Eq a) => List a -> List a -nub = nubBy (==) +nub = nubBy eq -- | Remove duplicate elements from a list, using the specified -- | function to determine equality of elements. diff --git a/test/Test/Data/List.purs b/test/Test/Data/List.purs index fd2d0c1..ade78cc 100644 --- a/test/Test/Data/List.purs +++ b/test/Test/Data/List.purs @@ -230,7 +230,7 @@ testList = do 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]] - log "nub should remove duplicate items from the list" + 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] log "nubBy should remove duplicate items from the list using a supplied predicate" diff --git a/test/Test/Data/List/Lazy.purs b/test/Test/Data/List/Lazy.purs index 28e0227..eae38d9 100644 --- a/test/Test/Data/List/Lazy.purs +++ b/test/Test/Data/List/Lazy.purs @@ -209,7 +209,7 @@ testListLazy = do 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]] - log "nub should remove duplicate items from the list" + 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] log "nubBy should remove duplicate items from the list using a supplied predicate"