Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions docs/Data/List.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`

Expand Down Expand Up @@ -661,5 +664,3 @@ second components.
``` purescript
foldM :: forall m a b. (Monad m) => (a -> b -> m a) -> a -> List b -> m a
```


9 changes: 5 additions & 4 deletions docs/Data/List/Lazy.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`

Expand Down Expand Up @@ -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))`


16 changes: 8 additions & 8 deletions src/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Data/List/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion test/Test/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/Test/Data/List/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down