Skip to content

Commit

Permalink
Merge pull request #127 from Primetalk/slice-union
Browse files Browse the repository at this point in the history
feat: add slice.Union
  • Loading branch information
Primetalk authored Feb 7, 2023
2 parents fb30929 + 84dbb1c commit c117a1b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ Some utilities that are convenient when working with slices.
- `slice.Filter[A any](as []A, p func(a A) bool) (res []A)`
- `slice.FilterNot[A any](as []A, p func(a A) bool) (res []A)` - same as `Filter`, but inverses the predicate `p`.
- `slice.Remove[A comparable](as []A, r []A) (res []A)` - Remove removes any elements in r from as.
- `slice.Intersection[A comparable](as []A, as2 []A) (res []A)` - Intersection leaves only elements that are both in as and as2.
- `slice.Union[A comparable](as []A, as2 []A) (res []A)` - Union returns elements that are either in as or in as2.
- `slice.Partition[A any](as []A, p fun.Predicate[A]) (resT []A, resF []A)` - Partition separates elements in as according to the predicate.
- `slice.Exists[A any](p fun.Predicate[A]) fun.Predicate[[]A]` - Exists returns a predicate on slices. The predicate is true if there is an element that satisfy the given element-wise predicate. It's false for an empty slice.
- `slice.Forall[A any](p fun.Predicate[A]) fun.Predicate[[]A]` - Forall returns a predicate on slices. The predicate is true if all elements satisfy the given element-wise predicate. It's true for an empty slice.
Expand Down
12 changes: 12 additions & 0 deletions slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,15 @@ func Remove[A comparable](as []A, r []A) (res []A) {
sr := ToSet(r)
return FilterNot(as, set.Contains(sr))
}

// Intersection leaves only elements that are both in as and as2.
func Intersection[A comparable](as []A, as2 []A) (res []A) {
sr := ToSet(as2)
return Filter(as, set.Contains(sr))
}

// Union returns elements that are either in as or in as2.
func Union[A comparable](as []A, as2 []A) (res []A) {
sr := ToSet(as)
return append(as, FilterNot(as2, set.Contains(sr))...)
}
14 changes: 14 additions & 0 deletions slice/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,17 @@ func TestRemove(t *testing.T) {
znats510 := slice.Range(5, 10)
assert.ElementsMatch(t, znats510, slice.Remove(znats10, znats5))
}

func TestIntersection(t *testing.T) {
znats10 := slice.Range(0, 10)
znats515 := slice.Range(5, 15)
znats5 := slice.Range(5, 10)
assert.ElementsMatch(t, znats5, slice.Intersection(znats10, znats515))
}

func TestUnion(t *testing.T) {
znats10 := slice.Range(0, 10)
znats515 := slice.Range(5, 15)
znats15 := slice.Range(0, 15)
assert.ElementsMatch(t, znats15, slice.Union(znats10, znats515))
}

0 comments on commit c117a1b

Please sign in to comment.