Skip to content

Commit ab61f68

Browse files
committed
make nubByEq more efficient, part of #71
1 parent d218f6f commit ab61f68

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/Data/Array.purs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -926,10 +926,12 @@ nubBy comp xs = case head indexedAndSorted of
926926
-- | ```
927927
-- |
928928
nubByEq :: forall a. (a -> a -> Boolean) -> Array a -> Array a
929-
nubByEq eq xs =
930-
case uncons xs of
931-
Just o -> o.head : nubByEq eq (filter (\y -> not (o.head `eq` y)) o.tail)
932-
Nothing -> []
929+
nubByEq eq xs = ST.run do
930+
arr <- STA.unsafeThaw []
931+
ST.foreach xs \x -> do
932+
e <- not <<< Exports.any (_ `eq` x) <$> (STA.unsafeFreeze arr)
933+
when e $ void $ STA.push x arr
934+
STA.unsafeFreeze arr
933935

934936
-- | Calculate the union of two arrays. Note that duplicates in the first array
935937
-- | are preserved while duplicates in the second array are removed.

0 commit comments

Comments
 (0)