Skip to content

Commit b649cef

Browse files
matthewleonkritzcreek
authored andcommitted
add nubEq
1 parent 3f0201f commit b649cef

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Data/Array.purs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ module Data.Array
9090
, groupBy
9191

9292
, nub
93+
, nubEq
9394
, nubBy
9495
, union
9596
, unionBy
@@ -891,6 +892,17 @@ nub arr = case head indexedAndSorted of
891892
where
892893
indexedAndSorted = sort $ mapWithIndex (flip Tuple) arr
893894

895+
-- | Remove the duplicates from an array, creating a new array.
896+
-- |
897+
-- | This less efficient version of `nub` only requires an `Eq` instance.
898+
-- |
899+
-- | ```purescript
900+
-- | nub [1, 2, 1, 3, 3] = [1, 2, 3]
901+
-- | ```
902+
-- |
903+
nubEq :: forall a. Eq a => Array a -> Array a
904+
nubEq = nubBy eq
905+
894906
-- | Remove the duplicates from an array, where element equality is determined
895907
-- | by the specified equivalence relation, creating a new array.
896908
-- |

test/Test/Data/Array.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@ testArray = do
312312
log "nub should preserve order"
313313
assert $ A.nub [1, 3, 4, 2, 2, 1] == [1, 3, 4, 2]
314314

315+
log "nubEq should remove duplicate elements from the list, keeping the first occurence"
316+
assert $ A.nubEq [1, 2, 2, 3, 4, 1] == [1, 2, 3, 4]
317+
318+
log "nubEq should preserve order"
319+
assert $ A.nubEq [1, 3, 4, 2, 2, 1] == [1, 3, 4, 2]
320+
315321
log "nubBy should remove duplicate items from the list using a supplied predicate"
316322
let nubPred = \x y -> if odd x then false else x == y
317323
assert $ A.nubBy nubPred [1, 2, 2, 3, 3, 4, 4, 1] == [1, 2, 3, 3, 4, 1]

0 commit comments

Comments
 (0)