style: reduce the number of files in root directory (because you need to scroll too many times before reading the readme) (#719)

This commit is contained in:
Samuel Berthe
2025-10-24 20:14:51 +02:00
committed by GitHub
parent 6e277bc6e4
commit 29dcc41a9c
19 changed files with 3430 additions and 3451 deletions

View File

@@ -4,10 +4,12 @@ updates:
directory: /
schedule:
interval: monthly
- package-ecosystem: gomod
directory: /
schedule:
interval: weekly
- package-ecosystem: npm
directory: /docs
schedule:

View File

@@ -1,4 +1,4 @@
name: Docs helpers validation
name: Documentation
on:
pull_request:
@@ -9,7 +9,7 @@ on:
- 'docs/**'
jobs:
validate-docs-helpers:
build-doc:
runs-on: ubuntu-latest
defaults:
run:
@@ -28,20 +28,37 @@ jobs:
- name: Build Docusaurus project
run: npm run build
# - name: Run docs helpers checks
# run: |
# node docs/scripts/check-duplicates-in-category.js
# node docs/scripts/check-similar-exists.js
# node docs/scripts/check-filename-matches-frontmatter.js
# node docs/scripts/check-similar-keys-exist-in-directory.js
# node docs/scripts/check-cross-references.js
# - name: Sync function signatures
# run: npm run sync:function-signatures
# - name: Commit and push changes
# uses: stefanzweifel/git-auto-commit-action@v5
# with:
# commit_message: 'docs: sync function signatures from source'
# commit_user_name: github-actions[bot]
# commit_user_email: github-actions[bot]@users.noreply.github.com
# file_pattern: docs/data/*.md
# validate-doc:
# runs-on: ubuntu-latest
# defaults:
# run:
# working-directory: ./docs
# steps:
# - name: Checkout repository
# uses: actions/checkout@v5
# - name: Setup Node.js
# uses: actions/setup-node@v6
# - name: Install dependencies
# run: npm i
# - name: Run docs helpers checks
# run: |
# node docs/scripts/check-duplicates-in-category.js
# node docs/scripts/check-similar-exists.js
# node docs/scripts/check-filename-matches-frontmatter.js
# node docs/scripts/check-similar-keys-exist-in-directory.js
# node docs/scripts/check-cross-references.js
# - name: Sync function signatures
# run: npm run sync:function-signatures
# - name: Commit and push changes
# uses: stefanzweifel/git-auto-commit-action@v5
# with:
# commit_message: 'docs: sync function signatures from source'
# commit_user_name: github-actions[bot]
# commit_user_email: github-actions[bot]@users.noreply.github.com
# file_pattern: docs/data/*.md

View File

@@ -25,7 +25,7 @@ jobs:
# remove tests to clean dependencies
- name: Remove xxx_test.go files
run: rm -rf $(find . -type f -name "*_test.go" ! -name "*_example_test.go") img/ docs/
run: rm -rf $(find . -type f -name "*_test.go" ! -name "*_example_test.go") img/ docs/ benchmark/
# cleanup test dependencies
- name: Cleanup dependencies

View File

@@ -1,4 +1,4 @@
package lo
package benchmark
import (
"math/rand"
@@ -6,6 +6,7 @@ import (
"testing"
"time"
"github.com/samber/lo"
lop "github.com/samber/lo/parallel"
"github.com/thoas/go-funk"
)
@@ -39,7 +40,7 @@ func BenchmarkMap(b *testing.B) {
b.Run("lo.Map", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = Map(arr, func(x int64, i int) string {
_ = lo.Map(arr, func(x int64, i int) string {
return strconv.FormatInt(x, 10)
})
}

View File

@@ -1,6 +1,6 @@
//go:build go1.23
package it
package benchmark
import (
"fmt"
@@ -8,25 +8,27 @@ import (
"math/rand/v2"
"strconv"
"testing"
"github.com/samber/lo/it"
)
var lengths = []int{10, 100, 1000}
var itLengths = []int{10, 100, 1000}
func BenchmarkChunk(b *testing.B) {
for _, n := range lengths {
func BenchmarkItChunk(b *testing.B) {
for _, n := range itLengths {
strs := genStrings(n)
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Chunk(strs, 5)
_ = it.Chunk(strs, 5)
}
})
}
for _, n := range lengths {
for _, n := range itLengths {
ints := genInts(n)
b.Run(fmt.Sprintf("ints%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Chunk(ints, 5)
_ = it.Chunk(ints, 5)
}
})
}
@@ -52,99 +54,100 @@ func genInts(n int) iter.Seq[int] {
}
}
func BenchmarkFlatten(b *testing.B) {
for _, n := range lengths {
func BenchmarkItFlatten(b *testing.B) {
for _, n := range itLengths {
ints := make([]iter.Seq[int], 0, n)
for i := 0; i < n; i++ {
ints = append(ints, genInts(n))
}
b.Run(fmt.Sprintf("ints_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Flatten(ints)
_ = it.Flatten(ints)
}
})
}
for _, n := range lengths {
for _, n := range itLengths {
strs := make([]iter.Seq[string], 0, n)
for i := 0; i < n; i++ {
strs = append(strs, genStrings(n))
}
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Flatten(strs)
_ = it.Flatten(strs)
}
})
}
}
func BenchmarkDrop(b *testing.B) {
for _, n := range lengths {
func BenchmarkItDrop(b *testing.B) {
for _, n := range itLengths {
strs := genStrings(n)
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Drop(strs, n/4)
_ = it.Drop(strs, n/4)
}
})
}
for _, n := range lengths {
for _, n := range itLengths {
ints := genInts(n)
b.Run(fmt.Sprintf("ints%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Drop(ints, n/4)
_ = it.Drop(ints, n/4)
}
})
}
}
func BenchmarkDropWhile(b *testing.B) {
for _, n := range lengths {
func BenchmarkItDropWhile(b *testing.B) {
for _, n := range itLengths {
strs := genStrings(n)
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DropWhile(strs, func(v string) bool { return len(v) < 4 })
_ = it.DropWhile(strs, func(v string) bool { return len(v) < 4 })
}
})
}
for _, n := range lengths {
for _, n := range itLengths {
ints := genInts(n)
b.Run(fmt.Sprintf("ints%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DropWhile(ints, func(v int) bool { return i < 10_000 })
_ = it.DropWhile(ints, func(v int) bool { return i < 10_000 })
}
})
}
}
func BenchmarkDropByIndex(b *testing.B) {
for _, n := range lengths {
func BenchmarkItDropByIndex(b *testing.B) {
for _, n := range itLengths {
strs := genStrings(n)
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DropByIndex(strs, n/4)
_ = it.DropByIndex(strs, n/4)
}
})
}
for _, n := range lengths {
for _, n := range itLengths {
ints := genInts(n)
b.Run(fmt.Sprintf("ints%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DropByIndex(ints, n/4)
_ = it.DropByIndex(ints, n/4)
}
})
}
}
func BenchmarkReplace(b *testing.B) {
func BenchmarkItReplace(b *testing.B) {
lengths := []int{1_000, 10_000, 100_000}
for _, n := range lengths {
strs := genStrings(n)
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Replace(strs, "321321", "123123", 10)
_ = it.Replace(strs, "321321", "123123", 10)
}
})
}
@@ -153,7 +156,7 @@ func BenchmarkReplace(b *testing.B) {
ints := genInts(n)
b.Run(fmt.Sprintf("ints%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Replace(ints, 321321, 123123, 10)
_ = it.Replace(ints, 321321, 123123, 10)
}
})
}

View File

@@ -1,10 +1,12 @@
package lo
package benchmark
import (
"fmt"
"math/rand"
"strconv"
"testing"
"github.com/samber/lo"
)
var lengths = []int{10, 100, 1000}
@@ -14,7 +16,7 @@ func BenchmarkChunk(b *testing.B) {
strs := genSliceString(n)
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Chunk(strs, 5)
_ = lo.Chunk(strs, 5)
}
})
}
@@ -23,7 +25,7 @@ func BenchmarkChunk(b *testing.B) {
ints := genSliceInt(n)
b.Run(fmt.Sprintf("ints%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Chunk(ints, 5)
_ = lo.Chunk(ints, 5)
}
})
}
@@ -53,7 +55,7 @@ func BenchmarkFlatten(b *testing.B) {
}
b.Run(fmt.Sprintf("ints_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Flatten(ints)
_ = lo.Flatten(ints)
}
})
}
@@ -65,7 +67,7 @@ func BenchmarkFlatten(b *testing.B) {
}
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Flatten(strs)
_ = lo.Flatten(strs)
}
})
}
@@ -76,7 +78,7 @@ func BenchmarkDrop(b *testing.B) {
strs := genSliceString(n)
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Drop(strs, n/4)
_ = lo.Drop(strs, n/4)
}
})
}
@@ -85,7 +87,7 @@ func BenchmarkDrop(b *testing.B) {
ints := genSliceInt(n)
b.Run(fmt.Sprintf("ints%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Drop(ints, n/4)
_ = lo.Drop(ints, n/4)
}
})
}
@@ -96,7 +98,7 @@ func BenchmarkDropRight(b *testing.B) {
strs := genSliceString(n)
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DropRight(strs, n/4)
_ = lo.DropRight(strs, n/4)
}
})
}
@@ -105,7 +107,7 @@ func BenchmarkDropRight(b *testing.B) {
ints := genSliceInt(n)
b.Run(fmt.Sprintf("ints%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DropRight(ints, n/4)
_ = lo.DropRight(ints, n/4)
}
})
}
@@ -116,7 +118,7 @@ func BenchmarkDropWhile(b *testing.B) {
strs := genSliceString(n)
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DropWhile(strs, func(v string) bool { return len(v) < 4 })
_ = lo.DropWhile(strs, func(v string) bool { return len(v) < 4 })
}
})
}
@@ -125,7 +127,7 @@ func BenchmarkDropWhile(b *testing.B) {
ints := genSliceInt(n)
b.Run(fmt.Sprintf("ints%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DropWhile(ints, func(v int) bool { return i < 10_000 })
_ = lo.DropWhile(ints, func(v int) bool { return i < 10_000 })
}
})
}
@@ -136,7 +138,7 @@ func BenchmarkDropRightWhile(b *testing.B) {
strs := genSliceString(n)
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DropRightWhile(strs, func(v string) bool { return len(v) < 4 })
_ = lo.DropRightWhile(strs, func(v string) bool { return len(v) < 4 })
}
})
}
@@ -145,7 +147,7 @@ func BenchmarkDropRightWhile(b *testing.B) {
ints := genSliceInt(n)
b.Run(fmt.Sprintf("ints%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DropRightWhile(ints, func(v int) bool { return i < 10_000 })
_ = lo.DropRightWhile(ints, func(v int) bool { return i < 10_000 })
}
})
}
@@ -156,7 +158,7 @@ func BenchmarkDropByIndex(b *testing.B) {
strs := genSliceString(n)
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DropByIndex(strs, n/4)
_ = lo.DropByIndex(strs, n/4)
}
})
}
@@ -165,7 +167,7 @@ func BenchmarkDropByIndex(b *testing.B) {
ints := genSliceInt(n)
b.Run(fmt.Sprintf("ints%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DropByIndex(ints, n/4)
_ = lo.DropByIndex(ints, n/4)
}
})
}
@@ -177,7 +179,7 @@ func BenchmarkReplace(b *testing.B) {
strs := genSliceString(n)
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Replace(strs, strs[n/4], "123123", 10)
_ = lo.Replace(strs, strs[n/4], "123123", 10)
}
})
}
@@ -186,7 +188,7 @@ func BenchmarkReplace(b *testing.B) {
ints := genSliceInt(n)
b.Run(fmt.Sprintf("ints%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Replace(ints, ints[n/4], 123123, 10)
_ = lo.Replace(ints, ints[n/4], 123123, 10)
}
})
}
@@ -195,6 +197,6 @@ func BenchmarkReplace(b *testing.B) {
func BenchmarkToSlicePtr(b *testing.B) {
preallocated := make([]int, 100000)
for i := 0; i < b.N; i++ {
_ = ToSlicePtr(preallocated)
_ = lo.ToSlicePtr(preallocated)
}
}

View File

@@ -1,489 +0,0 @@
package lo
import (
"fmt"
)
func ExampleTernary() {
result := Ternary(true, "a", "b")
fmt.Printf("%v", result)
// Output: a
}
func ExampleTernaryF() {
result := TernaryF(true, func() string { return "a" }, func() string { return "b" })
fmt.Printf("%v", result)
// Output: a
}
func ExampleIf() {
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
result2 := If(false, 1).
ElseIf(true, 2).
Else(3)
result3 := If(false, 1).
ElseIf(false, 2).
Else(3)
result4 := IfF(true, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
result5 := IfF(false, func() int { return 1 }).
ElseIfF(true, func() int { return 2 }).
ElseF(func() int { return 3 })
result6 := IfF(false, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
// Output:
// 1
// 2
// 3
// 1
// 2
// 3
}
func ExampleIfF() {
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
result2 := If(false, 1).
ElseIf(true, 2).
Else(3)
result3 := If(false, 1).
ElseIf(false, 2).
Else(3)
result4 := IfF(true, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
result5 := IfF(false, func() int { return 1 }).
ElseIfF(true, func() int { return 2 }).
ElseF(func() int { return 3 })
result6 := IfF(false, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
// Output:
// 1
// 2
// 3
// 1
// 2
// 3
}
func Example_ifElse_ElseIf() {
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
result2 := If(false, 1).
ElseIf(true, 2).
Else(3)
result3 := If(false, 1).
ElseIf(false, 2).
Else(3)
result4 := IfF(true, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
result5 := IfF(false, func() int { return 1 }).
ElseIfF(true, func() int { return 2 }).
ElseF(func() int { return 3 })
result6 := IfF(false, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
// Output:
// 1
// 2
// 3
// 1
// 2
// 3
}
func Example_ifElse_ElseIfF() {
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
result2 := If(false, 1).
ElseIf(true, 2).
Else(3)
result3 := If(false, 1).
ElseIf(false, 2).
Else(3)
result4 := IfF(true, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
result5 := IfF(false, func() int { return 1 }).
ElseIfF(true, func() int { return 2 }).
ElseF(func() int { return 3 })
result6 := IfF(false, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
// Output:
// 1
// 2
// 3
// 1
// 2
// 3
}
func Example_ifElse_Else() {
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
result2 := If(false, 1).
ElseIf(true, 2).
Else(3)
result3 := If(false, 1).
ElseIf(false, 2).
Else(3)
result4 := IfF(true, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
result5 := IfF(false, func() int { return 1 }).
ElseIfF(true, func() int { return 2 }).
ElseF(func() int { return 3 })
result6 := IfF(false, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
// Output:
// 1
// 2
// 3
// 1
// 2
// 3
}
func Example_ifElse_ElseF() {
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
result2 := If(false, 1).
ElseIf(true, 2).
Else(3)
result3 := If(false, 1).
ElseIf(false, 2).
Else(3)
result4 := IfF(true, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
result5 := IfF(false, func() int { return 1 }).
ElseIfF(true, func() int { return 2 }).
ElseF(func() int { return 3 })
result6 := IfF(false, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
// Output:
// 1
// 2
// 3
// 1
// 2
// 3
}
func ExampleSwitch() {
result1 := Switch[int, string](1).
Case(1, "1").
Case(2, "2").
Default("3")
result2 := Switch[int, string](2).
Case(1, "1").
Case(2, "2").
Default("3")
result3 := Switch[int, string](42).
Case(1, "1").
Case(2, "2").
Default("3")
result4 := Switch[int, string](1).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result5 := Switch[int, string](2).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result6 := Switch[int, string](42).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
// Output:
// 1
// 2
// 3
// 1
// 2
// 3
}
func Example_switchCase_Case() {
result1 := Switch[int, string](1).
Case(1, "1").
Case(2, "2").
Default("3")
result2 := Switch[int, string](2).
Case(1, "1").
Case(2, "2").
Default("3")
result3 := Switch[int, string](42).
Case(1, "1").
Case(2, "2").
Default("3")
result4 := Switch[int, string](1).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result5 := Switch[int, string](2).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result6 := Switch[int, string](42).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
// Output:
// 1
// 2
// 3
// 1
// 2
// 3
}
func Example_switchCase_CaseF() {
result1 := Switch[int, string](1).
Case(1, "1").
Case(2, "2").
Default("3")
result2 := Switch[int, string](2).
Case(1, "1").
Case(2, "2").
Default("3")
result3 := Switch[int, string](42).
Case(1, "1").
Case(2, "2").
Default("3")
result4 := Switch[int, string](1).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result5 := Switch[int, string](2).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result6 := Switch[int, string](42).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
// Output:
// 1
// 2
// 3
// 1
// 2
// 3
}
func Example_switchCase_Default() {
result1 := Switch[int, string](1).
Case(1, "1").
Case(2, "2").
Default("3")
result2 := Switch[int, string](2).
Case(1, "1").
Case(2, "2").
Default("3")
result3 := Switch[int, string](42).
Case(1, "1").
Case(2, "2").
Default("3")
result4 := Switch[int, string](1).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result5 := Switch[int, string](2).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result6 := Switch[int, string](42).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
// Output:
// 1
// 2
// 3
// 1
// 2
// 3
}
func Example_switchCase_DefaultF() {
result1 := Switch[int, string](1).
Case(1, "1").
Case(2, "2").
Default("3")
result2 := Switch[int, string](2).
Case(1, "1").
Case(2, "2").
Default("3")
result3 := Switch[int, string](42).
Case(1, "1").
Case(2, "2").
Default("3")
result4 := Switch[int, string](1).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result5 := Switch[int, string](2).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result6 := Switch[int, string](42).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
// Output:
// 1
// 2
// 3
// 1
// 2
// 3
}

View File

@@ -8,10 +8,12 @@ sidebar_position: 110
Hey! We are happy to have you as a new contributor. ✌️
For your first contribution please follow some guidelines:
For your contribution please follow some guidelines:
## Function Naming
Function naming: helpers must be self-explanatory and respect standards (other languages, libraries...). Feel free to suggest many names in your contributions.
Helpers must be self-explanatory and respect standards (other languages, libraries...). Feel free to suggest many names in your contributions or the related issue.
We hate breaking changes, so better think twice ;)
## Variadic functions
Many functions accept variadic parameters (like `lo.Keys(...map[K]V)` accepting multiple maps), providing flexibility while maintaining type safety.

View File

@@ -1,472 +0,0 @@
package lo
import (
"errors"
"fmt"
)
func ExampleValidate() {
i := 42
err1 := Validate(i < 0, "expected %d < 0", i)
err2 := Validate(i > 0, "expected %d > 0", i)
fmt.Printf("%v\n%v", err1, err2)
// Output:
// expected 42 < 0
// <nil>
}
func ExampleMust() {
defer func() {
_ = recover()
}()
// won't panic
Must(42, nil)
// won't panic
cb := func() (int, error) {
return 42, nil
}
Must(cb())
// will panic
Must(42, errors.New("my error"))
// will panic with error message
Must(42, errors.New("world"), "hello")
}
func ExampleMust0() {
defer func() {
_ = recover()
}()
// won't panic
Must0(nil)
// will panic
Must0(errors.New("my error"))
// will panic with error message
Must0(errors.New("world"), "hello")
}
func ExampleMust1() {
defer func() {
_ = recover()
}()
// won't panic
Must1(42, nil)
// won't panic
cb := func() (int, error) {
return 42, nil
}
Must1(cb())
// will panic
Must1(42, errors.New("my error"))
// will panic with error message
Must1(42, errors.New("world"), "hello")
}
func ExampleMust2() {
defer func() {
_ = recover()
}()
// won't panic
Must2(42, "hello", nil)
// will panic
Must2(42, "hello", errors.New("my error"))
// will panic with error message
Must2(42, "hello", errors.New("world"), "hello")
}
func ExampleMust3() {
defer func() {
_ = recover()
}()
// won't panic
Must3(42, "hello", 4.2, nil)
// will panic
Must3(42, "hello", 4.2, errors.New("my error"))
// will panic with error message
Must3(42, "hello", 4.2, errors.New("world"), "hello")
}
func ExampleMust4() {
defer func() {
_ = recover()
}()
// won't panic
Must4(42, "hello", 4.2, true, nil)
// will panic
Must4(42, "hello", 4.2, true, errors.New("my error"))
// will panic with error message
Must4(42, "hello", 4.2, true, errors.New("world"), "hello")
}
func ExampleMust5() {
defer func() {
_ = recover()
}()
// won't panic
Must5(42, "hello", 4.2, true, foo{}, nil)
// will panic
Must5(42, "hello", 4.2, true, foo{}, errors.New("my error"))
// will panic with error message
Must5(42, "hello", 4.2, true, foo{}, errors.New("world"), "hello")
}
func ExampleMust6() {
defer func() {
_ = recover()
}()
// won't panic
Must5(42, "hello", 4.2, true, foo{}, "foobar", nil)
// will panic
Must5(42, "hello", 4.2, true, foo{}, "foobar", errors.New("my error"))
// will panic with error message
Must5(42, "hello", 4.2, true, foo{}, "foobar", errors.New("world"), "hello")
}
func ExampleTry() {
ok1 := Try(func() error {
return nil
})
ok2 := Try(func() error {
return errors.New("my error")
})
ok3 := Try(func() error {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
// Output:
// true
// false
// false
}
func ExampleTry1() {
ok1 := Try1(func() error {
return nil
})
ok2 := Try1(func() error {
return errors.New("my error")
})
ok3 := Try1(func() error {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
// Output:
// true
// false
// false
}
func ExampleTry2() {
ok1 := Try2(func() (int, error) {
return 42, nil
})
ok2 := Try2(func() (int, error) {
return 42, errors.New("my error")
})
ok3 := Try2(func() (int, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
// Output:
// true
// false
// false
}
func ExampleTry3() {
ok1 := Try3(func() (int, string, error) {
return 42, "foobar", nil
})
ok2 := Try3(func() (int, string, error) {
return 42, "foobar", errors.New("my error")
})
ok3 := Try3(func() (int, string, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
// Output:
// true
// false
// false
}
func ExampleTry4() {
ok1 := Try4(func() (int, string, float64, error) {
return 42, "foobar", 4.2, nil
})
ok2 := Try4(func() (int, string, float64, error) {
return 42, "foobar", 4.2, errors.New("my error")
})
ok3 := Try4(func() (int, string, float64, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
// Output:
// true
// false
// false
}
func ExampleTry5() {
ok1 := Try5(func() (int, string, float64, bool, error) {
return 42, "foobar", 4.2, true, nil
})
ok2 := Try5(func() (int, string, float64, bool, error) {
return 42, "foobar", 4.2, true, errors.New("my error")
})
ok3 := Try5(func() (int, string, float64, bool, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
// Output:
// true
// false
// false
}
func ExampleTry6() {
ok1 := Try6(func() (int, string, float64, bool, foo, error) {
return 42, "foobar", 4.2, true, foo{}, nil
})
ok2 := Try6(func() (int, string, float64, bool, foo, error) {
return 42, "foobar", 4.2, true, foo{}, errors.New("my error")
})
ok3 := Try6(func() (int, string, float64, bool, foo, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
// Output:
// true
// false
// false
}
func ExampleTryOr() {
value1, ok1 := TryOr(func() (int, error) {
return 42, nil
}, 21)
value2, ok2 := TryOr(func() (int, error) {
return 42, errors.New("my error")
}, 21)
value3, ok3 := TryOr(func() (int, error) {
panic("my error")
}, 21)
fmt.Printf("%v %v\n", value1, ok1)
fmt.Printf("%v %v\n", value2, ok2)
fmt.Printf("%v %v\n", value3, ok3)
// Output:
// 42 true
// 21 false
// 21 false
}
func ExampleTryOr1() {
value1, ok1 := TryOr1(func() (int, error) {
return 42, nil
}, 21)
value2, ok2 := TryOr1(func() (int, error) {
return 42, errors.New("my error")
}, 21)
value3, ok3 := TryOr1(func() (int, error) {
panic("my error")
}, 21)
fmt.Printf("%v %v\n", value1, ok1)
fmt.Printf("%v %v\n", value2, ok2)
fmt.Printf("%v %v\n", value3, ok3)
// Output:
// 42 true
// 21 false
// 21 false
}
func ExampleTryOr2() {
value1, value2, ok3 := TryOr2(func() (int, string, error) {
panic("my error")
}, 21, "hello")
fmt.Printf("%v %v %v\n", value1, value2, ok3)
// Output: 21 hello false
}
func ExampleTryOr3() {
value1, value2, value3, ok3 := TryOr3(func() (int, string, bool, error) {
panic("my error")
}, 21, "hello", false)
fmt.Printf("%v %v %v %v\n", value1, value2, value3, ok3)
// Output: 21 hello false false
}
func ExampleTryOr4() {
value1, value2, value3, value4, ok3 := TryOr4(func() (int, string, bool, foo, error) {
panic("my error")
}, 21, "hello", false, foo{bar: "bar"})
fmt.Printf("%v %v %v %v %v\n", value1, value2, value3, value4, ok3)
// Output: 21 hello false {bar} false
}
func ExampleTryOr5() {
value1, value2, value3, value4, value5, ok3 := TryOr5(func() (int, string, bool, foo, float64, error) {
panic("my error")
}, 21, "hello", false, foo{bar: "bar"}, 4.2)
fmt.Printf("%v %v %v %v %v %v\n", value1, value2, value3, value4, value5, ok3)
// Output: 21 hello false {bar} 4.2 false
}
func ExampleTryOr6() {
value1, value2, value3, value4, value5, value6, ok3 := TryOr6(func() (int, string, bool, foo, float64, string, error) {
panic("my error")
}, 21, "hello", false, foo{bar: "bar"}, 4.2, "world")
fmt.Printf("%v %v %v %v %v %v %v\n", value1, value2, value3, value4, value5, value6, ok3)
// Output: 21 hello false {bar} 4.2 world false
}
func ExampleTryWithErrorValue() {
err1, ok1 := TryWithErrorValue(func() error {
return nil
})
err2, ok2 := TryWithErrorValue(func() error {
return errors.New("my error")
})
err3, ok3 := TryWithErrorValue(func() error {
panic("my error")
})
fmt.Printf("%v %v\n", err1, ok1)
fmt.Printf("%v %v\n", err2, ok2)
fmt.Printf("%v %v\n", err3, ok3)
// Output:
// <nil> true
// my error false
// my error false
}
func ExampleTryCatchWithErrorValue() {
TryCatchWithErrorValue(
func() error {
panic("trigger an error")
},
func(err any) {
fmt.Printf("catch: %s", err)
},
)
// Output: catch: trigger an error
}
type myError struct{}
func (e myError) Error() string {
return "my error"
}
func ExampleErrorsAs() {
doSomething := func() error {
return &myError{}
}
err := doSomething()
if rateLimitErr, ok := ErrorsAs[*myError](err); ok {
fmt.Printf("is type myError, err: %s", rateLimitErr.Error())
} else {
fmt.Printf("is not type myError")
}
// Output: is type myError, err: my error
}
func ExampleAssert() {
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
age := 20
// won't panic
Assert(age >= 18)
// won't panic
Assert(age >= 18, "age must be at least 18")
// will panic
Assert(age < 18)
// will panic
Assert(age < 18, "age must be less than 18")
// Output: assertion failed
}
func ExampleAssertf() {
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
age := 20
// won't panic
Assertf(age >= 18, "age must be at least 18, got %d", age)
// will panic
Assertf(age < 18, "age must be less than 18, got %d", age)
// Output: assertion failed: age must be less than 18, got 20
}

View File

@@ -1,644 +0,0 @@
package lo
import (
"fmt"
"time"
)
func ExampleIndexOf() {
list := []string{"foo", "bar", "baz"}
result := IndexOf(list, "bar")
fmt.Printf("%d", result)
// Output: 1
}
func ExampleIndexOf_notFound() {
list := []string{"foo", "bar", "baz"}
result := IndexOf(list, "qux")
fmt.Printf("%d", result)
// Output: -1
}
func ExampleLastIndexOf() {
list := []string{"foo", "bar", "baz", "bar"}
result := LastIndexOf(list, "bar")
fmt.Printf("%d", result)
// Output: 3
}
func ExampleLastIndexOf_notFound() {
list := []string{"foo", "bar", "baz"}
result := LastIndexOf(list, "qux")
fmt.Printf("%d", result)
// Output: -1
}
func ExampleFind() {
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
}
result, found := Find(users, func(user User) bool {
return user.Age > 30
})
fmt.Printf("%s %t", result.Name, found)
// Output: Charlie true
}
func ExampleFind_notFound() {
list := []int{1, 2, 3, 4, 5}
result, found := Find(list, func(n int) bool {
return n > 10
})
fmt.Printf("%d %t", result, found)
// Output: 0 false
}
func ExampleFindIndexOf() {
list := []int{1, 2, 3, 4, 5}
result, index, found := FindIndexOf(list, func(n int) bool {
return n > 2
})
fmt.Printf("%d %d %t", result, index, found)
// Output: 3 2 true
}
func ExampleFindIndexOf_notFound() {
list := []int{1, 2, 3, 4, 5}
result, index, found := FindIndexOf(list, func(n int) bool {
return n > 10
})
fmt.Printf("%d %d %t", result, index, found)
// Output: 0 -1 false
}
func ExampleFindLastIndexOf() {
list := []int{1, 2, 3, 4, 3, 5}
result, index, found := FindLastIndexOf(list, func(n int) bool {
return n == 3
})
fmt.Printf("%d %d %t", result, index, found)
// Output: 3 4 true
}
func ExampleFindLastIndexOf_notFound() {
list := []int{1, 2, 3, 4, 5}
result, index, found := FindLastIndexOf(list, func(n int) bool {
return n > 10
})
fmt.Printf("%d %d %t", result, index, found)
// Output: 0 -1 false
}
func ExampleFindOrElse() {
list := []int{1, 2, 3, 4, 5}
result := FindOrElse(list, -1, func(n int) bool {
return n > 10
})
fmt.Printf("%d", result)
// Output: -1
}
func ExampleFindOrElse_found() {
list := []int{1, 2, 3, 4, 5}
result := FindOrElse(list, -1, func(n int) bool {
return n > 3
})
fmt.Printf("%d", result)
// Output: 4
}
func ExampleFindKey() {
users := map[string]int{
"Alice": 25,
"Bob": 30,
"Charlie": 35,
}
key, found := FindKey(users, 30)
fmt.Printf("%s %t", key, found)
// Output: Bob true
}
func ExampleFindKey_notFound() {
users := map[string]int{
"Alice": 25,
"Bob": 30,
"Charlie": 35,
}
key, found := FindKey(users, 40)
fmt.Printf("%s %t", key, found)
// Output: false
}
func ExampleFindKeyBy() {
users := map[string]int{
"Alice": 25,
"Bob": 30,
"Charlie": 35,
}
key, found := FindKeyBy(users, func(name string, age int) bool {
return age > 30
})
fmt.Printf("%s %t", key, found)
// Output: Charlie true
}
func ExampleFindKeyBy_notFound() {
users := map[string]int{
"Alice": 25,
"Bob": 30,
"Charlie": 35,
}
key, found := FindKeyBy(users, func(name string, age int) bool {
return age > 40
})
fmt.Printf("%s %t", key, found)
// Output: false
}
func ExampleFindUniques() {
list := []int{1, 2, 2, 3, 3, 3, 4, 5}
result := FindUniques(list)
fmt.Printf("%v", result)
// Output: [1 4 5]
}
func ExampleFindUniquesBy() {
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 25},
{Name: "David", Age: 30},
{Name: "Eve", Age: 35},
}
result := FindUniquesBy(users, func(user User) int {
return user.Age
})
fmt.Printf("%d", len(result))
// Output: 1
}
func ExampleFindDuplicates() {
list := []int{1, 2, 2, 3, 3, 3, 4, 5}
result := FindDuplicates(list)
fmt.Printf("%v", result)
// Output: [2 3]
}
func ExampleFindDuplicatesBy() {
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 25},
{Name: "David", Age: 30},
{Name: "Eve", Age: 35},
}
result := FindDuplicatesBy(users, func(user User) int {
return user.Age
})
fmt.Printf("%d", len(result))
// Output: 2
}
func ExampleMin() {
list := []int{3, 1, 4, 1, 5, 9, 2, 6}
result := Min(list)
fmt.Printf("%d", result)
// Output: 1
}
func ExampleMin_empty() {
list := []int{}
result := Min(list)
fmt.Printf("%d", result)
// Output: 0
}
func ExampleMinIndex() {
list := []int{3, 1, 4, 1, 5, 9, 2, 6}
result, index := MinIndex(list)
fmt.Printf("%d %d", result, index)
// Output: 1 1
}
func ExampleMinIndex_empty() {
list := []int{}
result, index := MinIndex(list)
fmt.Printf("%d %d", result, index)
// Output: 0 -1
}
func ExampleMinBy() {
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
}
result := MinBy(users, func(a, b User) bool {
return a.Age < b.Age
})
fmt.Printf("%s", result.Name)
// Output: Alice
}
func ExampleMinIndexBy() {
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
}
result, index := MinIndexBy(users, func(a, b User) bool {
return a.Age < b.Age
})
fmt.Printf("%s %d", result.Name, index)
// Output: Alice 0
}
func ExampleEarliest() {
now := time.Now()
past := now.Add(-time.Hour)
future := now.Add(time.Hour)
result := Earliest(future, now, past)
fmt.Printf("%t", result.Equal(past))
// Output: true
}
func ExampleEarliestBy() {
type Event struct {
Name string
Time time.Time
}
now := time.Now()
events := []Event{
{Name: "Event A", Time: now.Add(time.Hour)},
{Name: "Event B", Time: now},
{Name: "Event C", Time: now.Add(-time.Hour)},
}
result := EarliestBy(events, func(event Event) time.Time {
return event.Time
})
fmt.Printf("%s", result.Name)
// Output: Event C
}
func ExampleMax() {
list := []int{3, 1, 4, 1, 5, 9, 2, 6}
result := Max(list)
fmt.Printf("%d", result)
// Output: 9
}
func ExampleMax_empty() {
list := []int{}
result := Max(list)
fmt.Printf("%d", result)
// Output: 0
}
func ExampleMaxIndex() {
list := []int{3, 1, 4, 1, 5, 9, 2, 6}
result, index := MaxIndex(list)
fmt.Printf("%d %d", result, index)
// Output: 9 5
}
func ExampleMaxIndex_empty() {
list := []int{}
result, index := MaxIndex(list)
fmt.Printf("%d %d", result, index)
// Output: 0 -1
}
func ExampleMaxBy() {
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
}
result := MaxBy(users, func(a, b User) bool {
return a.Age > b.Age
})
fmt.Printf("%s", result.Name)
// Output: Charlie
}
func ExampleMaxIndexBy() {
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
}
result, index := MaxIndexBy(users, func(a, b User) bool {
return a.Age > b.Age
})
fmt.Printf("%s %d", result.Name, index)
// Output: Charlie 2
}
func ExampleLatest() {
now := time.Now()
past := now.Add(-time.Hour)
future := now.Add(time.Hour)
result := Latest(future, now, past)
fmt.Printf("%t", result.Equal(future))
// Output: true
}
func ExampleLatestBy() {
type Event struct {
Name string
Time time.Time
}
now := time.Now()
events := []Event{
{Name: "Event A", Time: now.Add(time.Hour)},
{Name: "Event B", Time: now},
{Name: "Event C", Time: now.Add(-time.Hour)},
}
result := LatestBy(events, func(event Event) time.Time {
return event.Time
})
fmt.Printf("%s", result.Name)
// Output: Event A
}
func ExampleFirst() {
list := []int{1, 2, 3, 4, 5}
result, found := First(list)
fmt.Printf("%d %t", result, found)
// Output: 1 true
}
func ExampleFirst_empty() {
list := []int{}
result, found := First(list)
fmt.Printf("%d %t", result, found)
// Output: 0 false
}
func ExampleFirstOrEmpty() {
list := []int{1, 2, 3, 4, 5}
result := FirstOrEmpty(list)
fmt.Printf("%d", result)
// Output: 1
}
func ExampleFirstOrEmpty_empty() {
list := []int{}
result := FirstOrEmpty(list)
fmt.Printf("%d", result)
// Output: 0
}
func ExampleFirstOr() {
list := []int{1, 2, 3, 4, 5}
result := FirstOr(list, -1)
fmt.Printf("%d", result)
// Output: 1
}
func ExampleFirstOr_empty() {
list := []int{}
result := FirstOr(list, -1)
fmt.Printf("%d", result)
// Output: -1
}
func ExampleLast() {
list := []int{1, 2, 3, 4, 5}
result, found := Last(list)
fmt.Printf("%d %t", result, found)
// Output: 5 true
}
func ExampleLast_empty() {
list := []int{}
result, found := Last(list)
fmt.Printf("%d %t", result, found)
// Output: 0 false
}
func ExampleLastOrEmpty() {
list := []int{1, 2, 3, 4, 5}
result := LastOrEmpty(list)
fmt.Printf("%d", result)
// Output: 5
}
func ExampleLastOrEmpty_empty() {
list := []int{}
result := LastOrEmpty(list)
fmt.Printf("%d", result)
// Output: 0
}
func ExampleLastOr() {
list := []int{1, 2, 3, 4, 5}
result := LastOr(list, -1)
fmt.Printf("%d", result)
// Output: 5
}
func ExampleLastOr_empty() {
list := []int{}
result := LastOr(list, -1)
fmt.Printf("%d", result)
// Output: -1
}
func ExampleNth() {
list := []int{1, 2, 3, 4, 5}
result, err := Nth(list, 2)
fmt.Printf("%d %v", result, err)
// Output: 3 <nil>
}
func ExampleNth_negative() {
list := []int{1, 2, 3, 4, 5}
result, err := Nth(list, -2)
fmt.Printf("%d %v", result, err)
// Output: 4 <nil>
}
func ExampleNth_outOfBounds() {
list := []int{1, 2, 3, 4, 5}
result, err := Nth(list, 10)
fmt.Printf("%d %v", result, err)
// Output: 0 nth: 10 out of slice bounds
}
func ExampleNthOr() {
list := []int{1, 2, 3, 4, 5}
result := NthOr(list, 2, -1)
fmt.Printf("%d", result)
// Output: 3
}
func ExampleNthOr_outOfBounds() {
list := []int{1, 2, 3, 4, 5}
result := NthOr(list, 10, -1)
fmt.Printf("%d", result)
// Output: -1
}
func ExampleNthOrEmpty() {
list := []int{1, 2, 3, 4, 5}
result := NthOrEmpty(list, 2)
fmt.Printf("%d", result)
// Output: 3
}
func ExampleNthOrEmpty_outOfBounds() {
list := []int{1, 2, 3, 4, 5}
result := NthOrEmpty(list, 10)
fmt.Printf("%d", result)
// Output: 0
}

View File

@@ -1,34 +0,0 @@
package lo
import (
"fmt"
)
func ExampleWithoutBy() {
type User struct {
ID int
Name string
}
// original users
users := []User{
{ID: 1, Name: "Alice"},
{ID: 2, Name: "Bob"},
{ID: 3, Name: "Charlie"},
}
// exclude users with IDs 2 and 3
excludedIDs := []int{2, 3}
// extract function to get the user ID
extractID := func(user User) int {
return user.ID
}
// filtering users
filteredUsers := WithoutBy(users, extractID, excludedIDs...)
// output the filtered users
fmt.Printf("%v", filteredUsers)
// Output:
// [{1 Alice}]
}

3324
lo_example_test.go Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -3,8 +3,16 @@ package lo
import (
"testing"
"time"
"github.com/samber/lo/internal/xtime"
"go.uber.org/goleak"
)
func TestMain(m *testing.M) {
xtime.SetClock(xtime.NewFakeClock())
goleak.VerifyTestMain(m)
}
// https://github.com/stretchr/testify/issues/1101
func testWithTimeout(t *testing.T, timeout time.Duration) {
t.Helper()

View File

@@ -1,13 +0,0 @@
package lo
import (
"testing"
"github.com/samber/lo/internal/xtime"
"go.uber.org/goleak"
)
func TestMain(m *testing.M) {
xtime.SetClock(xtime.NewFakeClock())
goleak.VerifyTestMain(m)
}

View File

@@ -1,268 +0,0 @@
package lo
import (
"fmt"
"sort"
"strconv"
"strings"
)
func ExampleKeys() {
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"baz": 3}
result := Keys(kv, kv2)
sort.Strings(result)
fmt.Printf("%v", result)
// Output: [bar baz foo]
}
func ExampleUniqKeys() {
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"bar": 3}
result := UniqKeys(kv, kv2)
sort.Strings(result)
fmt.Printf("%v", result)
// Output: [bar foo]
}
func ExampleValues() {
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"baz": 3}
result := Values(kv, kv2)
sort.Ints(result)
fmt.Printf("%v", result)
// Output: [1 2 3]
}
func ExampleUniqValues() {
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"baz": 2}
result := UniqValues(kv, kv2)
sort.Ints(result)
fmt.Printf("%v", result)
// Output: [1 2]
}
func ExampleValueOr() {
kv := map[string]int{"foo": 1, "bar": 2}
result1 := ValueOr(kv, "foo", 42)
result2 := ValueOr(kv, "baz", 42)
fmt.Printf("%v %v", result1, result2)
// Output: 1 42
}
func ExamplePickBy() {
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := PickBy(kv, func(key string, value int) bool {
return value%2 == 1
})
fmt.Printf("%v %v %v", len(result), result["foo"], result["baz"])
// Output: 2 1 3
}
func ExamplePickByKeys() {
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := PickByKeys(kv, []string{"foo", "baz"})
fmt.Printf("%v %v %v", len(result), result["foo"], result["baz"])
// Output: 2 1 3
}
func ExamplePickByValues() {
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := PickByValues(kv, []int{1, 3})
fmt.Printf("%v %v %v", len(result), result["foo"], result["baz"])
// Output: 2 1 3
}
func ExampleOmitBy() {
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := OmitBy(kv, func(key string, value int) bool {
return value%2 == 1
})
fmt.Printf("%v", result)
// Output: map[bar:2]
}
func ExampleOmitByKeys() {
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := OmitByKeys(kv, []string{"foo", "baz"})
fmt.Printf("%v", result)
// Output: map[bar:2]
}
func ExampleOmitByValues() {
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := OmitByValues(kv, []int{1, 3})
fmt.Printf("%v", result)
// Output: map[bar:2]
}
func ExampleEntries() {
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := Entries(kv)
sort.Slice(result, func(i, j int) bool {
return strings.Compare(result[i].Key, result[j].Key) < 0
})
fmt.Printf("%v", result)
// Output: [{bar 2} {baz 3} {foo 1}]
}
func ExampleFromEntries() {
result := FromEntries([]Entry[string, int]{
{
Key: "foo",
Value: 1,
},
{
Key: "bar",
Value: 2,
},
{
Key: "baz",
Value: 3,
},
})
fmt.Printf("%v %v %v %v", len(result), result["foo"], result["bar"], result["baz"])
// Output: 3 1 2 3
}
func ExampleInvert() {
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := Invert(kv)
fmt.Printf("%v %v %v %v", len(result), result[1], result[2], result[3])
// Output: 3 foo bar baz
}
func ExampleAssign() {
result := Assign(
map[string]int{"a": 1, "b": 2},
map[string]int{"b": 3, "c": 4},
)
fmt.Printf("%v %v %v %v", len(result), result["a"], result["b"], result["c"])
// Output: 3 1 3 4
}
func ExampleChunkEntries() {
result := ChunkEntries(
map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
},
3,
)
for i := range result {
fmt.Printf("%d\n", len(result[i]))
}
// Output:
// 3
// 2
}
func ExampleMapKeys() {
kv := map[int]int{1: 1, 2: 2, 3: 3, 4: 4}
result := MapKeys(kv, func(_, k int) string {
return strconv.FormatInt(int64(k), 10)
})
fmt.Printf("%v %v %v %v %v", len(result), result["1"], result["2"], result["3"], result["4"])
// Output: 4 1 2 3 4
}
func ExampleMapValues() {
kv := map[int]int{1: 1, 2: 2, 3: 3, 4: 4}
result := MapValues(kv, func(v, _ int) string {
return strconv.FormatInt(int64(v), 10)
})
fmt.Printf("%v %q %q %q %q", len(result), result[1], result[2], result[3], result[4])
// Output: 4 "1" "2" "3" "4"
}
func ExampleMapEntries() {
kv := map[string]int{"foo": 1, "bar": 2}
result := MapEntries(kv, func(k string, v int) (int, string) {
return v, k
})
fmt.Printf("%v", result)
// Output: map[1:foo 2:bar]
}
func ExampleMapToSlice() {
kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}
result := MapToSlice(kv, func(k int, v int64) string {
return fmt.Sprintf("%d_%d", k, v)
})
sort.Strings(result)
fmt.Printf("%v", result)
// Output: [1_1 2_2 3_3 4_4]
}
func ExampleFilterMapToSlice() {
kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}
result := FilterMapToSlice(kv, func(k int, v int64) (string, bool) {
return fmt.Sprintf("%d_%d", k, v), k%2 == 0
})
sort.Strings(result)
fmt.Printf("%v", result)
// Output: [2_2 4_4]
}
func ExampleFilterKeys() {
kv := map[int]string{1: "foo", 2: "bar", 3: "baz"}
result := FilterKeys(kv, func(k int, v string) bool {
return v == "foo"
})
fmt.Printf("%v", result)
// Output: [1]
}
func ExampleFilterValues() {
kv := map[int]string{1: "foo", 2: "bar", 3: "baz"}
result := FilterValues(kv, func(k int, v string) bool {
return v == "foo"
})
fmt.Printf("%v", result)
// Output: [foo]
}

View File

@@ -1,108 +0,0 @@
package lo
import (
"fmt"
)
func ExampleRange() {
result1 := Range(4)
result2 := Range(-4)
result3 := RangeFrom(1, 5)
result4 := RangeFrom(1.0, 5)
result5 := RangeWithSteps(0, 20, 5)
result6 := RangeWithSteps[float32](-1.0, -4.0, -1.0)
result7 := RangeWithSteps(1, 4, -1)
result8 := Range(0)
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
fmt.Printf("%v\n", result7)
fmt.Printf("%v\n", result8)
// Output:
// [0 1 2 3]
// [0 -1 -2 -3]
// [1 2 3 4 5]
// [1 2 3 4 5]
// [0 5 10 15]
// [-1 -2 -3]
// []
// []
}
func ExampleClamp() {
result1 := Clamp(0, -10, 10)
result2 := Clamp(-42, -10, 10)
result3 := Clamp(42, -10, 10)
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
// Output:
// 0
// -10
// 10
}
func ExampleSum() {
list := []int{1, 2, 3, 4, 5}
sum := Sum(list)
fmt.Printf("%v", sum)
// Output: 15
}
func ExampleSumBy() {
list := []string{"foo", "bar"}
result := SumBy(list, func(item string) int {
return len(item)
})
fmt.Printf("%v", result)
// Output: 6
}
func ExampleProduct() {
list := []int{1, 2, 3, 4, 5}
result := Product(list)
fmt.Printf("%v", result)
// Output: 120
}
func ExampleProductBy() {
list := []string{"foo", "bar"}
result := ProductBy(list, func(item string) int {
return len(item)
})
fmt.Printf("%v", result)
// Output: 9
}
func ExampleMean() {
list := []int{1, 2, 3, 4, 5}
result := Mean(list)
fmt.Printf("%v", result)
// Output: 3
}
func ExampleMeanBy() {
list := []string{"foo", "bar"}
result := MeanBy(list, func(item string) int {
return len(item)
})
fmt.Printf("%v", result)
// Output: 3
}

View File

@@ -1,717 +0,0 @@
package lo
import (
"fmt"
"math"
"sort"
"strconv"
)
func ExampleFilter() {
list := []int64{1, 2, 3, 4}
result := Filter(list, func(nbr int64, index int) bool {
return nbr%2 == 0
})
fmt.Printf("%v", result)
// Output: [2 4]
}
func ExampleMap() {
list := []int64{1, 2, 3, 4}
result := Map(list, func(nbr int64, index int) string {
return strconv.FormatInt(nbr*2, 10)
})
fmt.Printf("%v", result)
// Output: [2 4 6 8]
}
func ExampleUniqMap() {
type User struct {
Name string
Age int
}
users := []User{{Name: "Alex", Age: 10}, {Name: "Alex", Age: 12}, {Name: "Bob", Age: 11}, {Name: "Alice", Age: 20}}
result := UniqMap(users, func(u User, index int) string {
return u.Name
})
sort.Strings(result)
fmt.Printf("%v", result)
// Output: [Alex Alice Bob]
}
func ExampleFilterMap() {
list := []int64{1, 2, 3, 4}
result := FilterMap(list, func(nbr int64, index int) (string, bool) {
return strconv.FormatInt(nbr*2, 10), nbr%2 == 0
})
fmt.Printf("%v", result)
// Output: [4 8]
}
func ExampleFlatMap() {
list := []int64{1, 2, 3, 4}
result := FlatMap(list, func(nbr int64, index int) []string {
return []string{
strconv.FormatInt(nbr, 10), // base 10
strconv.FormatInt(nbr, 2), // base 2
}
})
fmt.Printf("%v", result)
// Output: [1 1 2 10 3 11 4 100]
}
func ExampleReduce() {
list := []int64{1, 2, 3, 4}
result := Reduce(list, func(agg, item int64, index int) int64 {
return agg + item
}, 0)
fmt.Printf("%v", result)
// Output: 10
}
func ExampleReduceRight() {
list := [][]int{{0, 1}, {2, 3}, {4, 5}}
result := ReduceRight(list, func(agg, item []int, index int) []int {
return append(agg, item...)
}, []int{})
fmt.Printf("%v", result)
// Output: [4 5 2 3 0 1]
}
func ExampleForEach() {
list := []int64{1, 2, 3, 4}
ForEach(list, func(x int64, _ int) {
fmt.Println(x)
})
// Output:
// 1
// 2
// 3
// 4
}
func ExampleForEachWhile() {
list := []int64{1, 2, -math.MaxInt, 4}
ForEachWhile(list, func(x int64, _ int) bool {
if x < 0 {
return false
}
fmt.Println(x)
return true
})
// Output:
// 1
// 2
}
func ExampleTimes() {
result := Times(3, func(i int) string {
return strconv.FormatInt(int64(i), 10)
})
fmt.Printf("%v", result)
// Output: [0 1 2]
}
func ExampleUniq() {
list := []int{1, 2, 2, 1}
result := Uniq(list)
fmt.Printf("%v", result)
// Output: [1 2]
}
func ExampleUniqBy() {
list := []int{0, 1, 2, 3, 4, 5}
result := UniqBy(list, func(i int) int {
return i % 3
})
fmt.Printf("%v", result)
// Output: [0 1 2]
}
func ExampleGroupBy() {
list := []int{0, 1, 2, 3, 4, 5}
result := GroupBy(list, func(i int) int {
return i % 3
})
fmt.Printf("%v\n", result[0])
fmt.Printf("%v\n", result[1])
fmt.Printf("%v\n", result[2])
// Output:
// [0 3]
// [1 4]
// [2 5]
}
func ExampleGroupByMap() {
list := []int{0, 1, 2, 3, 4, 5}
result := GroupByMap(list, func(i int) (int, int) {
return i % 3, i * 2
})
fmt.Printf("%v\n", result[0])
fmt.Printf("%v\n", result[1])
fmt.Printf("%v\n", result[2])
// Output:
// [0 6]
// [2 8]
// [4 10]
}
func ExampleChunk() {
list := []int{0, 1, 2, 3, 4}
result := Chunk(list, 2)
for _, item := range result {
fmt.Printf("%v\n", item)
}
// Output:
// [0 1]
// [2 3]
// [4]
}
func ExamplePartitionBy() {
list := []int{-2, -1, 0, 1, 2, 3, 4}
result := PartitionBy(list, func(x int) string {
if x < 0 {
return "negative"
} else if x%2 == 0 {
return "even"
}
return "odd"
})
for _, item := range result {
fmt.Printf("%v\n", item)
}
// Output:
// [-2 -1]
// [0 2 4]
// [1 3]
}
func ExampleFlatten() {
list := [][]int{{0, 1, 2}, {3, 4, 5}}
result := Flatten(list)
fmt.Printf("%v", result)
// Output: [0 1 2 3 4 5]
}
func ExampleInterleave() {
list1 := [][]int{{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}
list2 := [][]int{{1}, {2, 5, 8}, {3, 6}, {4, 7, 9, 10}}
result1 := Interleave(list1...)
result2 := Interleave(list2...)
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
// Output:
// [1 2 3 4 5 6 7 8 9]
// [1 2 3 4 5 6 7 8 9 10]
}
func ExampleShuffle() {
list := []int{0, 1, 2, 3, 4, 5}
result := Shuffle(list)
fmt.Printf("%v", result)
}
func ExampleReverse() {
list := []int{0, 1, 2, 3, 4, 5}
result := Reverse(list)
fmt.Printf("%v", result)
// Output: [5 4 3 2 1 0]
}
func ExampleFill() {
list := []foo{{"a"}, {"a"}}
result := Fill(list, foo{"b"})
fmt.Printf("%v", result)
// Output: [{b} {b}]
}
func ExampleRepeat() {
result := Repeat(2, foo{"a"})
fmt.Printf("%v", result)
// Output: [{a} {a}]
}
func ExampleRepeatBy() {
result := RepeatBy(5, func(i int) string {
return strconv.FormatInt(int64(math.Pow(float64(i), 2)), 10)
})
fmt.Printf("%v", result)
// Output: [0 1 4 9 16]
}
func ExampleKeyBy() {
list := []string{"a", "aa", "aaa"}
result := KeyBy(list, func(str string) int {
return len(str)
})
fmt.Printf("%v", result)
// Output: map[1:a 2:aa 3:aaa]
}
func ExampleSliceToMap() {
list := []string{"a", "aa", "aaa"}
result := SliceToMap(list, func(str string) (string, int) {
return str, len(str)
})
fmt.Printf("%v", result)
// Output: map[a:1 aa:2 aaa:3]
}
func ExampleFilterSliceToMap() {
list := []string{"a", "aa", "aaa"}
result := FilterSliceToMap(list, func(str string) (string, int, bool) {
return str, len(str), len(str) > 1
})
fmt.Printf("%v", result)
// Output: map[aa:2 aaa:3]
}
func ExampleKeyify() {
list := []string{"a", "a", "b", "b", "d"}
set := Keyify(list)
_, ok1 := set["a"]
_, ok2 := set["c"]
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", set)
// Output:
// true
// false
// map[a:{} b:{} d:{}]
}
func ExampleDrop() {
list := []int{0, 1, 2, 3, 4, 5}
result := Drop(list, 2)
fmt.Printf("%v", result)
// Output: [2 3 4 5]
}
func ExampleDropRight() {
list := []int{0, 1, 2, 3, 4, 5}
result := DropRight(list, 2)
fmt.Printf("%v", result)
// Output: [0 1 2 3]
}
func ExampleDropWhile() {
list := []int{0, 1, 2, 3, 4, 5}
result := DropWhile(list, func(val int) bool {
return val < 2
})
fmt.Printf("%v", result)
// Output: [2 3 4 5]
}
func ExampleDropRightWhile() {
list := []int{0, 1, 2, 3, 4, 5}
result := DropRightWhile(list, func(val int) bool {
return val > 2
})
fmt.Printf("%v", result)
// Output: [0 1 2]
}
func ExampleDropByIndex() {
list := []int{0, 1, 2, 3, 4, 5}
result := DropByIndex(list, 2)
fmt.Printf("%v", result)
// Output: [0 1 3 4 5]
}
func ExampleReject() {
list := []int{0, 1, 2, 3, 4, 5}
result := Reject(list, func(x, _ int) bool {
return x%2 == 0
})
fmt.Printf("%v", result)
// Output: [1 3 5]
}
func ExampleCount() {
list := []int{0, 1, 2, 3, 4, 5, 0, 1, 2, 3}
result := Count(list, 2)
fmt.Printf("%v", result)
// Output: 2
}
func ExampleCountBy() {
list := []int{0, 1, 2, 3, 4, 5, 0, 1, 2, 3}
result := CountBy(list, func(i int) bool {
return i < 4
})
fmt.Printf("%v", result)
// Output: 8
}
func ExampleCountValues() {
result1 := CountValues([]int{})
result2 := CountValues([]int{1, 2})
result3 := CountValues([]int{1, 2, 2})
result4 := CountValues([]string{"foo", "bar", ""})
result5 := CountValues([]string{"foo", "bar", "bar"})
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
// Output:
// map[]
// map[1:1 2:1]
// map[1:1 2:2]
// map[:1 bar:1 foo:1]
// map[bar:2 foo:1]
}
func ExampleCountValuesBy() {
isEven := func(v int) bool {
return v%2 == 0
}
result1 := CountValuesBy([]int{}, isEven)
result2 := CountValuesBy([]int{1, 2}, isEven)
result3 := CountValuesBy([]int{1, 2, 2}, isEven)
length := func(v string) int {
return len(v)
}
result4 := CountValuesBy([]string{"foo", "bar", ""}, length)
result5 := CountValuesBy([]string{"foo", "bar", "bar"}, length)
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
// Output:
// map[]
// map[false:1 true:1]
// map[false:1 true:2]
// map[0:1 3:2]
// map[3:3]
}
func ExampleSubset() {
list := []int{0, 1, 2, 3, 4, 5}
result := Subset(list, 2, 3)
fmt.Printf("%v", result)
// Output: [2 3 4]
}
func ExampleSlice() {
list := []int{0, 1, 2, 3, 4, 5}
result := Slice(list, 1, 4)
fmt.Printf("%v\n", result)
result = Slice(list, 4, 1)
fmt.Printf("%v\n", result)
result = Slice(list, 4, 5)
fmt.Printf("%v\n", result)
// Output:
// [1 2 3]
// []
// [4]
}
func ExampleReplace() {
list := []int{0, 1, 0, 1, 2, 3, 0}
result := Replace(list, 0, 42, 1)
fmt.Printf("%v\n", result)
result = Replace(list, -1, 42, 1)
fmt.Printf("%v\n", result)
result = Replace(list, 0, 42, 2)
fmt.Printf("%v\n", result)
result = Replace(list, 0, 42, -1)
fmt.Printf("%v\n", result)
// Output:
// [42 1 0 1 2 3 0]
// [0 1 0 1 2 3 0]
// [42 1 42 1 2 3 0]
// [42 1 42 1 2 3 42]
}
func ExampleCompact() {
list := []string{"", "foo", "", "bar", ""}
result := Compact(list)
fmt.Printf("%v", result)
// Output: [foo bar]
}
func ExampleIsSorted() {
list := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
result := IsSorted(list)
fmt.Printf("%v", result)
// Output: true
}
func ExampleIsSortedByKey() {
list := []string{"a", "bb", "ccc"}
result := IsSortedByKey(list, func(s string) int {
return len(s)
})
fmt.Printf("%v", result)
// Output: true
}
func ExampleCut() {
collection := []string{"a", "b", "c", "d", "e", "f", "g"}
// Test with valid separator
before, after, found := Cut(collection, []string{"b", "c", "d"})
fmt.Printf("Before: %v, After: %v, Found: %t\n", before, after, found)
// Test with separator not found
before2, after2, found2 := Cut(collection, []string{"z"})
fmt.Printf("Before: %v, After: %v, Found: %t\n", before2, after2, found2)
// Test with separator at beginning
before3, after3, found3 := Cut(collection, []string{"a", "b"})
fmt.Printf("Before: %v, After: %v, Found: %t\n", before3, after3, found3)
// Output:
// Before: [a], After: [e f g], Found: true
// Before: [a b c d e f g], After: [], Found: false
// Before: [], After: [c d e f g], Found: true
}
func ExampleCutPrefix() {
collection := []string{"a", "b", "c", "d", "e", "f", "g"}
// Test with valid prefix
after, found := CutPrefix(collection, []string{"a", "b", "c"})
fmt.Printf("After: %v, Found: %t\n", after, found)
// Test with prefix not found
after2, found2 := CutPrefix(collection, []string{"b"})
fmt.Printf("After: %v, Found: %t\n", after2, found2)
// Test with empty prefix
after3, found3 := CutPrefix(collection, []string{})
fmt.Printf("After: %v, Found: %t\n", after3, found3)
// Output:
// After: [d e f g], Found: true
// After: [a b c d e f g], Found: false
// After: [a b c d e f g], Found: true
}
func ExampleCutSuffix() {
collection := []string{"a", "b", "c", "d", "e", "f", "g"}
// Test with valid suffix
before, found := CutSuffix(collection, []string{"f", "g"})
fmt.Printf("Before: %v, Found: %t\n", before, found)
// Test with suffix not found
before2, found2 := CutSuffix(collection, []string{"b"})
fmt.Printf("Before: %v, Found: %t\n", before2, found2)
// Test with empty suffix
before3, found3 := CutSuffix(collection, []string{})
fmt.Printf("Before: %v, Found: %t\n", before3, found3)
// Output:
// Before: [a b c d e], Found: true
// Before: [a b c d e f g], Found: false
// Before: [a b c d e f g], Found: true
}
func ExampleTrim() {
collection := []int{0, 1, 2, 0, 3, 0}
// Test with valid cutset
result := Trim(collection, []int{0})
fmt.Printf("Trim with cutset {0}: %v\n", result)
// Test with string collection
words := []string{" hello ", "world", " "}
result2 := Trim(words, []string{" "})
fmt.Printf("Trim with string cutset: %v\n", result2)
// Test with no cutset elements
result3 := Trim(collection, []int{5})
fmt.Printf("Trim with cutset {5} (not present): %v\n", result3)
// Output:
// Trim with cutset {0}: [1 2 0 3]
// Trim with string cutset: [ hello world ]
// Trim with cutset {5} (not present): [0 1 2 0 3 0]
}
func ExampleTrimLeft() {
collection := []int{0, 1, 2, 0, 3, 0}
// Test with valid cutset
result := TrimLeft(collection, []int{0})
fmt.Printf("TrimLeft with cutset {0}: %v\n", result)
// Test with string collection
words := []string{" hello ", "world", " "}
result2 := TrimLeft(words, []string{" "})
fmt.Printf("TrimLeft with string cutset: %v\n", result2)
// Test with no cutset elements
result3 := TrimLeft(collection, []int{5})
fmt.Printf("TrimLeft with cutset {5} (not present): %v\n", result3)
// Output:
// TrimLeft with cutset {0}: [1 2 0 3 0]
// TrimLeft with string cutset: [ hello world ]
// TrimLeft with cutset {5} (not present): [0 1 2 0 3 0]
}
func ExampleTrimPrefix() {
collection := []int{1, 2, 1, 2, 3}
// Test with valid prefix
result := TrimPrefix(collection, []int{1, 2})
fmt.Printf("TrimPrefix with prefix {1,2}: %v\n", result)
// Test with string collection
words := []string{"hello", "hello", "world"}
result2 := TrimPrefix(words, []string{"hello"})
fmt.Printf("TrimPrefix with string prefix: %v\n", result2)
// Test with prefix not present
result3 := TrimPrefix(collection, []int{5, 6})
fmt.Printf("TrimPrefix with prefix {5,6} (not present): %v\n", result3)
// Output:
// TrimPrefix with prefix {1,2}: [3]
// TrimPrefix with string prefix: [world]
// TrimPrefix with prefix {5,6} (not present): [1 2 1 2 3]
}
func ExampleTrimRight() {
collection := []int{0, 1, 2, 0, 3, 0}
// Test with valid cutset
result := TrimRight(collection, []int{0})
fmt.Printf("TrimRight with cutset {0}: %v\n", result)
// Test with string collection
words := []string{" hello ", "world", " "}
result2 := TrimRight(words, []string{" "})
fmt.Printf("TrimRight with string cutset: %v\n", result2)
// Test with no cutset elements
result3 := TrimRight(collection, []int{5})
fmt.Printf("TrimRight with cutset {5} (not present): %v\n", result3)
// Output:
// TrimRight with cutset {0}: [0 1 2 0 3]
// TrimRight with string cutset: [ hello world ]
// TrimRight with cutset {5} (not present): [0 1 2 0 3 0]
}
func ExampleTrimSuffix() {
collection := []int{1, 2, 1, 2, 3}
// Test with valid suffix
result := TrimSuffix(collection, []int{1, 2})
fmt.Printf("TrimSuffix with suffix {1,2}: %v\n", result)
// Test with string collection
words := []string{"hello", "world", "test"}
result2 := TrimSuffix(words, []string{"test"})
fmt.Printf("TrimSuffix with string suffix: %v\n", result2)
// Test with suffix not present
result3 := TrimSuffix(collection, []int{5, 6})
fmt.Printf("TrimSuffix with suffix {5,6} (not present): %v\n", result3)
// Output:
// TrimSuffix with suffix {1,2}: [1 2 1 2 3]
// TrimSuffix with string suffix: [hello world]
// TrimSuffix with suffix {5,6} (not present): [1 2 1 2 3]
}

View File

@@ -1,54 +0,0 @@
package lo
import (
"fmt"
"math"
)
func ExampleSubstring() {
result1 := Substring("hello", 2, 3)
result2 := Substring("hello", -4, 3)
result3 := Substring("hello", -2, math.MaxUint)
result4 := Substring("🏠🐶🐱", 0, 2)
result5 := Substring("你好,世界", 0, 3)
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
// Output:
// llo
// ell
// lo
// 🏠🐶
// 你好,
}
func ExampleChunkString() {
result1 := ChunkString("123456", 2)
result2 := ChunkString("1234567", 2)
result3 := ChunkString("", 2)
result4 := ChunkString("1", 2)
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
// Output:
// [12 34 56]
// [12 34 56 7]
// []
// [1]
}
func ExampleRuneLength() {
result1, chars1 := RuneLength("hellô"), len("hellô")
result2, chars2 := RuneLength("🤘"), len("🤘")
fmt.Printf("%v %v\n", result1, chars1)
fmt.Printf("%v %v\n", result2, chars2)
// Output:
// 5 6
// 1 4
}

View File

@@ -1,581 +0,0 @@
package lo
import (
"fmt"
)
func ExampleT2() {
result := T2("hello", 2)
fmt.Printf("%v %v", result.A, result.B)
// Output: hello 2
}
func ExampleT3() {
result := T3("hello", 2, true)
fmt.Printf("%v %v %v", result.A, result.B, result.C)
// Output: hello 2 true
}
func ExampleT4() {
result := T4("hello", 2, true, foo{bar: "bar"})
fmt.Printf("%v %v %v %v", result.A, result.B, result.C, result.D)
// Output: hello 2 true {bar}
}
func ExampleT5() {
result := T5("hello", 2, true, foo{bar: "bar"}, 4.2)
fmt.Printf("%v %v %v %v %v", result.A, result.B, result.C, result.D, result.E)
// Output: hello 2 true {bar} 4.2
}
func ExampleT6() {
result := T6("hello", 2, true, foo{bar: "bar"}, 4.2, "plop")
fmt.Printf("%v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F)
// Output: hello 2 true {bar} 4.2 plop
}
func ExampleT7() {
result := T7("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false)
fmt.Printf("%v %v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F, result.G)
// Output: hello 2 true {bar} 4.2 plop false
}
func ExampleT8() {
result := T8("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42)
fmt.Printf("%v %v %v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F, result.G, result.H)
// Output: hello 2 true {bar} 4.2 plop false 42
}
func ExampleT9() {
result := T9("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42, "hello world")
fmt.Printf("%v %v %v %v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F, result.G, result.H, result.I)
// Output: hello 2 true {bar} 4.2 plop false 42 hello world
}
func ExampleUnpack2() {
a, b := Unpack2(T2("hello", 2))
fmt.Printf("%v %v", a, b)
// Output: hello 2
}
func ExampleUnpack3() {
a, b, c := Unpack3(T3("hello", 2, true))
fmt.Printf("%v %v %v", a, b, c)
// Output: hello 2 true
}
func ExampleUnpack4() {
a, b, c, d := Unpack4(T4("hello", 2, true, foo{bar: "bar"}))
fmt.Printf("%v %v %v %v", a, b, c, d)
// Output: hello 2 true {bar}
}
func ExampleUnpack5() {
a, b, c, d, e := Unpack5(T5("hello", 2, true, foo{bar: "bar"}, 4.2))
fmt.Printf("%v %v %v %v %v", a, b, c, d, e)
// Output: hello 2 true {bar} 4.2
}
func ExampleUnpack6() {
a, b, c, d, e, f := Unpack6(T6("hello", 2, true, foo{bar: "bar"}, 4.2, "plop"))
fmt.Printf("%v %v %v %v %v %v", a, b, c, d, e, f)
// Output: hello 2 true {bar} 4.2 plop
}
func ExampleUnpack7() {
a, b, c, d, e, f, g := Unpack7(T7("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false))
fmt.Printf("%v %v %v %v %v %v %v", a, b, c, d, e, f, g)
// Output: hello 2 true {bar} 4.2 plop false
}
func ExampleUnpack8() {
a, b, c, d, e, f, g, h := Unpack8(T8("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42))
fmt.Printf("%v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h)
// Output: hello 2 true {bar} 4.2 plop false 42
}
func ExampleUnpack9() {
a, b, c, d, e, f, g, h, i := Unpack9(T9("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42, "hello world"))
fmt.Printf("%v %v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h, i)
// Output: hello 2 true {bar} 4.2 plop false 42 hello world
}
func ExampleZip2() {
result := Zip2([]string{"hello"}, []int{2})
fmt.Printf("%v", result)
// Output: [{hello 2}]
}
func ExampleZip3() {
result := Zip3([]string{"hello"}, []int{2}, []bool{true})
fmt.Printf("%v", result)
// Output: [{hello 2 true}]
}
func ExampleZip4() {
result := Zip4([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}})
fmt.Printf("%v", result)
// Output: [{hello 2 true {bar}}]
}
func ExampleZip5() {
result := Zip5([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2})
fmt.Printf("%v", result)
// Output: [{hello 2 true {bar} 4.2}]
}
func ExampleZip6() {
result := Zip6([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"})
fmt.Printf("%v", result)
// Output: [{hello 2 true {bar} 4.2 plop}]
}
func ExampleZip7() {
result := Zip7([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false})
fmt.Printf("%v", result)
// Output: [{hello 2 true {bar} 4.2 plop false}]
}
func ExampleZip8() {
result := Zip8([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42})
fmt.Printf("%v", result)
// Output: [{hello 2 true {bar} 4.2 plop false 42}]
}
func ExampleZip9() {
result := Zip9([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42}, []string{"hello world"})
fmt.Printf("%v", result)
// Output: [{hello 2 true {bar} 4.2 plop false 42 hello world}]
}
func ExampleUnzip2() {
a, b := Unzip2([]Tuple2[string, int]{T2("hello", 2)})
fmt.Printf("%v %v", a, b)
// Output: [hello] [2]
}
func ExampleUnzip3() {
a, b, c := Unzip3([]Tuple3[string, int, bool]{T3("hello", 2, true)})
fmt.Printf("%v %v %v", a, b, c)
// Output: [hello] [2] [true]
}
func ExampleUnzip4() {
a, b, c, d := Unzip4([]Tuple4[string, int, bool, foo]{T4("hello", 2, true, foo{bar: "bar"})})
fmt.Printf("%v %v %v %v", a, b, c, d)
// Output: [hello] [2] [true] [{bar}]
}
func ExampleUnzip5() {
a, b, c, d, e := Unzip5([]Tuple5[string, int, bool, foo, float64]{T5("hello", 2, true, foo{bar: "bar"}, 4.2)})
fmt.Printf("%v %v %v %v %v", a, b, c, d, e)
// Output: [hello] [2] [true] [{bar}] [4.2]
}
func ExampleUnzip6() {
a, b, c, d, e, f := Unzip6([]Tuple6[string, int, bool, foo, float64, string]{T6("hello", 2, true, foo{bar: "bar"}, 4.2, "plop")})
fmt.Printf("%v %v %v %v %v %v", a, b, c, d, e, f)
// Output: [hello] [2] [true] [{bar}] [4.2] [plop]
}
func ExampleUnzip7() {
a, b, c, d, e, f, g := Unzip7([]Tuple7[string, int, bool, foo, float64, string, bool]{T7("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false)})
fmt.Printf("%v %v %v %v %v %v %v", a, b, c, d, e, f, g)
// Output: [hello] [2] [true] [{bar}] [4.2] [plop] [false]
}
func ExampleUnzip8() {
a, b, c, d, e, f, g, h := Unzip8([]Tuple8[string, int, bool, foo, float64, string, bool, int]{T8("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42)})
fmt.Printf("%v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h)
// Output: [hello] [2] [true] [{bar}] [4.2] [plop] [false] [42]
}
func ExampleUnzip9() {
a, b, c, d, e, f, g, h, i := Unzip9([]Tuple9[string, int, bool, foo, float64, string, bool, int, string]{T9("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42, "hello world")})
fmt.Printf("%v %v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h, i)
// Output: [hello] [2] [true] [{bar}] [4.2] [plop] [false] [42] [hello world]
}
func ExampleCrossJoin2() {
result := CrossJoin2([]string{"a", "b"}, []int{1, 2, 3, 4})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// {a 1}
// {a 2}
// {a 3}
// {a 4}
// {b 1}
// {b 2}
// {b 3}
// {b 4}
}
func ExampleCrossJoin3() {
result := CrossJoin3([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// {a 1 true}
// {a 1 false}
// {a 2 true}
// {a 2 false}
// {a 3 true}
// {a 3 false}
// {a 4 true}
// {a 4 false}
// {b 1 true}
// {b 1 false}
// {b 2 true}
// {b 2 false}
// {b 3 true}
// {b 3 false}
// {b 4 true}
// {b 4 false}
}
func ExampleCrossJoin4() {
result := CrossJoin4([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// {a 1 true {bar}}
// {a 1 false {bar}}
// {a 2 true {bar}}
// {a 2 false {bar}}
// {a 3 true {bar}}
// {a 3 false {bar}}
// {a 4 true {bar}}
// {a 4 false {bar}}
// {b 1 true {bar}}
// {b 1 false {bar}}
// {b 2 true {bar}}
// {b 2 false {bar}}
// {b 3 true {bar}}
// {b 3 false {bar}}
// {b 4 true {bar}}
// {b 4 false {bar}}
}
func ExampleCrossJoin5() {
result := CrossJoin5([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// {a 1 true {bar} 4.2}
// {a 1 false {bar} 4.2}
// {a 2 true {bar} 4.2}
// {a 2 false {bar} 4.2}
// {a 3 true {bar} 4.2}
// {a 3 false {bar} 4.2}
// {a 4 true {bar} 4.2}
// {a 4 false {bar} 4.2}
// {b 1 true {bar} 4.2}
// {b 1 false {bar} 4.2}
// {b 2 true {bar} 4.2}
// {b 2 false {bar} 4.2}
// {b 3 true {bar} 4.2}
// {b 3 false {bar} 4.2}
// {b 4 true {bar} 4.2}
// {b 4 false {bar} 4.2}
}
func ExampleCrossJoin6() {
result := CrossJoin6([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// {a 1 true {bar} 4.2 plop}
// {a 1 false {bar} 4.2 plop}
// {a 2 true {bar} 4.2 plop}
// {a 2 false {bar} 4.2 plop}
// {a 3 true {bar} 4.2 plop}
// {a 3 false {bar} 4.2 plop}
// {a 4 true {bar} 4.2 plop}
// {a 4 false {bar} 4.2 plop}
// {b 1 true {bar} 4.2 plop}
// {b 1 false {bar} 4.2 plop}
// {b 2 true {bar} 4.2 plop}
// {b 2 false {bar} 4.2 plop}
// {b 3 true {bar} 4.2 plop}
// {b 3 false {bar} 4.2 plop}
// {b 4 true {bar} 4.2 plop}
// {b 4 false {bar} 4.2 plop}
}
func ExampleCrossJoin7() {
result := CrossJoin7([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// {a 1 true {bar} 4.2 plop false}
// {a 1 false {bar} 4.2 plop false}
// {a 2 true {bar} 4.2 plop false}
// {a 2 false {bar} 4.2 plop false}
// {a 3 true {bar} 4.2 plop false}
// {a 3 false {bar} 4.2 plop false}
// {a 4 true {bar} 4.2 plop false}
// {a 4 false {bar} 4.2 plop false}
// {b 1 true {bar} 4.2 plop false}
// {b 1 false {bar} 4.2 plop false}
// {b 2 true {bar} 4.2 plop false}
// {b 2 false {bar} 4.2 plop false}
// {b 3 true {bar} 4.2 plop false}
// {b 3 false {bar} 4.2 plop false}
// {b 4 true {bar} 4.2 plop false}
// {b 4 false {bar} 4.2 plop false}
}
func ExampleCrossJoin8() {
result := CrossJoin8([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// {a 1 true {bar} 4.2 plop false 42}
// {a 1 false {bar} 4.2 plop false 42}
// {a 2 true {bar} 4.2 plop false 42}
// {a 2 false {bar} 4.2 plop false 42}
// {a 3 true {bar} 4.2 plop false 42}
// {a 3 false {bar} 4.2 plop false 42}
// {a 4 true {bar} 4.2 plop false 42}
// {a 4 false {bar} 4.2 plop false 42}
// {b 1 true {bar} 4.2 plop false 42}
// {b 1 false {bar} 4.2 plop false 42}
// {b 2 true {bar} 4.2 plop false 42}
// {b 2 false {bar} 4.2 plop false 42}
// {b 3 true {bar} 4.2 plop false 42}
// {b 3 false {bar} 4.2 plop false 42}
// {b 4 true {bar} 4.2 plop false 42}
// {b 4 false {bar} 4.2 plop false 42}
}
func ExampleCrossJoin9() {
result := CrossJoin9([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42}, []string{"hello world"})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// {a 1 true {bar} 4.2 plop false 42 hello world}
// {a 1 false {bar} 4.2 plop false 42 hello world}
// {a 2 true {bar} 4.2 plop false 42 hello world}
// {a 2 false {bar} 4.2 plop false 42 hello world}
// {a 3 true {bar} 4.2 plop false 42 hello world}
// {a 3 false {bar} 4.2 plop false 42 hello world}
// {a 4 true {bar} 4.2 plop false 42 hello world}
// {a 4 false {bar} 4.2 plop false 42 hello world}
// {b 1 true {bar} 4.2 plop false 42 hello world}
// {b 1 false {bar} 4.2 plop false 42 hello world}
// {b 2 true {bar} 4.2 plop false 42 hello world}
// {b 2 false {bar} 4.2 plop false 42 hello world}
// {b 3 true {bar} 4.2 plop false 42 hello world}
// {b 3 false {bar} 4.2 plop false 42 hello world}
// {b 4 true {bar} 4.2 plop false 42 hello world}
// {b 4 false {bar} 4.2 plop false 42 hello world}
}
func ExampleCrossJoinBy2() {
result := CrossJoinBy2([]string{"a", "b"}, []int{1, 2, 3, 4}, func(a string, b int) string {
return fmt.Sprintf("%v-%v", a, b)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// a-1
// a-2
// a-3
// a-4
// b-1
// b-2
// b-3
// b-4
}
func ExampleCrossJoinBy3() {
result := CrossJoinBy3([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, func(a string, b int, c bool) string {
return fmt.Sprintf("%v-%v-%v", a, b, c)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// a-1-true
// a-1-false
// a-2-true
// a-2-false
// a-3-true
// a-3-false
// a-4-true
// a-4-false
// b-1-true
// b-1-false
// b-2-true
// b-2-false
// b-3-true
// b-3-false
// b-4-true
// b-4-false
}
func ExampleCrossJoinBy4() {
result := CrossJoinBy4([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, func(a string, b int, c bool, d foo) string {
return fmt.Sprintf("%v-%v-%v-%v", a, b, c, d)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// a-1-true-{bar}
// a-1-false-{bar}
// a-2-true-{bar}
// a-2-false-{bar}
// a-3-true-{bar}
// a-3-false-{bar}
// a-4-true-{bar}
// a-4-false-{bar}
// b-1-true-{bar}
// b-1-false-{bar}
// b-2-true-{bar}
// b-2-false-{bar}
// b-3-true-{bar}
// b-3-false-{bar}
// b-4-true-{bar}
// b-4-false-{bar}
}
func ExampleCrossJoinBy5() {
result := CrossJoinBy5([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, func(a string, b int, c bool, d foo, e float64) string {
return fmt.Sprintf("%v-%v-%v-%v-%v", a, b, c, d, e)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// a-1-true-{bar}-4.2
// a-1-false-{bar}-4.2
// a-2-true-{bar}-4.2
// a-2-false-{bar}-4.2
// a-3-true-{bar}-4.2
// a-3-false-{bar}-4.2
// a-4-true-{bar}-4.2
// a-4-false-{bar}-4.2
// b-1-true-{bar}-4.2
// b-1-false-{bar}-4.2
// b-2-true-{bar}-4.2
// b-2-false-{bar}-4.2
// b-3-true-{bar}-4.2
// b-3-false-{bar}-4.2
// b-4-true-{bar}-4.2
// b-4-false-{bar}-4.2
}
func ExampleCrossJoinBy6() {
result := CrossJoinBy6([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, func(a string, b int, c bool, d foo, e float64, f string) string {
return fmt.Sprintf("%v-%v-%v-%v-%v-%v", a, b, c, d, e, f)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// a-1-true-{bar}-4.2-plop
// a-1-false-{bar}-4.2-plop
// a-2-true-{bar}-4.2-plop
// a-2-false-{bar}-4.2-plop
// a-3-true-{bar}-4.2-plop
// a-3-false-{bar}-4.2-plop
// a-4-true-{bar}-4.2-plop
// a-4-false-{bar}-4.2-plop
// b-1-true-{bar}-4.2-plop
// b-1-false-{bar}-4.2-plop
// b-2-true-{bar}-4.2-plop
// b-2-false-{bar}-4.2-plop
// b-3-true-{bar}-4.2-plop
// b-3-false-{bar}-4.2-plop
// b-4-true-{bar}-4.2-plop
// b-4-false-{bar}-4.2-plop
}
func ExampleCrossJoinBy7() {
result := CrossJoinBy7([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, func(a string, b int, c bool, d foo, e float64, f string, g bool) string {
return fmt.Sprintf("%v-%v-%v-%v-%v-%v-%v", a, b, c, d, e, f, g)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// a-1-true-{bar}-4.2-plop-false
// a-1-false-{bar}-4.2-plop-false
// a-2-true-{bar}-4.2-plop-false
// a-2-false-{bar}-4.2-plop-false
// a-3-true-{bar}-4.2-plop-false
// a-3-false-{bar}-4.2-plop-false
// a-4-true-{bar}-4.2-plop-false
// a-4-false-{bar}-4.2-plop-false
// b-1-true-{bar}-4.2-plop-false
// b-1-false-{bar}-4.2-plop-false
// b-2-true-{bar}-4.2-plop-false
// b-2-false-{bar}-4.2-plop-false
// b-3-true-{bar}-4.2-plop-false
// b-3-false-{bar}-4.2-plop-false
// b-4-true-{bar}-4.2-plop-false
// b-4-false-{bar}-4.2-plop-false
}
func ExampleCrossJoinBy8() {
result := CrossJoinBy8([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42}, func(a string, b int, c bool, d foo, e float64, f string, g bool, h int) string {
return fmt.Sprintf("%v-%v-%v-%v-%v-%v-%v-%v", a, b, c, d, e, f, g, h)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// a-1-true-{bar}-4.2-plop-false-42
// a-1-false-{bar}-4.2-plop-false-42
// a-2-true-{bar}-4.2-plop-false-42
// a-2-false-{bar}-4.2-plop-false-42
// a-3-true-{bar}-4.2-plop-false-42
// a-3-false-{bar}-4.2-plop-false-42
// a-4-true-{bar}-4.2-plop-false-42
// a-4-false-{bar}-4.2-plop-false-42
// b-1-true-{bar}-4.2-plop-false-42
// b-1-false-{bar}-4.2-plop-false-42
// b-2-true-{bar}-4.2-plop-false-42
// b-2-false-{bar}-4.2-plop-false-42
// b-3-true-{bar}-4.2-plop-false-42
// b-3-false-{bar}-4.2-plop-false-42
// b-4-true-{bar}-4.2-plop-false-42
// b-4-false-{bar}-4.2-plop-false-42
}
func ExampleCrossJoinBy9() {
result := CrossJoinBy9([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42}, []string{"hello world"}, func(a string, b int, c bool, d foo, e float64, f string, g bool, h int, i string) string {
return fmt.Sprintf("%v-%v-%v-%v-%v-%v-%v-%v-%v", a, b, c, d, e, f, g, h, i)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
// Output:
// a-1-true-{bar}-4.2-plop-false-42-hello world
// a-1-false-{bar}-4.2-plop-false-42-hello world
// a-2-true-{bar}-4.2-plop-false-42-hello world
// a-2-false-{bar}-4.2-plop-false-42-hello world
// a-3-true-{bar}-4.2-plop-false-42-hello world
// a-3-false-{bar}-4.2-plop-false-42-hello world
// a-4-true-{bar}-4.2-plop-false-42-hello world
// a-4-false-{bar}-4.2-plop-false-42-hello world
// b-1-true-{bar}-4.2-plop-false-42-hello world
// b-1-false-{bar}-4.2-plop-false-42-hello world
// b-2-true-{bar}-4.2-plop-false-42-hello world
// b-2-false-{bar}-4.2-plop-false-42-hello world
// b-3-true-{bar}-4.2-plop-false-42-hello world
// b-3-false-{bar}-4.2-plop-false-42-hello world
// b-4-true-{bar}-4.2-plop-false-42-hello world
// b-4-false-{bar}-4.2-plop-false-42-hello world
}