@@ -519,14 +519,16 @@ nubBy eq xs =
519
519
Just o -> o.head : nubBy eq (filter (\y -> not (o.head `eq` y)) o.tail)
520
520
Nothing -> []
521
521
522
- -- | Calculate the union of two lists.
522
+ -- | Calculate the union of two arrays. Note that duplicates in the first array
523
+ -- | are preserved while duplicates in the second array are removed.
523
524
-- |
524
525
-- | Running time: `O(n^2)`
525
526
union :: forall a . Eq a => Array a -> Array a -> Array a
526
527
union = unionBy (==)
527
528
528
529
-- | Calculate the union of two arrays, using the specified function to
529
- -- | determine equality of elements.
530
+ -- | determine equality of elements. Note that duplicates in the first array
531
+ -- | are preserved while duplicates in the second array are removed.
530
532
unionBy :: forall a . (a -> a -> Boolean ) -> Array a -> Array a -> Array a
531
533
unionBy eq xs ys = xs <> foldl (flip (deleteBy eq)) (nubBy eq ys) xs
532
534
@@ -551,12 +553,16 @@ difference xs ys
551
553
552
554
infix 5 difference as \\
553
555
554
- -- | Calculate the intersection of two arrays, creating a new array.
556
+ -- | Calculate the intersection of two arrays, creating a new array. Note that
557
+ -- | duplicates in the first array are preserved while duplicates in the second
558
+ -- | array are removed.
555
559
intersect :: forall a . Eq a => Array a -> Array a -> Array a
556
560
intersect = intersectBy eq
557
561
558
562
-- | Calculate the intersection of two arrays, using the specified equivalence
559
- -- | relation to compare elements, creating a new array.
563
+ -- | relation to compare elements, creating a new array. Note that duplicates
564
+ -- | in the first array are preserved while duplicates in the second array are
565
+ -- | removed.
560
566
intersectBy :: forall a . (a -> a -> Boolean ) -> Array a -> Array a -> Array a
561
567
intersectBy eq xs ys = filter (\x -> isJust (findIndex (eq x) ys)) xs
562
568
0 commit comments