Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions intersect.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ func ContainsBy[T any](collection []T, predicate func(item T) bool) bool {
// Every returns true if all elements of a subset are contained in a collection or if the subset is empty.
// Play: https://go.dev/play/p/W1EvyqY6t9j
func Every[T comparable](collection, subset []T) bool {
for i := range subset {
if !Contains(collection, subset[i]) {
if len(subset) == 0 {
return true
}

seen := Keyify(collection)

for _, item := range subset {
if _, ok := seen[item]; !ok {
return false
}
}
Expand All @@ -52,8 +58,13 @@ func EveryBy[T any](collection []T, predicate func(item T) bool) bool {
// If the subset is empty Some returns false.
// Play: https://go.dev/play/p/Lj4ceFkeT9V
func Some[T comparable](collection, subset []T) bool {
for i := range subset {
if Contains(collection, subset[i]) {
if len(subset) == 0 {
return false
}

seen := Keyify(subset)
for i := range collection {
if _, ok := seen[collection[i]]; ok {
return true
}
}
Expand All @@ -77,8 +88,13 @@ func SomeBy[T any](collection []T, predicate func(item T) bool) bool {
// None returns true if no element of a subset is contained in a collection or if the subset is empty.
// Play: https://go.dev/play/p/fye7JsmxzPV
func None[T comparable](collection, subset []T) bool {
for i := range subset {
if Contains(collection, subset[i]) {
if len(subset) == 0 {
return true
}

seen := Keyify(subset)
for i := range collection {
if _, ok := seen[collection[i]]; ok {
return false
}
}
Expand Down
20 changes: 18 additions & 2 deletions it/intersect.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ func EveryBy[T any](collection iter.Seq[T], predicate func(item T) bool) bool {
// Will iterate through the entire sequence if subset elements never match.
// Play: https://go.dev/play/p/KmX-fXictQl
func Some[T comparable](collection iter.Seq[T], subset ...T) bool {
return SomeBy(collection, func(item T) bool { return lo.Contains(subset, item) })
if len(subset) == 0 {
return false
}

seen := lo.Keyify(subset)
return SomeBy(collection, func(item T) bool {
_, ok := seen[item]
return ok
})
}

// SomeBy returns true if the predicate returns true for any of the elements in the collection.
Expand All @@ -67,7 +75,15 @@ func SomeBy[T any](collection iter.Seq[T], predicate func(item T) bool) bool {
// Will iterate through the entire sequence if subset elements never match.
// Play: https://go.dev/play/p/KmX-fXictQl
func None[T comparable](collection iter.Seq[T], subset ...T) bool {
return NoneBy(collection, func(item T) bool { return lo.Contains(subset, item) })
if len(subset) == 0 {
return true
}

seen := lo.Keyify(subset)
return NoneBy(collection, func(item T) bool {
_, ok := seen[item]
return ok
})
}

// NoneBy returns true if the predicate returns true for none of the elements in the collection or if the collection is empty.
Expand Down
9 changes: 7 additions & 2 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ func PickByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map {
// Play: https://go.dev/play/p/1zdzSvbfsJc
func PickByValues[K, V comparable, Map ~map[K]V](in Map, values []V) Map {
r := Map{}

seen := Keyify(values)
for k, v := range in {
if Contains(values, v) {
if _, ok := seen[v]; ok {
r[k] = v
}
}
Expand Down Expand Up @@ -165,11 +167,14 @@ func OmitByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map {
// Play: https://go.dev/play/p/9UYZi-hrs8j
func OmitByValues[K, V comparable, Map ~map[K]V](in Map, values []V) Map {
r := Map{}

seen := Keyify(values)
for k, v := range in {
if !Contains(values, v) {
if _, ok := seen[v]; !ok {
r[k] = v
}
}

return r
}

Expand Down
Loading