Skip to content

Commit 4b8afc8

Browse files
committed
Add union, unionBy
1 parent 8690471 commit 4b8afc8

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

docs/Data.Array.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,25 @@ nubBy :: forall a. (a -> a -> Boolean) -> Array a -> Array a
443443
Remove the duplicates from an array, where element equality is determined
444444
by the specified equivalence relation, creating a new array.
445445

446+
#### `union`
447+
448+
``` purescript
449+
union :: forall a. (Eq a) => Array a -> Array a -> Array a
450+
```
451+
452+
Calculate the union of two lists.
453+
454+
Running time: `O(n^2)`
455+
456+
#### `unionBy`
457+
458+
``` purescript
459+
unionBy :: forall a. (a -> a -> Boolean) -> Array a -> Array a -> Array a
460+
```
461+
462+
Calculate the union of two arrays, using the specified function to
463+
determine equality of elements.
464+
446465
#### `delete`
447466

448467
``` purescript

src/Data/Array.purs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ module Data.Array
6161

6262
, nub
6363
, nubBy
64-
-- , union
65-
-- , unionBy
64+
, union
65+
, unionBy
6666
, delete
6767
, deleteBy
6868

@@ -84,6 +84,7 @@ import Control.Alternative (Alternative)
8484
import Control.Lazy (Lazy, defer)
8585
import Control.MonadPlus (MonadPlus)
8686
import Control.Plus (Plus)
87+
import Data.Foldable (foldl)
8788
import Data.Functor.Invariant (Invariant, imapF)
8889
import Data.Maybe (Maybe(..), maybe, isJust)
8990
import Data.Monoid (Monoid, mempty)
@@ -466,6 +467,17 @@ nubBy _ [] = []
466467
nubBy eq xs = case uncons xs of
467468
Just o -> o.head : nubBy eq (filter (\y -> not (o.head `eq` y)) o.tail)
468469

470+
-- | Calculate the union of two lists.
471+
-- |
472+
-- | Running time: `O(n^2)`
473+
union :: forall a. (Eq a) => Array a -> Array a -> Array a
474+
union = unionBy (==)
475+
476+
-- | Calculate the union of two arrays, using the specified function to
477+
-- | determine equality of elements.
478+
unionBy :: forall a. (a -> a -> Boolean) -> Array a -> Array a -> Array a
479+
unionBy eq xs ys = xs ++ foldl (flip (deleteBy eq)) (nubBy eq ys) xs
480+
469481
-- | Delete the first element of an array which is equal to the specified value,
470482
-- | creating a new array.
471483
delete :: forall a. (Eq a) => a -> Array a -> Array a

test/Test/Data/Array.purs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ testArray = do
236236
let nubPred = \x y -> if odd x then false else x == y
237237
assert $ nubBy nubPred [1, 2, 2, 3, 3, 4, 4, 1] == [1, 2, 3, 3, 4, 1]
238238

239+
log "union should produce the union of two arrays"
240+
assert $ union [1, 2, 3] [2, 3, 4] == [1, 2, 3, 4]
241+
assert $ union [1, 1, 2, 3] [2, 3, 4] == [1, 1, 2, 3, 4]
242+
243+
log "unionBy should produce the union of two arrays using the specified equality relation"
244+
assert $ unionBy (\_ y -> y < 5) [1, 2, 3] [2, 3, 4, 5, 6] == [1, 2, 3, 5, 6]
245+
239246
log "delete should remove the first matching item from an array"
240247
assert $ delete 1 [1, 2, 1] == [2, 1]
241248
assert $ delete 2 [1, 2, 1] == [1, 1]

0 commit comments

Comments
 (0)