Skip to content

Commit

Permalink
ref(lo): use internal version of lo (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Aug 10, 2024
1 parent bd64563 commit ee39297
Show file tree
Hide file tree
Showing 14 changed files with 1,231 additions and 7 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"bodyclose",
"cenkalti",
"Chasinga",
"Clonable",
"cmds",
"Cobrass",
"coverpkg",
Expand Down
2 changes: 1 addition & 1 deletion boost/annotated-wait-group.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync"
"sync/atomic"

"github.com/samber/lo"
"github.com/snivilised/lorax/internal/lo"
"go.uber.org/zap/exp/zapslog"
"go.uber.org/zap/zapcore"
)
Expand Down
2 changes: 1 addition & 1 deletion boost/worker-pool-legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/samber/lo"
"github.com/snivilised/lorax/internal/lo"
"go.uber.org/zap/exp/zapslog"
"go.uber.org/zap/zapcore"
)
Expand Down
2 changes: 1 addition & 1 deletion boost/worker-pool-legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/fortytw2/leaktest"
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo ok
. "github.com/onsi/gomega" //nolint:revive // gomega ok
"github.com/samber/lo"
"github.com/snivilised/lorax/internal/lo"

"github.com/snivilised/lorax/boost"
"github.com/snivilised/lorax/internal/helpers"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.0
require (
github.com/onsi/ginkgo/v2 v2.20.0
github.com/onsi/gomega v1.34.1
github.com/samber/lo v1.46.0
github.com/samber/lo v1.46.0 // indirect
github.com/snivilised/extendio v0.7.0
)

Expand Down
10 changes: 10 additions & 0 deletions internal/lo/constraints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package lo

// MIT License
//
// Copyright (c) 2022 Samuel Berthe

// Clonable defines a constraint of types having Clone() T method.
type Clonable[T any] interface {
Clone() T
}
185 changes: 185 additions & 0 deletions internal/lo/intersect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package lo

// Contains returns true if an element is present in a collection.
func Contains[T comparable](collection []T, element T) bool {
for _, item := range collection {
if item == element {
return true
}
}

return false
}

// ContainsBy returns true if predicate function return true.
func ContainsBy[T any](collection []T, predicate func(item T) bool) bool {
for _, item := range collection {
if predicate(item) {
return true
}
}

return false
}

// Every returns true if all elements of a subset are contained into a collection or if the subset is empty.
func Every[T comparable](collection, subset []T) bool {
for _, elem := range subset {
if !Contains(collection, elem) {
return false
}
}

return true
}

// EveryBy returns true if the predicate returns true for all of the elements in the collection or if the collection is empty.
func EveryBy[T any](collection []T, predicate func(item T) bool) bool {
for _, v := range collection {
if !predicate(v) {
return false
}
}

return true
}

// Some returns true if at least 1 element of a subset is contained into a collection.
// If the subset is empty Some returns false.
func Some[T comparable](collection, subset []T) bool {
for _, elem := range subset {
if Contains(collection, elem) {
return true
}
}

return false
}

// SomeBy returns true if the predicate returns true for any of the elements in the collection.
// If the collection is empty SomeBy returns false.
func SomeBy[T any](collection []T, predicate func(item T) bool) bool {
for _, v := range collection {
if predicate(v) {
return true
}
}

return false
}

// None returns true if no element of a subset are contained into a collection or if the subset is empty.
func None[T comparable](collection, subset []T) bool {
for _, elem := range subset {
if Contains(collection, elem) {
return false
}
}

return true
}

// NoneBy returns true if the predicate returns true for none of the elements in the collection or if the collection is empty.
func NoneBy[T any](collection []T, predicate func(item T) bool) bool {
for _, v := range collection {
if predicate(v) {
return false
}
}

return true
}

// Intersect returns the intersection between two collections.
func Intersect[T comparable](list1, list2 []T) []T {
result := []T{}
seen := map[T]struct{}{}

for _, elem := range list1 {
seen[elem] = struct{}{}
}

for _, elem := range list2 {
if _, ok := seen[elem]; ok {
result = append(result, elem)
}
}

return result
}

// Difference returns the difference between two collections.
// The first value is the collection of element absent of list2.
// The second value is the collection of element absent of list1.
func Difference[T comparable](list1, list2 []T) (left, right []T) {
left = []T{}
right = []T{}

seenLeft := map[T]struct{}{}
seenRight := map[T]struct{}{}

for _, elem := range list1 {
seenLeft[elem] = struct{}{}
}

for _, elem := range list2 {
seenRight[elem] = struct{}{}
}

for _, elem := range list1 {
if _, ok := seenRight[elem]; !ok {
left = append(left, elem)
}
}

for _, elem := range list2 {
if _, ok := seenLeft[elem]; !ok {
right = append(right, elem)
}
}

return left, right
}

// Union returns all distinct elements from given collections.
// result returns will not change the order of elements relatively.
func Union[T comparable](lists ...[]T) []T {
result := []T{}
seen := map[T]struct{}{}

for _, list := range lists {
for _, e := range list {
if _, ok := seen[e]; !ok {
seen[e] = struct{}{}
result = append(result, e)
}
}
}

return result
}

// Without returns slice excluding all given values.
func Without[T comparable](collection []T, exclude ...T) []T {
result := make([]T, 0, len(collection))
for _, e := range collection {
if !Contains(exclude, e) {
result = append(result, e)
}
}
return result
}

// WithoutEmpty returns slice excluding empty values.
func WithoutEmpty[T comparable](collection []T) []T {
var empty T

result := make([]T, 0, len(collection))
for _, e := range collection {
if e != empty {
result = append(result, e)
}
}

return result
}
Loading

0 comments on commit ee39297

Please sign in to comment.