mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-19 00:28:03 +02:00
make more use of generics
This commit is contained in:
21
vendor/github.com/jesseduffield/generics/LICENSE
generated
vendored
Normal file
21
vendor/github.com/jesseduffield/generics/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Jesse Duffield
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
35
vendor/github.com/jesseduffield/generics/hashmap/functions.go
generated
vendored
Normal file
35
vendor/github.com/jesseduffield/generics/hashmap/functions.go
generated
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
package hashmap
|
||||
|
||||
func Keys[Key comparable, Value any](m map[Key]Value) []Key {
|
||||
keys := make([]Key, 0, len(m))
|
||||
for key := range m {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
func Values[Key comparable, Value any](m map[Key]Value) []Value {
|
||||
values := make([]Value, 0, len(m))
|
||||
for _, value := range m {
|
||||
values = append(values, value)
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func TransformValues[Key comparable, Value any, NewValue any](
|
||||
m map[Key]Value, fn func(Value) NewValue,
|
||||
) map[Key]NewValue {
|
||||
output := make(map[Key]NewValue)
|
||||
for key, value := range m {
|
||||
output[key] = fn(value)
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func TransformKeys[Key comparable, Value any, NewKey comparable](m map[Key]Value, fn func(Key) NewKey) map[NewKey]Value {
|
||||
output := make(map[NewKey]Value)
|
||||
for key, value := range m {
|
||||
output[fn(key)] = value
|
||||
}
|
||||
return output
|
||||
}
|
49
vendor/github.com/jesseduffield/generics/list/comparable_list.go
generated
vendored
Normal file
49
vendor/github.com/jesseduffield/generics/list/comparable_list.go
generated
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
package list
|
||||
|
||||
import (
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
type ComparableList[T comparable] struct {
|
||||
*List[T]
|
||||
}
|
||||
|
||||
func NewComparable[T comparable]() *ComparableList[T] {
|
||||
return &ComparableList[T]{List: New[T]()}
|
||||
}
|
||||
|
||||
func NewComparableFromSlice[T comparable](slice []T) *ComparableList[T] {
|
||||
return &ComparableList[T]{List: NewFromSlice(slice)}
|
||||
}
|
||||
|
||||
func (l *ComparableList[T]) Equal(other *ComparableList[T]) bool {
|
||||
return l.EqualSlice(other.ToSlice())
|
||||
}
|
||||
|
||||
func (l *ComparableList[T]) EqualSlice(other []T) bool {
|
||||
return slices.Equal(l.ToSlice(), other)
|
||||
}
|
||||
|
||||
func (l *ComparableList[T]) Compact() {
|
||||
l.slice = slices.Compact(l.slice)
|
||||
}
|
||||
|
||||
func (l *ComparableList[T]) Index(needle T) int {
|
||||
return slices.Index(l.slice, needle)
|
||||
}
|
||||
|
||||
func (l *ComparableList[T]) Contains(needle T) bool {
|
||||
return slices.Contains(l.slice, needle)
|
||||
}
|
||||
|
||||
func (l *ComparableList[T]) SortFuncInPlace(test func(a T, b T) bool) {
|
||||
slices.SortFunc(l.slice, test)
|
||||
}
|
||||
|
||||
func (l *ComparableList[T]) SortFunc(test func(a T, b T) bool) *ComparableList[T] {
|
||||
newSlice := slices.Clone(l.slice)
|
||||
|
||||
slices.SortFunc(newSlice, test)
|
||||
|
||||
return NewComparableFromSlice(newSlice)
|
||||
}
|
72
vendor/github.com/jesseduffield/generics/list/functions.go
generated
vendored
Normal file
72
vendor/github.com/jesseduffield/generics/list/functions.go
generated
vendored
Normal file
@ -0,0 +1,72 @@
|
||||
package list
|
||||
|
||||
func Some[T any](slice []T, test func(T) bool) bool {
|
||||
for _, value := range slice {
|
||||
if test(value) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func Every[T any](slice []T, test func(T) bool) bool {
|
||||
for _, value := range slice {
|
||||
if !test(value) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func Map[T any, V any](slice []T, f func(T) V) []V {
|
||||
result := make([]V, len(slice))
|
||||
for i, value := range slice {
|
||||
result[i] = f(value)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func MapInPlace[T any](slice []T, f func(T) T) {
|
||||
for i, value := range slice {
|
||||
slice[i] = f(value)
|
||||
}
|
||||
}
|
||||
|
||||
func Filter[T any](slice []T, test func(T) bool) []T {
|
||||
result := make([]T, 0)
|
||||
for _, element := range slice {
|
||||
if test(element) {
|
||||
result = append(result, element)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func FilterInPlace[T any](slice []T, test func(T) bool) []T {
|
||||
newLength := 0
|
||||
for _, element := range slice {
|
||||
if test(element) {
|
||||
slice[newLength] = element
|
||||
newLength++
|
||||
}
|
||||
}
|
||||
|
||||
return slice[:newLength]
|
||||
}
|
||||
|
||||
func Reverse[T any](slice []T) []T {
|
||||
result := make([]T, len(slice))
|
||||
for i := range slice {
|
||||
result[i] = slice[len(slice)-1-i]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func ReverseInPlace[T any](slice []T) {
|
||||
for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
|
||||
slice[i], slice[j] = slice[j], slice[i]
|
||||
}
|
||||
}
|
117
vendor/github.com/jesseduffield/generics/list/list.go
generated
vendored
Normal file
117
vendor/github.com/jesseduffield/generics/list/list.go
generated
vendored
Normal file
@ -0,0 +1,117 @@
|
||||
package list
|
||||
|
||||
import (
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
type List[T any] struct {
|
||||
slice []T
|
||||
}
|
||||
|
||||
func New[T any]() *List[T] {
|
||||
return &List[T]{}
|
||||
}
|
||||
|
||||
func NewFromSlice[T any](slice []T) *List[T] {
|
||||
return &List[T]{slice: slice}
|
||||
}
|
||||
|
||||
func (l *List[T]) ToSlice() []T {
|
||||
return l.slice
|
||||
}
|
||||
|
||||
// Mutative methods
|
||||
|
||||
func (l *List[T]) Push(v T) {
|
||||
l.slice = append(l.slice, v)
|
||||
}
|
||||
|
||||
func (l *List[T]) Pop() {
|
||||
l.slice = l.slice[0 : len(l.slice)-1]
|
||||
}
|
||||
|
||||
func (l *List[T]) Insert(index int, values ...T) {
|
||||
l.slice = slices.Insert(l.slice, index, values...)
|
||||
}
|
||||
|
||||
func (l *List[T]) Append(values ...T) {
|
||||
l.slice = append(l.slice, values...)
|
||||
}
|
||||
|
||||
func (l *List[T]) Prepend(values ...T) {
|
||||
l.slice = append(values, l.slice...)
|
||||
}
|
||||
|
||||
func (l *List[T]) Remove(index int) {
|
||||
l.Delete(index, index+1)
|
||||
}
|
||||
|
||||
func (l *List[T]) Delete(from int, to int) {
|
||||
l.slice = slices.Delete(l.slice, from, to)
|
||||
}
|
||||
|
||||
func (l *List[T]) FilterInPlace(test func(value T) bool) {
|
||||
l.slice = FilterInPlace(l.slice, test)
|
||||
}
|
||||
|
||||
func (l *List[T]) MapInPlace(f func(value T) T) {
|
||||
MapInPlace(l.slice, f)
|
||||
}
|
||||
|
||||
func (l *List[T]) ReverseInPlace() {
|
||||
ReverseInPlace(l.slice)
|
||||
}
|
||||
|
||||
// Non-mutative methods
|
||||
|
||||
// Similar to Append but we leave the original slice untouched and return a new list
|
||||
func (l *List[T]) Concat(values ...T) *List[T] {
|
||||
newSlice := make([]T, 0, len(l.slice)+len(values))
|
||||
newSlice = append(newSlice, l.slice...)
|
||||
newSlice = append(newSlice, values...)
|
||||
return &List[T]{slice: newSlice}
|
||||
}
|
||||
|
||||
func (l *List[T]) Filter(test func(value T) bool) *List[T] {
|
||||
return NewFromSlice(Filter(l.slice, test))
|
||||
}
|
||||
|
||||
// Unfortunately this does not support mapping from one type to another
|
||||
// because Go does not yet (and may never) support methods defining their own
|
||||
// type parameters. For that functionality you'll need to use the standalone
|
||||
// Map function instead
|
||||
func (l *List[T]) Map(f func(value T) T) *List[T] {
|
||||
return NewFromSlice(Map(l.slice, f))
|
||||
}
|
||||
|
||||
func (l *List[T]) Clone() *List[T] {
|
||||
return NewFromSlice(slices.Clone(l.slice))
|
||||
}
|
||||
|
||||
func (l *List[T]) Some(test func(value T) bool) bool {
|
||||
return Some(l.slice, test)
|
||||
}
|
||||
|
||||
func (l *List[T]) Every(test func(value T) bool) bool {
|
||||
return Every(l.slice, test)
|
||||
}
|
||||
|
||||
func (l *List[T]) IndexFunc(f func(T) bool) int {
|
||||
return slices.IndexFunc(l.slice, f)
|
||||
}
|
||||
|
||||
func (l *List[T]) ContainsFunc(f func(T) bool) bool {
|
||||
return l.IndexFunc(f) != -1
|
||||
}
|
||||
|
||||
func (l *List[T]) Reverse() *List[T] {
|
||||
return NewFromSlice(Reverse(l.slice))
|
||||
}
|
||||
|
||||
func (l *List[T]) IsEmpty() bool {
|
||||
return len(l.slice) == 0
|
||||
}
|
||||
|
||||
func (l *List[T]) Len() int {
|
||||
return len(l.slice)
|
||||
}
|
49
vendor/github.com/jesseduffield/generics/set/set.go
generated
vendored
Normal file
49
vendor/github.com/jesseduffield/generics/set/set.go
generated
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
package set
|
||||
|
||||
import "github.com/jesseduffield/generics/hashmap"
|
||||
|
||||
type Set[T comparable] struct {
|
||||
hashMap map[T]bool
|
||||
}
|
||||
|
||||
func New[T comparable]() *Set[T] {
|
||||
return &Set[T]{hashMap: make(map[T]bool)}
|
||||
}
|
||||
|
||||
func NewFromSlice[T comparable](slice []T) *Set[T] {
|
||||
hashMap := make(map[T]bool)
|
||||
for _, value := range slice {
|
||||
hashMap[value] = true
|
||||
}
|
||||
|
||||
return &Set[T]{hashMap: hashMap}
|
||||
}
|
||||
|
||||
func (s *Set[T]) Add(value T) {
|
||||
s.hashMap[value] = true
|
||||
}
|
||||
|
||||
func (s *Set[T]) AddSlice(slice []T) {
|
||||
for _, value := range slice {
|
||||
s.Add(value)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Set[T]) Remove(value T) {
|
||||
delete(s.hashMap, value)
|
||||
}
|
||||
|
||||
func (s *Set[T]) RemoveSlice(slice []T) {
|
||||
for _, value := range slice {
|
||||
s.Remove(value)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Set[T]) Includes(value T) bool {
|
||||
return s.hashMap[value]
|
||||
}
|
||||
|
||||
// output slice is not necessarily in the same order that items were added
|
||||
func (s *Set[T]) ToSlice() []T {
|
||||
return hashmap.Keys(s.hashMap)
|
||||
}
|
36
vendor/github.com/samber/lo/.gitignore
generated
vendored
Normal file
36
vendor/github.com/samber/lo/.gitignore
generated
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
|
||||
# Created by https://www.toptal.com/developers/gitignore/api/go
|
||||
# Edit at https://www.toptal.com/developers/gitignore?templates=go
|
||||
|
||||
### Go ###
|
||||
# If you prefer the allow list template instead of the deny list, see community template:
|
||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||
#
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
||||
### Go Patch ###
|
||||
/vendor/
|
||||
/Godeps/
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/go
|
||||
|
||||
cover.out
|
||||
cover.html
|
||||
.vscode
|
71
vendor/github.com/samber/lo/CHANGELOG.md
generated
vendored
Normal file
71
vendor/github.com/samber/lo/CHANGELOG.md
generated
vendored
Normal file
@ -0,0 +1,71 @@
|
||||
# Changelog
|
||||
|
||||
## 1.3.0 (2022-03-03)
|
||||
|
||||
Last and Nth return errors
|
||||
|
||||
## 1.2.0 (2022-03-03)
|
||||
|
||||
Adding `lop.Map` and `lop.ForEach`.
|
||||
|
||||
## 1.1.0 (2022-03-03)
|
||||
|
||||
Adding `i int` param to `lo.Map()`, `lo.Filter()`, `lo.ForEach()` and `lo.Reduce()` predicates.
|
||||
|
||||
## 1.0.0 (2022-03-02)
|
||||
|
||||
*Initial release*
|
||||
|
||||
Supported helpers for slices:
|
||||
|
||||
- Filter
|
||||
- Map
|
||||
- Reduce
|
||||
- ForEach
|
||||
- Uniq
|
||||
- UniqBy
|
||||
- GroupBy
|
||||
- Chunk
|
||||
- Flatten
|
||||
- Shuffle
|
||||
- Reverse
|
||||
- Fill
|
||||
- ToMap
|
||||
|
||||
Supported helpers for maps:
|
||||
|
||||
- Keys
|
||||
- Values
|
||||
- Entries
|
||||
- FromEntries
|
||||
- Assign (maps merge)
|
||||
|
||||
Supported intersection helpers:
|
||||
|
||||
- Contains
|
||||
- Every
|
||||
- Some
|
||||
- Intersect
|
||||
- Difference
|
||||
|
||||
Supported search helpers:
|
||||
|
||||
- IndexOf
|
||||
- LastIndexOf
|
||||
- Find
|
||||
- Min
|
||||
- Max
|
||||
- Last
|
||||
- Nth
|
||||
|
||||
Other functional programming helpers:
|
||||
|
||||
- Ternary (1 line if/else statement)
|
||||
- If / ElseIf / Else
|
||||
- Switch / Case / Default
|
||||
- ToPtr
|
||||
- ToSlicePtr
|
||||
|
||||
Constraints:
|
||||
|
||||
- Clonable
|
8
vendor/github.com/samber/lo/Dockerfile
generated
vendored
Normal file
8
vendor/github.com/samber/lo/Dockerfile
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
FROM golang:1.18rc1-bullseye
|
||||
|
||||
WORKDIR /go/src/github.com/samber/lo
|
||||
|
||||
COPY Makefile go.* /go/src/github.com/samber/lo/
|
||||
|
||||
RUN make tools
|
21
vendor/github.com/samber/lo/LICENSE
generated
vendored
Normal file
21
vendor/github.com/samber/lo/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Samuel Berthe
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
51
vendor/github.com/samber/lo/Makefile
generated
vendored
Normal file
51
vendor/github.com/samber/lo/Makefile
generated
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
|
||||
BIN=go
|
||||
# BIN=go1.18beta1
|
||||
|
||||
go1.18beta1:
|
||||
go install golang.org/dl/go1.18beta1@latest
|
||||
go1.18beta1 download
|
||||
|
||||
build:
|
||||
${BIN} build -v ./...
|
||||
|
||||
test:
|
||||
go test -race -v ./...
|
||||
watch-test:
|
||||
reflex -R assets.go -t 50ms -s -- sh -c 'gotest -race -v ./...'
|
||||
|
||||
bench:
|
||||
go test -benchmem -count 3 -bench ./...
|
||||
watch-bench:
|
||||
reflex -R assets.go -t 50ms -s -- sh -c 'go test -benchmem -count 3 -bench ./...'
|
||||
|
||||
coverage:
|
||||
${BIN} test -v -coverprofile cover.out .
|
||||
${BIN} tool cover -html=cover.out -o cover.html
|
||||
|
||||
# tools
|
||||
tools:
|
||||
${BIN} install github.com/cespare/reflex@latest
|
||||
${BIN} install github.com/rakyll/gotest@latest
|
||||
${BIN} install github.com/psampaz/go-mod-outdated@latest
|
||||
${BIN} install github.com/jondot/goweight@latest
|
||||
${BIN} install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
||||
${BIN} get -t -u golang.org/x/tools/cmd/cover
|
||||
${BIN} get -t -u github.com/sonatype-nexus-community/nancy@latest
|
||||
go mod tidy
|
||||
|
||||
lint:
|
||||
golangci-lint run --timeout 60s --max-same-issues 50 ./...
|
||||
lint-fix:
|
||||
golangci-lint run --timeout 60s --max-same-issues 50 --fix ./...
|
||||
|
||||
audit: tools
|
||||
${BIN} mod tidy
|
||||
${BIN} list -json -m all | nancy sleuth
|
||||
|
||||
outdated: tools
|
||||
${BIN} mod tidy
|
||||
${BIN} list -u -m -json all | go-mod-outdated -update -direct
|
||||
|
||||
weight: tools
|
||||
goweight
|
981
vendor/github.com/samber/lo/README.md
generated
vendored
Normal file
981
vendor/github.com/samber/lo/README.md
generated
vendored
Normal file
@ -0,0 +1,981 @@
|
||||
# lo
|
||||
|
||||

|
||||
[](https://pkg.go.dev/github.com/samber/lo)
|
||||
[](https://goreportcard.com/report/github.com/samber/lo)
|
||||
|
||||
✨ **`lo` is a Lodash-style Go library based on Go 1.18+ Generics.**
|
||||
|
||||
This project started as an experiment with the new generics implementation. It may look like [Lodash](https://github.com/lodash/lodash) in some aspects. I used to code with the fantastic ["go-funk"](https://github.com/thoas/go-funk) package, but "go-funk" uses reflection and therefore is not typesafe.
|
||||
|
||||
As expected, benchmarks demonstrate that generics will be much faster than implementations based on the "reflect" package. Benchmarks also show similar performance gains compared to pure `for` loops. [See below](#-benchmark).
|
||||
|
||||
In the future, 5 to 10 helpers will overlap with those coming into the Go standard library (under package names `slices` and `maps`). I feel this library is legitimate and offers many more valuable abstractions.
|
||||
|
||||
### Why this name?
|
||||
|
||||
I wanted a **short name**, similar to "Lodash" and no Go package currently uses this name.
|
||||
|
||||
## 🚀 Install
|
||||
|
||||
```sh
|
||||
go get github.com/samber/lo
|
||||
```
|
||||
|
||||
## 💡 Usage
|
||||
|
||||
You can import `lo` using:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/samber/lo"
|
||||
lop "github.com/samber/lo/parallel"
|
||||
)
|
||||
```
|
||||
|
||||
Then use one of the helpers below:
|
||||
|
||||
```go
|
||||
names := lo.Uniq[string]([]string{"Samuel", "Marc", "Samuel"})
|
||||
// []string{"Samuel", "Marc"}
|
||||
```
|
||||
|
||||
Most of the time, the compiler will be able to infer the type so that you can call: `lo.Uniq([]string{...})`.
|
||||
|
||||
## 🤠 Spec
|
||||
|
||||
GoDoc: [https://godoc.org/github.com/samber/lo](https://godoc.org/github.com/samber/lo)
|
||||
|
||||
Supported helpers for slices:
|
||||
|
||||
- Filter
|
||||
- Map
|
||||
- FlatMap
|
||||
- Reduce
|
||||
- ForEach
|
||||
- Times
|
||||
- Uniq
|
||||
- UniqBy
|
||||
- GroupBy
|
||||
- Chunk
|
||||
- PartitionBy
|
||||
- Flatten
|
||||
- Shuffle
|
||||
- Reverse
|
||||
- Fill
|
||||
- Repeat
|
||||
- KeyBy
|
||||
- Drop
|
||||
- DropRight
|
||||
- DropWhile
|
||||
- DropRightWhile
|
||||
|
||||
Supported helpers for maps:
|
||||
|
||||
- Keys
|
||||
- Values
|
||||
- Entries
|
||||
- FromEntries
|
||||
- Assign (merge of maps)
|
||||
- MapValues
|
||||
|
||||
Supported helpers for tuples:
|
||||
|
||||
- Zip2 -> Zip9
|
||||
- Unzip2 -> Unzip9
|
||||
|
||||
Supported intersection helpers:
|
||||
|
||||
- Contains
|
||||
- ContainsBy
|
||||
- Every
|
||||
- Some
|
||||
- Intersect
|
||||
- Difference
|
||||
- Union
|
||||
|
||||
Supported search helpers:
|
||||
|
||||
- IndexOf
|
||||
- LastIndexOf
|
||||
- Find
|
||||
- Min
|
||||
- Max
|
||||
- Last
|
||||
- Nth
|
||||
- Sample
|
||||
- Samples
|
||||
|
||||
Other functional programming helpers:
|
||||
|
||||
- Ternary (1 line if/else statement)
|
||||
- If / ElseIf / Else
|
||||
- Switch / Case / Default
|
||||
- ToPtr
|
||||
- ToSlicePtr
|
||||
- Attempt
|
||||
- Range / RangeFrom / RangeWithSteps
|
||||
|
||||
Constraints:
|
||||
|
||||
- Clonable
|
||||
|
||||
### Map
|
||||
|
||||
Manipulates a slice of one type and transforms it into a slice of another type:
|
||||
|
||||
```go
|
||||
import "github.com/samber/lo"
|
||||
|
||||
lo.Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
|
||||
return strconv.FormatInt(x, 10)
|
||||
})
|
||||
// []string{"1", "2", "3", "4"}
|
||||
```
|
||||
|
||||
Parallel processing: like `lo.Map()`, but the mapper function is called in a goroutine. Results are returned in the same order.
|
||||
|
||||
```go
|
||||
import lop "github.com/samber/lo/parallel"
|
||||
|
||||
lop.Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
|
||||
return strconv.FormatInt(x, 10)
|
||||
})
|
||||
// []string{"1", "2", "3", "4"}
|
||||
```
|
||||
|
||||
### FlatMap
|
||||
|
||||
Manipulates a slice and transforms and flattens it to a slice of another type.
|
||||
|
||||
```go
|
||||
lo.FlatMap[int, string]([]int{0, 1, 2}, func(x int, _ int) []string {
|
||||
return []string{
|
||||
strconv.FormatInt(x, 10),
|
||||
strconv.FormatInt(x, 10),
|
||||
}
|
||||
})
|
||||
// []string{"0", "0", "1", "1", "2", "2"}
|
||||
```
|
||||
|
||||
### Filter
|
||||
|
||||
Iterates over a collection and returns an array of all the elements the predicate function returns `true` for.
|
||||
|
||||
```go
|
||||
even := lo.Filter[int]([]int{1, 2, 3, 4}, func(x int, _ int) bool {
|
||||
return x%2 == 0
|
||||
})
|
||||
// []int{2, 4}
|
||||
```
|
||||
|
||||
### Contains
|
||||
|
||||
Returns true if an element is present in a collection.
|
||||
|
||||
```go
|
||||
present := lo.Contains[int]([]int{0, 1, 2, 3, 4, 5}, 5)
|
||||
// true
|
||||
```
|
||||
|
||||
### Contains
|
||||
|
||||
Returns true if the predicate function returns `true`.
|
||||
|
||||
```go
|
||||
present := lo.ContainsBy[int]([]int{0, 1, 2, 3, 4, 5}, func(x int) bool {
|
||||
return x == 3
|
||||
})
|
||||
// true
|
||||
```
|
||||
|
||||
### Reduce
|
||||
|
||||
Reduces a collection to a single value. The value is calculated by accumulating the result of running each element in the collection through an accumulator function. Each successive invocation is supplied with the return value returned by the previous call.
|
||||
|
||||
```go
|
||||
sum := lo.Reduce[int, int]([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int {
|
||||
return agg + item
|
||||
}, 0)
|
||||
// 10
|
||||
```
|
||||
|
||||
### ForEach
|
||||
|
||||
Iterates over elements of a collection and invokes the function over each element.
|
||||
|
||||
```go
|
||||
import "github.com/samber/lo"
|
||||
|
||||
lo.ForEach[string]([]string{"hello", "world"}, func(x string, _ int) {
|
||||
println(x)
|
||||
})
|
||||
// prints "hello\nworld\n"
|
||||
```
|
||||
|
||||
Parallel processing: like `lo.ForEach()`, but the callback is called as a goroutine.
|
||||
|
||||
```go
|
||||
import lop "github.com/samber/lo/parallel"
|
||||
|
||||
lop.ForEach[string]([]string{"hello", "world"}, func(x string, _ int) {
|
||||
println(x)
|
||||
})
|
||||
// prints "hello\nworld\n" or "world\nhello\n"
|
||||
```
|
||||
|
||||
### Times
|
||||
|
||||
Times invokes the iteratee n times, returning an array of the results of each invocation. The iteratee is invoked with index as argument.
|
||||
|
||||
```go
|
||||
import "github.com/samber/lo"
|
||||
|
||||
lo.Times[string](3, func(i int) string {
|
||||
return strconv.FormatInt(int64(i), 10)
|
||||
})
|
||||
// []string{"0", "1", "2"}
|
||||
```
|
||||
|
||||
Parallel processing: like `lo.Times()`, but callback is called in goroutine.
|
||||
|
||||
```go
|
||||
import lop "github.com/samber/lo/parallel"
|
||||
|
||||
lop.Times[string](3, func(i int) string {
|
||||
return strconv.FormatInt(int64(i), 10)
|
||||
})
|
||||
// []string{"0", "1", "2"}
|
||||
```
|
||||
|
||||
### Uniq
|
||||
|
||||
Returns a duplicate-free version of an array, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array.
|
||||
|
||||
```go
|
||||
uniqValues := lo.Uniq[int]([]int{1, 2, 2, 1})
|
||||
// []int{1, 2}
|
||||
```
|
||||
|
||||
### UniqBy
|
||||
|
||||
Returns a duplicate-free version of an array, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is invoked for each element in array to generate the criterion by which uniqueness is computed.
|
||||
|
||||
```go
|
||||
uniqValues := lo.UniqBy[int, int]([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
|
||||
return i%3
|
||||
})
|
||||
// []int{0, 1, 2}
|
||||
```
|
||||
|
||||
### GroupBy
|
||||
|
||||
Returns an object composed of keys generated from the results of running each element of collection through iteratee.
|
||||
|
||||
```go
|
||||
import lo "github.com/samber/lo"
|
||||
|
||||
groups := lo.GroupBy[int, int]([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
|
||||
return i%3
|
||||
})
|
||||
// map[int][]int{0: []int{0, 3}, 1: []int{1, 4}, 2: []int{2, 5}}
|
||||
```
|
||||
|
||||
Parallel processing: like `lo.GroupBy()`, but callback is called in goroutine.
|
||||
|
||||
```go
|
||||
import lop "github.com/samber/lo/parallel"
|
||||
|
||||
lop.GroupBy[int, int]([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
|
||||
return i%3
|
||||
})
|
||||
// map[int][]int{0: []int{0, 3}, 1: []int{1, 4}, 2: []int{2, 5}}
|
||||
```
|
||||
|
||||
### Chunk
|
||||
|
||||
Returns an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.
|
||||
|
||||
```go
|
||||
lo.Chunk[int]([]int{0, 1, 2, 3, 4, 5}, 2)
|
||||
// [][]int{{0, 1}, {2, 3}, {4, 5}}
|
||||
|
||||
lo.Chunk[int]([]int{0, 1, 2, 3, 4, 5, 6}, 2)
|
||||
// [][]int{{0, 1}, {2, 3}, {4, 5}, {6}}
|
||||
|
||||
lo.Chunk[int]([]int{}, 2)
|
||||
// [][]int{}
|
||||
|
||||
lo.Chunk[int]([]int{0}, 2)
|
||||
// [][]int{{0}}
|
||||
```
|
||||
|
||||
### PartitionBy
|
||||
|
||||
Returns an array of elements split into groups. The order of grouped values is determined by the order they occur in collection. The grouping is generated from the results of running each element of collection through iteratee.
|
||||
|
||||
```go
|
||||
import lo "github.com/samber/lo"
|
||||
|
||||
partitions := lo.PartitionBy[int, string]([]int{-2, -1, 0, 1, 2, 3, 4, 5}, func(x int) string {
|
||||
if x < 0 {
|
||||
return "negative"
|
||||
} else if x%2 == 0 {
|
||||
return "even"
|
||||
}
|
||||
return "odd"
|
||||
})
|
||||
// [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}}
|
||||
```
|
||||
|
||||
Parallel processing: like `lo.PartitionBy()`, but callback is called in goroutine. Results are returned in the same order.
|
||||
|
||||
```go
|
||||
import lop "github.com/samber/lo/parallel"
|
||||
|
||||
partitions := lo.PartitionBy[int, string]([]int{-2, -1, 0, 1, 2, 3, 4, 5}, func(x int) string {
|
||||
if x < 0 {
|
||||
return "negative"
|
||||
} else if x%2 == 0 {
|
||||
return "even"
|
||||
}
|
||||
return "odd"
|
||||
})
|
||||
// [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}}
|
||||
```
|
||||
|
||||
### Flatten
|
||||
|
||||
Returns an array a single level deep.
|
||||
|
||||
```go
|
||||
flat := lo.Flatten[int]([][]int{{0, 1}, {2, 3, 4, 5}})
|
||||
// []int{0, 1, 2, 3, 4, 5}
|
||||
```
|
||||
|
||||
### Shuffle
|
||||
|
||||
Returns an array of shuffled values. Uses the Fisher-Yates shuffle algorithm.
|
||||
|
||||
```go
|
||||
randomOrder := lo.Shuffle[int]([]int{0, 1, 2, 3, 4, 5})
|
||||
// []int{0, 1, 2, 3, 4, 5}
|
||||
```
|
||||
|
||||
### Reverse
|
||||
|
||||
Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
|
||||
|
||||
```go
|
||||
reverseOder := lo.Reverse[int]([]int{0, 1, 2, 3, 4, 5})
|
||||
// []int{5, 4, 3, 2, 1, 0}
|
||||
```
|
||||
|
||||
### Fill
|
||||
|
||||
Fills elements of array with `initial` value.
|
||||
|
||||
```go
|
||||
type foo struct {
|
||||
bar string
|
||||
}
|
||||
|
||||
func (f foo) Clone() foo {
|
||||
return foo{f.bar}
|
||||
}
|
||||
|
||||
initializedSlice := lo.Fill[foo]([]foo{foo{"a"}, foo{"a"}}, foo{"b"})
|
||||
// []foo{foo{"b"}, foo{"b"}}
|
||||
```
|
||||
|
||||
### Repeat
|
||||
|
||||
Builds a slice with N copies of initial value.
|
||||
|
||||
```go
|
||||
type foo struct {
|
||||
bar string
|
||||
}
|
||||
|
||||
func (f foo) Clone() foo {
|
||||
return foo{f.bar}
|
||||
}
|
||||
|
||||
initializedSlice := lo.Repeat[foo](2, foo{"a"})
|
||||
// []foo{foo{"a"}, foo{"a"}}
|
||||
```
|
||||
|
||||
### KeyBy
|
||||
|
||||
Transforms a slice or an array of structs to a map based on a pivot callback.
|
||||
|
||||
```go
|
||||
m := lo.KeyBy[int, string]([]string{"a", "aa", "aaa"}, func(str string) int {
|
||||
return len(str)
|
||||
})
|
||||
// map[int]string{1: "a", 2: "aa", 3: "aaa"}
|
||||
|
||||
type Character struct {
|
||||
dir string
|
||||
code int
|
||||
}
|
||||
characters := []Character{
|
||||
{dir: "left", code: 97},
|
||||
{dir: "right", code: 100},
|
||||
}
|
||||
result := KeyBy[Character, string](characters, func(char Character) string {
|
||||
return string(rune(char.code))
|
||||
})
|
||||
//map[a:{dir:left code:97} d:{dir:right code:100}]
|
||||
```
|
||||
|
||||
### Drop
|
||||
|
||||
Drops n elements from the beginning of a slice or array.
|
||||
|
||||
```go
|
||||
l := lo.Drop[int]([]int{0, 1, 2, 3, 4, 5}, 2)
|
||||
// []int{2, 3, 4, 5}
|
||||
```
|
||||
|
||||
### DropRight
|
||||
|
||||
Drops n elements from the end of a slice or array.
|
||||
|
||||
```go
|
||||
l := lo.DropRight[int]([]int{0, 1, 2, 3, 4, 5}, 2)
|
||||
// []int{0, 1, 2, 3}
|
||||
```
|
||||
|
||||
### DropWhile
|
||||
|
||||
Drop elements from the beginning of a slice or array while the predicate returns true.
|
||||
|
||||
```go
|
||||
l := lo.DropWhile[string]([]string{"a", "aa", "aaa", "aa", "aa"}, func(val string) bool {
|
||||
return len(val) <= 2
|
||||
})
|
||||
// []string{"aaa", "aa", "a"}
|
||||
```
|
||||
|
||||
### DropRightWhile
|
||||
|
||||
Drop elements from the end of a slice or array while the predicate returns true.
|
||||
|
||||
```go
|
||||
l := lo.DropRightWhile[string]([]string{"a", "aa", "aaa", "aa", "aa"}, func(val string) bool {
|
||||
return len(val) <= 2
|
||||
})
|
||||
// []string{"a", "aa", "aaa"}
|
||||
```
|
||||
|
||||
### Keys
|
||||
|
||||
Creates an array of the map keys.
|
||||
|
||||
```go
|
||||
keys := lo.Keys[string, int](map[string]int{"foo": 1, "bar": 2})
|
||||
// []string{"bar", "foo"}
|
||||
```
|
||||
|
||||
### Values
|
||||
|
||||
Creates an array of the map values.
|
||||
|
||||
```go
|
||||
values := lo.Values[string, int](map[string]int{"foo": 1, "bar": 2})
|
||||
// []int{1, 2}
|
||||
```
|
||||
|
||||
### Entries
|
||||
|
||||
Transforms a map into array of key/value pairs.
|
||||
|
||||
```go
|
||||
entries := lo.Entries[string, int](map[string]int{"foo": 1, "bar": 2})
|
||||
// []lo.Entry[string, int]{
|
||||
// {
|
||||
// Key: "foo",
|
||||
// Value: 1,
|
||||
// },
|
||||
// {
|
||||
// Key: "bar",
|
||||
// Value: 2,
|
||||
// },
|
||||
// }
|
||||
```
|
||||
|
||||
### FromEntries
|
||||
|
||||
Transforms an array of key/value pairs into a map.
|
||||
|
||||
```go
|
||||
m := lo.FromEntries[string, int]([]lo.Entry[string, int]{
|
||||
{
|
||||
Key: "foo",
|
||||
Value: 1,
|
||||
},
|
||||
{
|
||||
Key: "bar",
|
||||
Value: 2,
|
||||
},
|
||||
})
|
||||
// map[string]int{"foo": 1, "bar": 2}
|
||||
```
|
||||
|
||||
### Assign
|
||||
|
||||
Merges multiple maps from left to right.
|
||||
|
||||
```go
|
||||
mergedMaps := lo.Assign[string, int](
|
||||
map[string]int{"a": 1, "b": 2},
|
||||
map[string]int{"b": 3, "c": 4},
|
||||
)
|
||||
// map[string]int{"a": 1, "b": 3, "c": 4}
|
||||
```
|
||||
|
||||
### MapValues
|
||||
|
||||
Manipulates a map values and transforms it to a map of another type.
|
||||
|
||||
```go
|
||||
m1 := map[int]int64{1: 1, 2: 2, 3: 3}
|
||||
|
||||
m2 := lo.MapValues[int, int64, string](m, func(x int64, _ int) string {
|
||||
return strconv.FormatInt(x, 10)
|
||||
})
|
||||
// map[int]string{1: "1", 2: "2", 3: "3"}
|
||||
```
|
||||
|
||||
### Zip2 -> Zip9
|
||||
|
||||
Zip creates a slice of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.
|
||||
|
||||
When collections have different size, the Tuple attributes are filled with zero value.
|
||||
|
||||
```go
|
||||
tuples := lo.Zip2[string, int]([]string{"a", "b"}, []int{1, 2})
|
||||
// []Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}}
|
||||
```
|
||||
|
||||
### Unzip2 -> Unzip9
|
||||
|
||||
Unzip accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.
|
||||
|
||||
```go
|
||||
a, b := lo.Unzip2[string, int]([]Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}})
|
||||
// []string{"a", "b"}
|
||||
// []int{1, 2}
|
||||
```
|
||||
|
||||
### Every
|
||||
|
||||
Returns true if all elements of a subset are contained into a collection.
|
||||
|
||||
```go
|
||||
ok := lo.Every[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2})
|
||||
// true
|
||||
|
||||
ok := lo.Every[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 6})
|
||||
// false
|
||||
```
|
||||
|
||||
### Some
|
||||
|
||||
Returns true if at least 1 element of a subset is contained into a collection.
|
||||
|
||||
```go
|
||||
ok := lo.Some[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2})
|
||||
// true
|
||||
|
||||
ok := lo.Some[int]([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6})
|
||||
// false
|
||||
```
|
||||
|
||||
### Intersect
|
||||
|
||||
Returns the intersection between two collections.
|
||||
|
||||
```go
|
||||
result1 := lo.Intersect[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2})
|
||||
// []int{0, 2}
|
||||
|
||||
result2 := lo.Intersect[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 6}
|
||||
// []int{0}
|
||||
|
||||
result3 := lo.Intersect[int]([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6})
|
||||
// []int{}
|
||||
```
|
||||
|
||||
### 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.
|
||||
|
||||
```go
|
||||
left, right := lo.Difference[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2, 6})
|
||||
// []int{1, 3, 4, 5}, []int{6}
|
||||
|
||||
left, right := Difference[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 1, 2, 3, 4, 5})
|
||||
// []int{}, []int{}
|
||||
```
|
||||
|
||||
### Union
|
||||
|
||||
Returns all distinct elements from both collections. Result will not change the order of elements relatively.
|
||||
|
||||
```go
|
||||
union := lo.Union[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2, 10})
|
||||
// []int{0, 1, 2, 3, 4, 5, 10}
|
||||
```
|
||||
|
||||
### IndexOf
|
||||
|
||||
Returns the index at which the first occurrence of a value is found in an array or return -1 if the value cannot be found.
|
||||
|
||||
```go
|
||||
found := lo.IndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 2)
|
||||
// 2
|
||||
|
||||
notFound := lo.IndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 6)
|
||||
// -1
|
||||
```
|
||||
|
||||
### LastIndex
|
||||
|
||||
Returns the index at which the last occurrence of a value is found in an array or return -1 if the value cannot be found.
|
||||
|
||||
```go
|
||||
found := lo.LastIndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 2)
|
||||
// 4
|
||||
|
||||
notFound := lo.LastIndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 6)
|
||||
// -1
|
||||
```
|
||||
|
||||
### Find
|
||||
|
||||
Search an element in a slice based on a predicate. It returns element and true if element was found.
|
||||
|
||||
```go
|
||||
str, ok := lo.Find[string]([]string{"a", "b", "c", "d"}, func(i string) bool {
|
||||
return i == "b"
|
||||
})
|
||||
// "b", true
|
||||
|
||||
str, ok := lo.Find[string]([]string{"foobar"}, func(i string) bool {
|
||||
return i == "b"
|
||||
})
|
||||
// "", false
|
||||
```
|
||||
|
||||
### Min
|
||||
|
||||
Search the minimum value of a collection.
|
||||
|
||||
```go
|
||||
min := lo.Min[int]([]int{1, 2, 3})
|
||||
// 1
|
||||
|
||||
min := lo.Min[int]([]int{})
|
||||
// 0
|
||||
```
|
||||
|
||||
### Max
|
||||
|
||||
Search the maximum value of a collection.
|
||||
|
||||
```go
|
||||
max := lo.Max[int]([]int{1, 2, 3})
|
||||
// 3
|
||||
|
||||
max := lo.Max[int]([]int{})
|
||||
// 0
|
||||
```
|
||||
|
||||
### Last
|
||||
|
||||
Returns the last element of a collection or error if empty.
|
||||
|
||||
```go
|
||||
last, err := lo.Last[int]([]int{1, 2, 3})
|
||||
// 3
|
||||
```
|
||||
|
||||
### Nth
|
||||
|
||||
Returns the element at index `nth` of collection. If `nth` is negative, the nth element from the end is returned. An error is returned when nth is out of slice bounds.
|
||||
|
||||
```go
|
||||
nth, err := lo.Nth[int]([]int{0, 1, 2, 3}, 2)
|
||||
// 2
|
||||
|
||||
nth, err := lo.Nth[int]([]int{0, 1, 2, 3}, -2)
|
||||
// 2
|
||||
```
|
||||
|
||||
### Sample
|
||||
|
||||
Returns a random item from collection.
|
||||
|
||||
```go
|
||||
lo.Sample[string]([]string{"a", "b", "c"})
|
||||
// a random string from []string{"a", "b", "c"}
|
||||
|
||||
lo.Sample[string]([]string{})
|
||||
// ""
|
||||
```
|
||||
|
||||
### Samples
|
||||
|
||||
Returns N random unique items from collection.
|
||||
|
||||
```go
|
||||
lo.Samples[string]([]string{"a", "b", "c"}, 3)
|
||||
// []string{"a", "b", "c"} in random order
|
||||
```
|
||||
|
||||
### Ternary
|
||||
|
||||
A 1 line if/else statement.
|
||||
|
||||
```go
|
||||
result := lo.Ternary[string](true, "a", "b")
|
||||
// "a"
|
||||
|
||||
result := lo.Ternary[string](false, "a", "b")
|
||||
// "b"
|
||||
```
|
||||
|
||||
### If / ElseIf / Else
|
||||
|
||||
```go
|
||||
result := lo.If[int](true, 1).
|
||||
ElseIf(false, 2).
|
||||
Else(3)
|
||||
// 1
|
||||
|
||||
result := lo.If[int](false, 1).
|
||||
ElseIf(true, 2).
|
||||
Else(3)
|
||||
// 2
|
||||
|
||||
result := lo.If[int](false, 1).
|
||||
ElseIf(false, 2).
|
||||
Else(3)
|
||||
// 3
|
||||
```
|
||||
|
||||
### Switch / Case / Default
|
||||
|
||||
```go
|
||||
result := lo.Switch[int, string](1).
|
||||
Case(1, "1").
|
||||
Case(2, "2").
|
||||
Default("3")
|
||||
// "1"
|
||||
|
||||
result := lo.Switch[int, string](2).
|
||||
Case(1, "1").
|
||||
Case(2, "2").
|
||||
Default("3")
|
||||
// "2"
|
||||
|
||||
result := lo.Switch[int, string](42).
|
||||
Case(1, "1").
|
||||
Case(2, "2").
|
||||
Default("3")
|
||||
// "3"
|
||||
```
|
||||
|
||||
Using callbacks:
|
||||
|
||||
```go
|
||||
result := lo.Switch[int, string](1).
|
||||
CaseF(1, func() string {
|
||||
return "1"
|
||||
}).
|
||||
CaseF(2, func() string {
|
||||
return "2"
|
||||
}).
|
||||
DefaultF(func() string {
|
||||
return "3"
|
||||
})
|
||||
// "1"
|
||||
```
|
||||
|
||||
### ToPtr
|
||||
|
||||
Returns a pointer copy of value.
|
||||
|
||||
```go
|
||||
ptr := lo.ToPtr[string]("hello world")
|
||||
// *string{"hello world"}
|
||||
```
|
||||
|
||||
### ToSlicePtr
|
||||
|
||||
Returns a slice of pointer copy of value.
|
||||
|
||||
```go
|
||||
ptr := lo.ToSlicePtr[string]([]string{"hello", "world"})
|
||||
// []*string{"hello", "world"}
|
||||
```
|
||||
|
||||
### Attempt
|
||||
|
||||
Invokes a function N times until it returns valid output. Returning either the caught error or nil. When first argument is less than `1`, the function runs until a sucessfull response is returned.
|
||||
|
||||
```go
|
||||
iter, err := lo.Attempt(42, func(i int) error {
|
||||
if i == 5 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("failed")
|
||||
})
|
||||
// 6
|
||||
// nil
|
||||
|
||||
iter, err := lo.Attempt(2, func(i int) error {
|
||||
if i == 5 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("failed")
|
||||
})
|
||||
// 2
|
||||
// error "failed"
|
||||
|
||||
iter, err := lo.Attempt(0, func(i int) error {
|
||||
if i < 42 {
|
||||
return fmt.Errorf("failed")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
// 43
|
||||
// nil
|
||||
```
|
||||
|
||||
### Range / RangeFrom / RangeWithSteps
|
||||
|
||||
Creates an array of numbers (positive and/or negative) progressing from start up to, but not including end.
|
||||
|
||||
```go
|
||||
result := Range(4)
|
||||
// [0, 1, 2, 3]
|
||||
|
||||
result := Range(-4);
|
||||
// [0, -1, -2, -3]
|
||||
|
||||
result := RangeFrom(1, 5);
|
||||
// [1, 2, 3, 4]
|
||||
|
||||
result := RangeFrom[float64](1.0, 5);
|
||||
// [1.0, 2.0, 3.0, 4.0]
|
||||
|
||||
result := RangeWithSteps(0, 20, 5);
|
||||
// [0, 5, 10, 15]
|
||||
|
||||
result := RangeWithSteps[float32](-1.0, -4.0, -1.0);
|
||||
// [-1.0, -2.0, -3.0]
|
||||
|
||||
result := RangeWithSteps(1, 4, -1);
|
||||
// []
|
||||
|
||||
result := Range(0);
|
||||
// []
|
||||
```
|
||||
|
||||
For more advanced retry strategies (delay, exponential backoff...), please take a look on [cenkalti/backoff](https://github.com/cenkalti/backoff).
|
||||
|
||||
## 🛩 Benchmark
|
||||
|
||||
We executed a simple benchmark with the a dead-simple `lo.Map` loop:
|
||||
|
||||
See the full implementation [here](./benchmark_test.go).
|
||||
|
||||
```go
|
||||
_ = lo.Map[int64](arr, func(x int64, i int) string {
|
||||
return strconv.FormatInt(x, 10)
|
||||
})
|
||||
```
|
||||
|
||||
**Result:**
|
||||
|
||||
Here is a comparison between `lo.Map`, `lop.Map`, `go-funk` library and a simple Go `for` loop.
|
||||
|
||||
```
|
||||
$ go test -benchmem -bench ./...
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/samber/lo
|
||||
cpu: Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
|
||||
cpu: Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz
|
||||
BenchmarkMap/lo.Map-8 8 132728237 ns/op 39998945 B/op 1000002 allocs/op
|
||||
BenchmarkMap/lop.Map-8 2 503947830 ns/op 119999956 B/op 3000007 allocs/op
|
||||
BenchmarkMap/reflect-8 2 826400560 ns/op 170326512 B/op 4000042 allocs/op
|
||||
BenchmarkMap/for-8 9 126252954 ns/op 39998674 B/op 1000001 allocs/op
|
||||
PASS
|
||||
ok github.com/samber/lo 6.657s
|
||||
```
|
||||
|
||||
- `lo.Map` is way faster (x7) than `go-funk`, a relection-based Map implementation.
|
||||
- `lo.Map` have the same allocation profile than `for`.
|
||||
- `lo.Map` is 4% slower than `for`.
|
||||
- `lop.Map` is slower than `lo.Map` because it implies more memory allocation and locks. `lop.Map` will be usefull for long-running callbacks, such as i/o bound processing.
|
||||
- `for` beats other implementations for memory and CPU.
|
||||
|
||||
## 🤝 Contributing
|
||||
|
||||
- Ping me on twitter [@samuelberthe](https://twitter.com/samuelberthe) (DMs, mentions, whatever :))
|
||||
- Fork the [project](https://github.com/samber/lo)
|
||||
- Fix [open issues](https://github.com/samber/lo/issues) or request new features
|
||||
|
||||
Don't hesitate ;)
|
||||
|
||||
### Install go 1.18
|
||||
|
||||
```bash
|
||||
make go1.18beta1
|
||||
```
|
||||
|
||||
If your OS currently not default to Go 1.18, replace `BIN=go` by `BIN=go1.18beta1` in the Makefile.
|
||||
|
||||
### With Docker
|
||||
|
||||
```bash
|
||||
docker-compose run --rm dev
|
||||
```
|
||||
|
||||
### Without Docker
|
||||
|
||||
```bash
|
||||
# Install some dev dependencies
|
||||
make tools
|
||||
|
||||
# Run tests
|
||||
make test
|
||||
# or
|
||||
make watch-test
|
||||
```
|
||||
|
||||
## 👤 Authors
|
||||
|
||||
- Samuel Berthe
|
||||
|
||||
## 💫 Show your support
|
||||
|
||||
Give a ⭐️ if this project helped you!
|
||||
|
||||
[](https://www.patreon.com/samber)
|
||||
|
||||
## 📝 License
|
||||
|
||||
Copyright © 2022 [Samuel Berthe](https://github.com/samber).
|
||||
|
||||
This project is [MIT](./LICENSE) licensed.
|
99
vendor/github.com/samber/lo/condition.go
generated
vendored
Normal file
99
vendor/github.com/samber/lo/condition.go
generated
vendored
Normal file
@ -0,0 +1,99 @@
|
||||
package lo
|
||||
|
||||
// Ternary is a 1 line if/else statement.
|
||||
func Ternary[T any](condition bool, ifOutput T, elseOutput T) T {
|
||||
if condition {
|
||||
return ifOutput
|
||||
}
|
||||
|
||||
return elseOutput
|
||||
}
|
||||
|
||||
type ifElse[T any] struct {
|
||||
result T
|
||||
done bool
|
||||
}
|
||||
|
||||
// If.
|
||||
func If[T any](condition bool, result T) *ifElse[T] {
|
||||
if condition {
|
||||
return &ifElse[T]{result, true}
|
||||
}
|
||||
|
||||
var t T
|
||||
return &ifElse[T]{t, false}
|
||||
}
|
||||
|
||||
// ElseIf.
|
||||
func (i *ifElse[T]) ElseIf(condition bool, result T) *ifElse[T] {
|
||||
if !i.done && condition {
|
||||
i.result = result
|
||||
i.done = true
|
||||
}
|
||||
|
||||
return i
|
||||
}
|
||||
|
||||
// Else.
|
||||
func (i *ifElse[T]) Else(result T) T {
|
||||
if i.done {
|
||||
return i.result
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
type switchCase[T comparable, R any] struct {
|
||||
predicate T
|
||||
result R
|
||||
done bool
|
||||
}
|
||||
|
||||
// Switch is a pure functional switch/case/default statement.
|
||||
func Switch[T comparable, R any](predicate T) *switchCase[T, R] {
|
||||
var result R
|
||||
|
||||
return &switchCase[T, R]{
|
||||
predicate,
|
||||
result,
|
||||
false,
|
||||
}
|
||||
}
|
||||
|
||||
// Case.
|
||||
func (s *switchCase[T, R]) Case(val T, result R) *switchCase[T, R] {
|
||||
if !s.done && s.predicate == val {
|
||||
s.result = result
|
||||
s.done = true
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// CaseF.
|
||||
func (s *switchCase[T, R]) CaseF(val T, cb func() R) *switchCase[T, R] {
|
||||
if !s.done && s.predicate == val {
|
||||
s.result = cb()
|
||||
s.done = true
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// Default.
|
||||
func (s *switchCase[T, R]) Default(result R) R {
|
||||
if !s.done {
|
||||
s.result = result
|
||||
}
|
||||
|
||||
return s.result
|
||||
}
|
||||
|
||||
// DefaultF.
|
||||
func (s *switchCase[T, R]) DefaultF(cb func() R) R {
|
||||
if !s.done {
|
||||
s.result = cb()
|
||||
}
|
||||
|
||||
return s.result
|
||||
}
|
6
vendor/github.com/samber/lo/constraints.go
generated
vendored
Normal file
6
vendor/github.com/samber/lo/constraints.go
generated
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
package lo
|
||||
|
||||
// Clonable defines a constraint of types having Clone() T method.
|
||||
type Clonable[T any] interface {
|
||||
Clone() T
|
||||
}
|
9
vendor/github.com/samber/lo/docker-compose.yml
generated
vendored
Normal file
9
vendor/github.com/samber/lo/docker-compose.yml
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
dev:
|
||||
build: .
|
||||
volumes:
|
||||
- ./:/go/src/github.com/samber/lo
|
||||
working_dir: /go/src/github.com/samber/lo
|
||||
command: bash -c 'make tools ; make watch-test'
|
65
vendor/github.com/samber/lo/drop.go
generated
vendored
Normal file
65
vendor/github.com/samber/lo/drop.go
generated
vendored
Normal file
@ -0,0 +1,65 @@
|
||||
package lo
|
||||
|
||||
//Drop drops n elements from the beginning of a slice or array.
|
||||
func Drop[T any](collection []T, n int) []T {
|
||||
if len(collection) <= n {
|
||||
return make([]T, 0)
|
||||
}
|
||||
|
||||
result := make([]T, len(collection)-n)
|
||||
for i := n; i < len(collection); i++ {
|
||||
result[i-n] = collection[i]
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
//DropWhile drops elements from the beginning of a slice or array while the predicate returns true.
|
||||
func DropWhile[T any](collection []T, predicate func(T) bool) []T {
|
||||
i := 0
|
||||
for ; i < len(collection); i++ {
|
||||
if !predicate(collection[i]) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
result := make([]T, len(collection)-i)
|
||||
|
||||
for j := 0; i < len(collection); i, j = i+1, j+1 {
|
||||
result[j] = collection[i]
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
//DropRight drops n elements from the end of a slice or array.
|
||||
func DropRight[T any](collection []T, n int) []T {
|
||||
if len(collection) <= n {
|
||||
return make([]T, 0)
|
||||
}
|
||||
|
||||
result := make([]T, len(collection)-n)
|
||||
for i := len(collection) - 1 - n; i != 0; i-- {
|
||||
result[i] = collection[i]
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
//DropRightWhile drops elements from the end of a slice or array while the predicate returns true.
|
||||
func DropRightWhile[T any](collection []T, predicate func(T) bool) []T {
|
||||
i := len(collection) - 1
|
||||
for ; i >= 0; i-- {
|
||||
if !predicate(collection[i]) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
result := make([]T, i+1)
|
||||
|
||||
for ; i >= 0; i-- {
|
||||
result[i] = collection[i]
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
157
vendor/github.com/samber/lo/find.go
generated
vendored
Normal file
157
vendor/github.com/samber/lo/find.go
generated
vendored
Normal file
@ -0,0 +1,157 @@
|
||||
package lo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"math"
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
// import "golang.org/x/exp/constraints"
|
||||
|
||||
// IndexOf returns the index at which the first occurrence of a value is found in an array or return -1
|
||||
// if the value cannot be found.
|
||||
func IndexOf[T comparable](collection []T, element T) int {
|
||||
for i, item := range collection {
|
||||
if item == element {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
// IndexOf returns the index at which the last occurrence of a value is found in an array or return -1
|
||||
// if the value cannot be found.
|
||||
func LastIndexOf[T comparable](collection []T, element T) int {
|
||||
length := len(collection)
|
||||
|
||||
for i := length - 1; i >= 0; i-- {
|
||||
if collection[i] == element {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
// Find search an element in a slice based on a predicate. It returns element and true if element was found.
|
||||
func Find[T any](collection []T, predicate func(T) bool) (T, bool) {
|
||||
for _, item := range collection {
|
||||
if predicate(item) {
|
||||
return item, true
|
||||
}
|
||||
}
|
||||
|
||||
var result T
|
||||
return result, false
|
||||
}
|
||||
|
||||
// Min search the minimum value of a collection.
|
||||
func Min[T constraints.Ordered](collection []T) T {
|
||||
var min T
|
||||
|
||||
if len(collection) == 0 {
|
||||
return min
|
||||
}
|
||||
|
||||
min = collection[0]
|
||||
|
||||
for i := 1; i < len(collection); i++ {
|
||||
item := collection[i]
|
||||
|
||||
// if item.Less(min) {
|
||||
if item < min {
|
||||
min = item
|
||||
}
|
||||
}
|
||||
|
||||
return min
|
||||
}
|
||||
|
||||
// Max search the maximum value of a collection.
|
||||
func Max[T constraints.Ordered](collection []T) T {
|
||||
var max T
|
||||
|
||||
if len(collection) == 0 {
|
||||
return max
|
||||
}
|
||||
|
||||
max = collection[0]
|
||||
|
||||
for i := 1; i < len(collection); i++ {
|
||||
item := collection[i]
|
||||
|
||||
if item > max {
|
||||
max = item
|
||||
}
|
||||
}
|
||||
|
||||
return max
|
||||
}
|
||||
|
||||
// Last returns the last element of a collection or error if empty.
|
||||
func Last[T any](collection []T) (T, error) {
|
||||
length := len(collection)
|
||||
|
||||
if length == 0 {
|
||||
var t T
|
||||
return t, fmt.Errorf("last: cannot extract the last element of an empty slice")
|
||||
}
|
||||
|
||||
return collection[length-1], nil
|
||||
}
|
||||
|
||||
// Nth returns the element at index `nth` of collection. If `nth` is negative, the nth element
|
||||
// from the end is returned. An error is returned when nth is out of slice bounds.
|
||||
func Nth[T any](collection []T, nth int) (T, error) {
|
||||
if int(math.Abs(float64(nth))) >= len(collection) {
|
||||
var t T
|
||||
return t, fmt.Errorf("nth: %d out of slice bounds", nth)
|
||||
}
|
||||
|
||||
length := len(collection)
|
||||
|
||||
if nth >= 0 {
|
||||
return collection[nth], nil
|
||||
}
|
||||
|
||||
return collection[length+nth], nil
|
||||
}
|
||||
|
||||
// Sample returns a random item from collection.
|
||||
func Sample[T any](collection []T) T {
|
||||
size := len(collection)
|
||||
if size == 0 {
|
||||
return Empty[T]()
|
||||
}
|
||||
|
||||
return collection[rand.Intn(size)]
|
||||
}
|
||||
|
||||
// Samples returns N random unique items from collection.
|
||||
func Samples[T any](collection []T, count int) []T {
|
||||
size := len(collection)
|
||||
|
||||
// put values into a map, for faster deletion
|
||||
cOpy := make([]T, 0, size)
|
||||
for _, v := range collection {
|
||||
cOpy = append(cOpy, v)
|
||||
}
|
||||
|
||||
results := []T{}
|
||||
|
||||
for i := 0; i < size && i < count; i++ {
|
||||
copyLength := size - i
|
||||
|
||||
index := rand.Intn(size - i)
|
||||
results = append(results, cOpy[index])
|
||||
|
||||
// Removes element.
|
||||
// It is faster to swap with last element and remove it.
|
||||
cOpy[index] = cOpy[copyLength-1]
|
||||
cOpy = cOpy[:copyLength-1]
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
131
vendor/github.com/samber/lo/intersect.go
generated
vendored
Normal file
131
vendor/github.com/samber/lo/intersect.go
generated
vendored
Normal file
@ -0,0 +1,131 @@
|
||||
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(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.
|
||||
func Every[T comparable](collection []T, subset []T) bool {
|
||||
for _, elem := range subset {
|
||||
if !Contains(collection, elem) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Some returns true if at least 1 element of a subset is contained into a collection.
|
||||
func Some[T comparable](collection []T, subset []T) bool {
|
||||
for _, elem := range subset {
|
||||
if Contains(collection, elem) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Intersect returns the intersection between two collections.
|
||||
func Intersect[T comparable](list1 []T, 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 []T, list2 []T) ([]T, []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 both collections.
|
||||
// result returns will not change the order of elements relatively.
|
||||
func Union[T comparable](list1 []T, list2 []T) []T {
|
||||
result := []T{}
|
||||
|
||||
seen := map[T]struct{}{}
|
||||
hasAdd := map[T]struct{}{}
|
||||
|
||||
for _, e := range list1 {
|
||||
seen[e] = struct{}{}
|
||||
}
|
||||
|
||||
for _, e := range list2 {
|
||||
seen[e] = struct{}{}
|
||||
}
|
||||
|
||||
for _, e := range list1 {
|
||||
if _, ok := seen[e]; ok {
|
||||
result = append(result, e)
|
||||
hasAdd[e] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
for _, e := range list2 {
|
||||
if _, ok := hasAdd[e]; ok {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[e]; ok {
|
||||
result = append(result, e)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
72
vendor/github.com/samber/lo/map.go
generated
vendored
Normal file
72
vendor/github.com/samber/lo/map.go
generated
vendored
Normal file
@ -0,0 +1,72 @@
|
||||
package lo
|
||||
|
||||
// Keys creates an array of the map keys.
|
||||
func Keys[K comparable, V any](in map[K]V) []K {
|
||||
result := make([]K, 0, len(in))
|
||||
|
||||
for k, _ := range in {
|
||||
result = append(result, k)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Values creates an array of the map values.
|
||||
func Values[K comparable, V any](in map[K]V) []V {
|
||||
result := make([]V, 0, len(in))
|
||||
|
||||
for _, v := range in {
|
||||
result = append(result, v)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Entries transforms a map into array of key/value pairs.
|
||||
func Entries[K comparable, V any](in map[K]V) []Entry[K, V] {
|
||||
entries := make([]Entry[K, V], 0, len(in))
|
||||
|
||||
for k, v := range in {
|
||||
entries = append(entries, Entry[K, V]{
|
||||
Key: k,
|
||||
Value: v,
|
||||
})
|
||||
}
|
||||
|
||||
return entries
|
||||
}
|
||||
|
||||
// FromEntries transforms an array of key/value pairs into a map.
|
||||
func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V {
|
||||
out := map[K]V{}
|
||||
|
||||
for _, v := range entries {
|
||||
out[v.Key] = v.Value
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// Assign merges multiple maps from left to right.
|
||||
func Assign[K comparable, V any](maps ...map[K]V) map[K]V {
|
||||
out := map[K]V{}
|
||||
|
||||
for _, m := range maps {
|
||||
for k, v := range m {
|
||||
out[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// MapValues manipulates a map values and transforms it to a map of another type.
|
||||
func MapValues[K comparable, V any, R any](in map[K]V, iteratee func(V, K) R) map[K]R {
|
||||
result := map[K]R{}
|
||||
|
||||
for k, v := range in {
|
||||
result[k] = iteratee(v, k)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
19
vendor/github.com/samber/lo/pointers.go
generated
vendored
Normal file
19
vendor/github.com/samber/lo/pointers.go
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
package lo
|
||||
|
||||
// ToPtr returns a pointer copy of value.
|
||||
func ToPtr[T any](x T) *T {
|
||||
return &x
|
||||
}
|
||||
|
||||
// ToPtr returns a slice of pointer copy of value.
|
||||
func ToSlicePtr[T any](collection []T) []*T {
|
||||
return Map(collection, func (x T, _ int) *T {
|
||||
return &x
|
||||
})
|
||||
}
|
||||
|
||||
// Empty returns an empty value.
|
||||
func Empty[T any]() T {
|
||||
var t T
|
||||
return t
|
||||
}
|
19
vendor/github.com/samber/lo/retry.go
generated
vendored
Normal file
19
vendor/github.com/samber/lo/retry.go
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
package lo
|
||||
|
||||
// Attempt invokes a function N times until it returns valid output. Returning either the caught error or nil. When first argument is less than `1`, the function runs until a sucessfull response is returned.
|
||||
func Attempt(maxIteration int, f func(int) error) (int, error) {
|
||||
var err error
|
||||
|
||||
for i := 0; maxIteration <= 0 || i < maxIteration; i++ {
|
||||
// for retries >= 0 {
|
||||
err = f(i)
|
||||
if err == nil {
|
||||
return i + 1, nil
|
||||
}
|
||||
}
|
||||
|
||||
return maxIteration, err
|
||||
}
|
||||
|
||||
// throttle ?
|
||||
// debounce ?
|
242
vendor/github.com/samber/lo/slice.go
generated
vendored
Normal file
242
vendor/github.com/samber/lo/slice.go
generated
vendored
Normal file
@ -0,0 +1,242 @@
|
||||
package lo
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
// Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for.
|
||||
func Filter[V any](collection []V, predicate func(V, int) bool) []V {
|
||||
result := []V{}
|
||||
|
||||
for i, item := range collection {
|
||||
if predicate(item, i) {
|
||||
result = append(result, item)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Map manipulates a slice and transforms it to a slice of another type.
|
||||
func Map[T any, R any](collection []T, iteratee func(T, int) R) []R {
|
||||
result := make([]R, len(collection))
|
||||
|
||||
for i, item := range collection {
|
||||
result[i] = iteratee(item, i)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// FlatMap manipulates a slice and transforms and flattens it to a slice of another type.
|
||||
func FlatMap[T any, R any](collection []T, iteratee func(T, int) []R) []R {
|
||||
result := []R{}
|
||||
|
||||
for i, item := range collection {
|
||||
result = append(result, iteratee(item, i)...)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Reduce reduces collection to a value which is the accumulated result of running each element in collection
|
||||
// through accumulator, where each successive invocation is supplied the return value of the previous.
|
||||
func Reduce[T any, R any](collection []T, accumulator func(R, T, int) R, initial R) R {
|
||||
for i, item := range collection {
|
||||
initial = accumulator(initial, item, i)
|
||||
}
|
||||
|
||||
return initial
|
||||
}
|
||||
|
||||
// ForEach iterates over elements of collection and invokes iteratee for each element.
|
||||
func ForEach[T any](collection []T, iteratee func(T, int)) {
|
||||
for i, item := range collection {
|
||||
iteratee(item, i)
|
||||
}
|
||||
}
|
||||
|
||||
// Times invokes the iteratee n times, returning an array of the results of each invocation.
|
||||
// The iteratee is invoked with index as argument.
|
||||
func Times[T any](count int, iteratee func(int) T) []T {
|
||||
result := make([]T, count)
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
result[i] = iteratee(i)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Uniq returns a duplicate-free version of an array, in which only the first occurrence of each element is kept.
|
||||
// The order of result values is determined by the order they occur in the array.
|
||||
func Uniq[T comparable](collection []T) []T {
|
||||
result := make([]T, 0, len(collection))
|
||||
seen := make(map[T]struct{}, len(collection))
|
||||
|
||||
for _, item := range collection {
|
||||
if _, ok := seen[item]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
seen[item] = struct{}{}
|
||||
result = append(result, item)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Uniq returns a duplicate-free version of an array, in which only the first occurrence of each element is kept.
|
||||
// The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is
|
||||
// invoked for each element in array to generate the criterion by which uniqueness is computed.
|
||||
func UniqBy[T any, U comparable](collection []T, iteratee func(T) U) []T {
|
||||
result := make([]T, 0, len(collection))
|
||||
seen := make(map[U]struct{}, len(collection))
|
||||
|
||||
for _, item := range collection {
|
||||
key := iteratee(item)
|
||||
|
||||
if _, ok := seen[key]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
seen[key] = struct{}{}
|
||||
result = append(result, item)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// GroupBy returns an object composed of keys generated from the results of running each element of collection through iteratee.
|
||||
func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T {
|
||||
result := map[U][]T{}
|
||||
|
||||
for _, item := range collection {
|
||||
key := iteratee(item)
|
||||
|
||||
if _, ok := result[key]; !ok {
|
||||
result[key] = []T{}
|
||||
}
|
||||
|
||||
result[key] = append(result[key], item)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Chunk returns an array of elements split into groups the length of size. If array can't be split evenly,
|
||||
// the final chunk will be the remaining elements.
|
||||
func Chunk[T any](collection []T, size int) [][]T {
|
||||
if size <= 0 {
|
||||
panic("Second parameter must be greater than 0")
|
||||
}
|
||||
|
||||
result := make([][]T, 0, len(collection)/2+1)
|
||||
length := len(collection)
|
||||
|
||||
for i := 0; i < length; i++ {
|
||||
chunk := i / size
|
||||
|
||||
if i%size == 0 {
|
||||
result = append(result, make([]T, 0, size))
|
||||
}
|
||||
|
||||
result[chunk] = append(result[chunk], collection[i])
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// PartitionBy returns an array of elements split into groups. The order of grouped values is
|
||||
// determined by the order they occur in collection. The grouping is generated from the results
|
||||
// of running each element of collection through iteratee.
|
||||
func PartitionBy[T any, K comparable](collection []T, iteratee func(x T) K) [][]T {
|
||||
result := [][]T{}
|
||||
seen := map[K]int{}
|
||||
|
||||
for _, item := range collection {
|
||||
key := iteratee(item)
|
||||
|
||||
resultIndex, ok := seen[key]
|
||||
if !ok {
|
||||
resultIndex = len(result)
|
||||
seen[key] = resultIndex
|
||||
result = append(result, []T{})
|
||||
}
|
||||
|
||||
result[resultIndex] = append(result[resultIndex], item)
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
// unordered:
|
||||
// groups := GroupBy[T, K](collection, iteratee)
|
||||
// return Values[K, []T](groups)
|
||||
}
|
||||
|
||||
// Flattens returns an array a single level deep.
|
||||
func Flatten[T any](collection [][]T) []T {
|
||||
result := []T{}
|
||||
|
||||
for _, item := range collection {
|
||||
result = append(result, item...)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Shuffle returns an array of shuffled values. Uses the Fisher-Yates shuffle algorithm.
|
||||
func Shuffle[T any](collection []T) []T {
|
||||
rand.Shuffle(len(collection), func(i, j int) {
|
||||
collection[i], collection[j] = collection[j], collection[i]
|
||||
})
|
||||
|
||||
return collection
|
||||
}
|
||||
|
||||
// Reverse reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
|
||||
func Reverse[T any](collection []T) []T {
|
||||
length := len(collection)
|
||||
half := length / 2
|
||||
|
||||
for i := 0; i < half; i = i + 1 {
|
||||
j := length - 1 - i
|
||||
collection[i], collection[j] = collection[j], collection[i]
|
||||
}
|
||||
|
||||
return collection
|
||||
}
|
||||
|
||||
// Fill fills elements of array with `initial` value.
|
||||
func Fill[T Clonable[T]](collection []T, initial T) []T {
|
||||
result := make([]T, 0, len(collection))
|
||||
|
||||
for _ = range collection {
|
||||
result = append(result, initial.Clone())
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Repeat builds a slice with N copies of initial value.
|
||||
func Repeat[T Clonable[T]](count int, initial T) []T {
|
||||
result := make([]T, 0, count)
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
result = append(result, initial.Clone())
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// KeyBy transforms a slice or an array of structs to a map based on a pivot callback.
|
||||
func KeyBy[K comparable, V any](collection []V, iteratee func(V) K) map[K]V {
|
||||
result := make(map[K]V, len(collection))
|
||||
|
||||
for _, v := range collection {
|
||||
k := iteratee(v)
|
||||
result[k] = v
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
413
vendor/github.com/samber/lo/tuples.go
generated
vendored
Normal file
413
vendor/github.com/samber/lo/tuples.go
generated
vendored
Normal file
@ -0,0 +1,413 @@
|
||||
package lo
|
||||
|
||||
func longestCollection(collections ...[]interface{}) int {
|
||||
max := 0
|
||||
|
||||
for _, collection := range collections {
|
||||
if len(collection) > max {
|
||||
max = len(collection)
|
||||
}
|
||||
}
|
||||
|
||||
return max
|
||||
}
|
||||
|
||||
// Zip2 creates a slice of grouped elements, the first of which contains the first elements
|
||||
// of the given arrays, the second of which contains the second elements of the given arrays, and so on.
|
||||
// When collections have different size, the Tuple attributes are filled with zero value.
|
||||
func Zip2[A any, B any](a []A, b []B) []Tuple2[A, B] {
|
||||
size := Max[int]([]int{len(a), len(b)})
|
||||
|
||||
result := make([]Tuple2[A, B], 0, size)
|
||||
|
||||
for index := 0; index < size; index++ {
|
||||
_a, _ := Nth[A](a, index)
|
||||
_b, _ := Nth[B](b, index)
|
||||
|
||||
result = append(result, Tuple2[A, B]{
|
||||
A: _a,
|
||||
B: _b,
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Zip3 creates a slice of grouped elements, the first of which contains the first elements
|
||||
// of the given arrays, the second of which contains the second elements of the given arrays, and so on.
|
||||
// When collections have different size, the Tuple attributes are filled with zero value.
|
||||
func Zip3[A any, B any, C any](a []A, b []B, c []C) []Tuple3[A, B, C] {
|
||||
size := Max[int]([]int{len(a), len(b), len(c)})
|
||||
|
||||
result := make([]Tuple3[A, B, C], 0, size)
|
||||
|
||||
for index := 0; index < size; index++ {
|
||||
_a, _ := Nth[A](a, index)
|
||||
_b, _ := Nth[B](b, index)
|
||||
_c, _ := Nth[C](c, index)
|
||||
|
||||
result = append(result, Tuple3[A, B, C]{
|
||||
A: _a,
|
||||
B: _b,
|
||||
C: _c,
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Zip4 creates a slice of grouped elements, the first of which contains the first elements
|
||||
// of the given arrays, the second of which contains the second elements of the given arrays, and so on.
|
||||
// When collections have different size, the Tuple attributes are filled with zero value.
|
||||
func Zip4[A any, B any, C any, D any](a []A, b []B, c []C, d []D) []Tuple4[A, B, C, D] {
|
||||
size := Max[int]([]int{len(a), len(b), len(c), len(d)})
|
||||
|
||||
result := make([]Tuple4[A, B, C, D], 0, size)
|
||||
|
||||
for index := 0; index < size; index++ {
|
||||
_a, _ := Nth[A](a, index)
|
||||
_b, _ := Nth[B](b, index)
|
||||
_c, _ := Nth[C](c, index)
|
||||
_d, _ := Nth[D](d, index)
|
||||
|
||||
result = append(result, Tuple4[A, B, C, D]{
|
||||
A: _a,
|
||||
B: _b,
|
||||
C: _c,
|
||||
D: _d,
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Zip5 creates a slice of grouped elements, the first of which contains the first elements
|
||||
// of the given arrays, the second of which contains the second elements of the given arrays, and so on.
|
||||
// When collections have different size, the Tuple attributes are filled with zero value.
|
||||
func Zip5[A any, B any, C any, D any, E any](a []A, b []B, c []C, d []D, e []E) []Tuple5[A, B, C, D, E] {
|
||||
size := Max[int]([]int{len(a), len(b), len(c), len(d), len(e)})
|
||||
|
||||
result := make([]Tuple5[A, B, C, D, E], 0, size)
|
||||
|
||||
for index := 0; index < size; index++ {
|
||||
_a, _ := Nth[A](a, index)
|
||||
_b, _ := Nth[B](b, index)
|
||||
_c, _ := Nth[C](c, index)
|
||||
_d, _ := Nth[D](d, index)
|
||||
_e, _ := Nth[E](e, index)
|
||||
|
||||
result = append(result, Tuple5[A, B, C, D, E]{
|
||||
A: _a,
|
||||
B: _b,
|
||||
C: _c,
|
||||
D: _d,
|
||||
E: _e,
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Zip6 creates a slice of grouped elements, the first of which contains the first elements
|
||||
// of the given arrays, the second of which contains the second elements of the given arrays, and so on.
|
||||
// When collections have different size, the Tuple attributes are filled with zero value.
|
||||
func Zip6[A any, B any, C any, D any, E any, F any](a []A, b []B, c []C, d []D, e []E, f []F) []Tuple6[A, B, C, D, E, F] {
|
||||
size := Max[int]([]int{len(a), len(b), len(c), len(d), len(e), len(f)})
|
||||
|
||||
result := make([]Tuple6[A, B, C, D, E, F], 0, size)
|
||||
|
||||
for index := 0; index < size; index++ {
|
||||
_a, _ := Nth[A](a, index)
|
||||
_b, _ := Nth[B](b, index)
|
||||
_c, _ := Nth[C](c, index)
|
||||
_d, _ := Nth[D](d, index)
|
||||
_e, _ := Nth[E](e, index)
|
||||
_f, _ := Nth[F](f, index)
|
||||
|
||||
result = append(result, Tuple6[A, B, C, D, E, F]{
|
||||
A: _a,
|
||||
B: _b,
|
||||
C: _c,
|
||||
D: _d,
|
||||
E: _e,
|
||||
F: _f,
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Zip7 creates a slice of grouped elements, the first of which contains the first elements
|
||||
// of the given arrays, the second of which contains the second elements of the given arrays, and so on.
|
||||
// When collections have different size, the Tuple attributes are filled with zero value.
|
||||
func Zip7[A any, B any, C any, D any, E any, F any, G any](a []A, b []B, c []C, d []D, e []E, f []F, g []G) []Tuple7[A, B, C, D, E, F, G] {
|
||||
size := Max[int]([]int{len(a), len(b), len(c), len(d), len(e), len(f), len(g)})
|
||||
|
||||
result := make([]Tuple7[A, B, C, D, E, F, G], 0, size)
|
||||
|
||||
for index := 0; index < size; index++ {
|
||||
_a, _ := Nth[A](a, index)
|
||||
_b, _ := Nth[B](b, index)
|
||||
_c, _ := Nth[C](c, index)
|
||||
_d, _ := Nth[D](d, index)
|
||||
_e, _ := Nth[E](e, index)
|
||||
_f, _ := Nth[F](f, index)
|
||||
_g, _ := Nth[G](g, index)
|
||||
|
||||
result = append(result, Tuple7[A, B, C, D, E, F, G]{
|
||||
A: _a,
|
||||
B: _b,
|
||||
C: _c,
|
||||
D: _d,
|
||||
E: _e,
|
||||
F: _f,
|
||||
G: _g,
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Zip8 creates a slice of grouped elements, the first of which contains the first elements
|
||||
// of the given arrays, the second of which contains the second elements of the given arrays, and so on.
|
||||
// When collections have different size, the Tuple attributes are filled with zero value.
|
||||
func Zip8[A any, B any, C any, D any, E any, F any, G any, H any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H) []Tuple8[A, B, C, D, E, F, G, H] {
|
||||
size := Max[int]([]int{len(a), len(b), len(c), len(d), len(e), len(f), len(g), len(h)})
|
||||
|
||||
result := make([]Tuple8[A, B, C, D, E, F, G, H], 0, size)
|
||||
|
||||
for index := 0; index < size; index++ {
|
||||
_a, _ := Nth[A](a, index)
|
||||
_b, _ := Nth[B](b, index)
|
||||
_c, _ := Nth[C](c, index)
|
||||
_d, _ := Nth[D](d, index)
|
||||
_e, _ := Nth[E](e, index)
|
||||
_f, _ := Nth[F](f, index)
|
||||
_g, _ := Nth[G](g, index)
|
||||
_h, _ := Nth[H](h, index)
|
||||
|
||||
result = append(result, Tuple8[A, B, C, D, E, F, G, H]{
|
||||
A: _a,
|
||||
B: _b,
|
||||
C: _c,
|
||||
D: _d,
|
||||
E: _e,
|
||||
F: _f,
|
||||
G: _g,
|
||||
H: _h,
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Zip9 creates a slice of grouped elements, the first of which contains the first elements
|
||||
// of the given arrays, the second of which contains the second elements of the given arrays, and so on.
|
||||
// When collections have different size, the Tuple attributes are filled with zero value.
|
||||
func Zip9[A any, B any, C any, D any, E any, F any, G any, H any, I any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, i []I) []Tuple9[A, B, C, D, E, F, G, H, I] {
|
||||
size := Max[int]([]int{len(a), len(b), len(c), len(d), len(e), len(f), len(g), len(h), len(i)})
|
||||
|
||||
result := make([]Tuple9[A, B, C, D, E, F, G, H, I], 0, size)
|
||||
|
||||
for index := 0; index < size; index++ {
|
||||
_a, _ := Nth[A](a, index)
|
||||
_b, _ := Nth[B](b, index)
|
||||
_c, _ := Nth[C](c, index)
|
||||
_d, _ := Nth[D](d, index)
|
||||
_e, _ := Nth[E](e, index)
|
||||
_f, _ := Nth[F](f, index)
|
||||
_g, _ := Nth[G](g, index)
|
||||
_h, _ := Nth[H](h, index)
|
||||
_i, _ := Nth[I](i, index)
|
||||
|
||||
result = append(result, Tuple9[A, B, C, D, E, F, G, H, I]{
|
||||
A: _a,
|
||||
B: _b,
|
||||
C: _c,
|
||||
D: _d,
|
||||
E: _e,
|
||||
F: _f,
|
||||
G: _g,
|
||||
H: _h,
|
||||
I: _i,
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Unzip2 accepts an array of grouped elements and creates an array regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
func Unzip2[A any, B any](tuples []Tuple2[A, B]) ([]A, []B) {
|
||||
size := len(tuples)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
|
||||
for _, tuple := range tuples {
|
||||
r1 = append(r1, tuple.A)
|
||||
r2 = append(r2, tuple.B)
|
||||
}
|
||||
|
||||
return r1, r2
|
||||
}
|
||||
|
||||
// Unzip3 accepts an array of grouped elements and creates an array regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
func Unzip3[A any, B any, C any](tuples []Tuple3[A, B, C]) ([]A, []B, []C) {
|
||||
size := len(tuples)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
r3 := make([]C, 0, size)
|
||||
|
||||
for _, tuple := range tuples {
|
||||
r1 = append(r1, tuple.A)
|
||||
r2 = append(r2, tuple.B)
|
||||
r3 = append(r3, tuple.C)
|
||||
}
|
||||
|
||||
return r1, r2, r3
|
||||
}
|
||||
|
||||
// Unzip4 accepts an array of grouped elements and creates an array regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
func Unzip4[A any, B any, C any, D any](tuples []Tuple4[A, B, C, D]) ([]A, []B, []C, []D) {
|
||||
size := len(tuples)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
r3 := make([]C, 0, size)
|
||||
r4 := make([]D, 0, size)
|
||||
|
||||
for _, tuple := range tuples {
|
||||
r1 = append(r1, tuple.A)
|
||||
r2 = append(r2, tuple.B)
|
||||
r3 = append(r3, tuple.C)
|
||||
r4 = append(r4, tuple.D)
|
||||
}
|
||||
|
||||
return r1, r2, r3, r4
|
||||
}
|
||||
|
||||
// Unzip5 accepts an array of grouped elements and creates an array regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
func Unzip5[A any, B any, C any, D any, E any](tuples []Tuple5[A, B, C, D, E]) ([]A, []B, []C, []D, []E) {
|
||||
size := len(tuples)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
r3 := make([]C, 0, size)
|
||||
r4 := make([]D, 0, size)
|
||||
r5 := make([]E, 0, size)
|
||||
|
||||
for _, tuple := range tuples {
|
||||
r1 = append(r1, tuple.A)
|
||||
r2 = append(r2, tuple.B)
|
||||
r3 = append(r3, tuple.C)
|
||||
r4 = append(r4, tuple.D)
|
||||
r5 = append(r5, tuple.E)
|
||||
}
|
||||
|
||||
return r1, r2, r3, r4, r5
|
||||
}
|
||||
|
||||
// Unzip6 accepts an array of grouped elements and creates an array regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
func Unzip6[A any, B any, C any, D any, E any, F any](tuples []Tuple6[A, B, C, D, E, F]) ([]A, []B, []C, []D, []E, []F) {
|
||||
size := len(tuples)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
r3 := make([]C, 0, size)
|
||||
r4 := make([]D, 0, size)
|
||||
r5 := make([]E, 0, size)
|
||||
r6 := make([]F, 0, size)
|
||||
|
||||
for _, tuple := range tuples {
|
||||
r1 = append(r1, tuple.A)
|
||||
r2 = append(r2, tuple.B)
|
||||
r3 = append(r3, tuple.C)
|
||||
r4 = append(r4, tuple.D)
|
||||
r5 = append(r5, tuple.E)
|
||||
r6 = append(r6, tuple.F)
|
||||
}
|
||||
|
||||
return r1, r2, r3, r4, r5, r6
|
||||
}
|
||||
|
||||
// Unzip7 accepts an array of grouped elements and creates an array regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
func Unzip7[A any, B any, C any, D any, E any, F any, G any](tuples []Tuple7[A, B, C, D, E, F, G]) ([]A, []B, []C, []D, []E, []F, []G) {
|
||||
size := len(tuples)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
r3 := make([]C, 0, size)
|
||||
r4 := make([]D, 0, size)
|
||||
r5 := make([]E, 0, size)
|
||||
r6 := make([]F, 0, size)
|
||||
r7 := make([]G, 0, size)
|
||||
|
||||
for _, tuple := range tuples {
|
||||
r1 = append(r1, tuple.A)
|
||||
r2 = append(r2, tuple.B)
|
||||
r3 = append(r3, tuple.C)
|
||||
r4 = append(r4, tuple.D)
|
||||
r5 = append(r5, tuple.E)
|
||||
r6 = append(r6, tuple.F)
|
||||
r7 = append(r7, tuple.G)
|
||||
}
|
||||
|
||||
return r1, r2, r3, r4, r5, r6, r7
|
||||
}
|
||||
|
||||
// Unzip8 accepts an array of grouped elements and creates an array regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
func Unzip8[A any, B any, C any, D any, E any, F any, G any, H any](tuples []Tuple8[A, B, C, D, E, F, G, H]) ([]A, []B, []C, []D, []E, []F, []G, []H) {
|
||||
size := len(tuples)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
r3 := make([]C, 0, size)
|
||||
r4 := make([]D, 0, size)
|
||||
r5 := make([]E, 0, size)
|
||||
r6 := make([]F, 0, size)
|
||||
r7 := make([]G, 0, size)
|
||||
r8 := make([]H, 0, size)
|
||||
|
||||
for _, tuple := range tuples {
|
||||
r1 = append(r1, tuple.A)
|
||||
r2 = append(r2, tuple.B)
|
||||
r3 = append(r3, tuple.C)
|
||||
r4 = append(r4, tuple.D)
|
||||
r5 = append(r5, tuple.E)
|
||||
r6 = append(r6, tuple.F)
|
||||
r7 = append(r7, tuple.G)
|
||||
r8 = append(r8, tuple.H)
|
||||
}
|
||||
|
||||
return r1, r2, r3, r4, r5, r6, r7, r8
|
||||
}
|
||||
|
||||
// Unzip9 accepts an array of grouped elements and creates an array regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
func Unzip9[A any, B any, C any, D any, E any, F any, G any, H any, I any](tuples []Tuple9[A, B, C, D, E, F, G, H, I]) ([]A, []B, []C, []D, []E, []F, []G, []H, []I) {
|
||||
size := len(tuples)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
r3 := make([]C, 0, size)
|
||||
r4 := make([]D, 0, size)
|
||||
r5 := make([]E, 0, size)
|
||||
r6 := make([]F, 0, size)
|
||||
r7 := make([]G, 0, size)
|
||||
r8 := make([]H, 0, size)
|
||||
r9 := make([]I, 0, size)
|
||||
|
||||
for _, tuple := range tuples {
|
||||
r1 = append(r1, tuple.A)
|
||||
r2 = append(r2, tuple.B)
|
||||
r3 = append(r3, tuple.C)
|
||||
r4 = append(r4, tuple.D)
|
||||
r5 = append(r5, tuple.E)
|
||||
r6 = append(r6, tuple.F)
|
||||
r7 = append(r7, tuple.G)
|
||||
r8 = append(r8, tuple.H)
|
||||
r9 = append(r9, tuple.I)
|
||||
}
|
||||
|
||||
return r1, r2, r3, r4, r5, r6, r7, r8, r9
|
||||
}
|
83
vendor/github.com/samber/lo/types.go
generated
vendored
Normal file
83
vendor/github.com/samber/lo/types.go
generated
vendored
Normal file
@ -0,0 +1,83 @@
|
||||
package lo
|
||||
|
||||
// Entry defines a key/value pairs.
|
||||
type Entry[K comparable, V any] struct {
|
||||
Key K
|
||||
Value V
|
||||
}
|
||||
|
||||
// Tuple2 is a group of 2 elements (pair).
|
||||
type Tuple2[A any, B any] struct {
|
||||
A A
|
||||
B B
|
||||
}
|
||||
|
||||
// Tuple3 is a group of 3 elements.
|
||||
type Tuple3[A any, B any, C any] struct {
|
||||
A A
|
||||
B B
|
||||
C C
|
||||
}
|
||||
|
||||
// Tuple4 is a group of 4 elements.
|
||||
type Tuple4[A any, B any, C any, D any] struct {
|
||||
A A
|
||||
B B
|
||||
C C
|
||||
D D
|
||||
}
|
||||
|
||||
// Tuple5 is a group of 5 elements.
|
||||
type Tuple5[A any, B any, C any, D any, E any] struct {
|
||||
A A
|
||||
B B
|
||||
C C
|
||||
D D
|
||||
E E
|
||||
}
|
||||
|
||||
// Tuple6 is a group of 6 elements.
|
||||
type Tuple6[A any, B any, C any, D any, E any, F any] struct {
|
||||
A A
|
||||
B B
|
||||
C C
|
||||
D D
|
||||
E E
|
||||
F F
|
||||
}
|
||||
|
||||
// Tuple7 is a group of 7 elements.
|
||||
type Tuple7[A any, B any, C any, D any, E any, F any, G any] struct {
|
||||
A A
|
||||
B B
|
||||
C C
|
||||
D D
|
||||
E E
|
||||
F F
|
||||
G G
|
||||
}
|
||||
|
||||
// Tuple8 is a group of 8 elements.
|
||||
type Tuple8[A any, B any, C any, D any, E any, F any, G any, H any] struct {
|
||||
A A
|
||||
B B
|
||||
C C
|
||||
D D
|
||||
E E
|
||||
F F
|
||||
G G
|
||||
H H
|
||||
}
|
||||
|
||||
// Tuple9 is a group of 9 elements.
|
||||
type Tuple9[A any, B any, C any, D any, E any, F any, G any, H any, I any] struct {
|
||||
A A
|
||||
B B
|
||||
C C
|
||||
D D
|
||||
E E
|
||||
F F
|
||||
G G
|
||||
H H
|
||||
I I
|
||||
}
|
50
vendor/github.com/samber/lo/util.go
generated
vendored
Normal file
50
vendor/github.com/samber/lo/util.go
generated
vendored
Normal file
@ -0,0 +1,50 @@
|
||||
package lo
|
||||
|
||||
import "golang.org/x/exp/constraints"
|
||||
|
||||
// Range creates an array of numbers (positive and/or negative) with given length.
|
||||
func Range(elementNum int) []int {
|
||||
length := If(elementNum < 0, -elementNum).Else(elementNum)
|
||||
result := make([]int, length)
|
||||
step := If(elementNum < 0, -1).Else(1)
|
||||
for i, j := 0, 0; i < length; i, j = i+1, j+step {
|
||||
result[i] = j
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// RangeFrom creates an array of numbers from start with specified length.
|
||||
func RangeFrom[T constraints.Integer | constraints.Float](start T, elementNum int) []T {
|
||||
length := If(elementNum < 0, -elementNum).Else(elementNum)
|
||||
result := make([]T, length)
|
||||
step := If(elementNum < 0, -1).Else(1)
|
||||
for i, j := 0, start; i < length; i, j = i+1, j+T(step) {
|
||||
result[i] = j
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// RangeWithSteps creates an array of numbers (positive and/or negative) progressing from start up to, but not including end.
|
||||
// step set to zero will return empty array.
|
||||
func RangeWithSteps[T constraints.Integer | constraints.Float](start, end, step T) []T {
|
||||
result := []T{}
|
||||
if start == end || step == 0 {
|
||||
return result
|
||||
}
|
||||
if start < end {
|
||||
if step < 0 {
|
||||
return result
|
||||
}
|
||||
for i := start; i < end; i += step {
|
||||
result = append(result, i)
|
||||
}
|
||||
return result
|
||||
}
|
||||
if step > 0 {
|
||||
return result
|
||||
}
|
||||
for i := start; i > end; i += step {
|
||||
result = append(result, i)
|
||||
}
|
||||
return result
|
||||
}
|
Reference in New Issue
Block a user