Skip to content

Commit d9312d9

Browse files
committed
Speed up difference
Also add its complexity to the documentation.
1 parent 845fb13 commit d9312d9

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
@@ -573,6 +573,8 @@ unionBy eq xs ys = xs <> foldl (flip (deleteBy eq)) (nubBy eq ys) xs
573573

574574
-- | Delete the first element of an array which is equal to the specified value,
575575
-- | creating a new array.
576+
-- |
577+
-- | Running time: `O(n)`
576578
delete :: forall a. Eq a => a -> Array a -> Array a
577579
delete = deleteBy eq
578580

@@ -585,10 +587,11 @@ deleteBy eq x ys = maybe ys (\i -> unsafePartial $ fromJust (deleteAt i ys)) (fi
585587

586588
-- | Delete the first occurrence of each element in the second array from the
587589
-- | first array, creating a new array.
590+
-- |
591+
-- | Running time: `O(n*m)`, where n is the length of the first array, and m is
592+
-- | the length of the second.
588593
difference :: forall a. Eq a => Array a -> Array a -> Array a
589-
difference xs ys
590-
| null xs = []
591-
| otherwise = uncons' (const xs) (\z zs -> delete z xs \\ zs) ys
594+
difference = foldr delete
592595

593596
infix 5 difference as \\
594597

0 commit comments

Comments
 (0)