@@ -519,14 +519,16 @@ nubBy eq xs =
519519 Just o -> o.head : nubBy eq (filter (\y -> not (o.head `eq` y)) o.tail)
520520 Nothing -> []
521521
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.
523524-- |
524525-- | Running time: `O(n^2)`
525526union :: forall a . Eq a => Array a -> Array a -> Array a
526527union = unionBy (==)
527528
528529-- | 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.
530532unionBy :: forall a . (a -> a -> Boolean ) -> Array a -> Array a -> Array a
531533unionBy eq xs ys = xs <> foldl (flip (deleteBy eq)) (nubBy eq ys) xs
532534
@@ -551,12 +553,16 @@ difference xs ys
551553
552554infix 5 difference as \\
553555
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.
555559intersect :: forall a . Eq a => Array a -> Array a -> Array a
556560intersect = intersectBy eq
557561
558562-- | 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.
560566intersectBy :: forall a . (a -> a -> Boolean ) -> Array a -> Array a -> Array a
561567intersectBy eq xs ys = filter (\x -> isJust (findIndex (eq x) ys)) xs
562568
0 commit comments