Skip to content

Commit 54e5240

Browse files
authored
Merge pull request #81 from purescript/remove-uncons-difference
Speed up `difference`
2 parents 4473953 + d9312d9 commit 54e5240

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/Data/Array.purs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,8 @@ unionBy eq xs ys = xs <> foldl (flip (deleteBy eq)) (nubBy eq ys) xs
580580

581581
-- | Delete the first element of an array which is equal to the specified value,
582582
-- | creating a new array.
583+
-- |
584+
-- | Running time: `O(n)`
583585
delete :: forall a. Eq a => a -> Array a -> Array a
584586
delete = deleteBy eq
585587

@@ -592,10 +594,11 @@ deleteBy eq x ys = maybe ys (\i -> unsafePartial $ fromJust (deleteAt i ys)) (fi
592594

593595
-- | Delete the first occurrence of each element in the second array from the
594596
-- | first array, creating a new array.
597+
-- |
598+
-- | Running time: `O(n*m)`, where n is the length of the first array, and m is
599+
-- | the length of the second.
595600
difference :: forall a. Eq a => Array a -> Array a -> Array a
596-
difference xs ys
597-
| null xs = []
598-
| otherwise = uncons' (const xs) (\z zs -> delete z xs \\ zs) ys
601+
difference = foldr delete
599602

600603
infix 5 difference as \\
601604

0 commit comments

Comments
 (0)