Files
lo/it/math_example_test.go
Nathan Baulch 43cef1f439 feat: new iter package (#672)
* lint: pin golangci-lint version

* lint: fix issues triggered by go1.23 upgrade

* feat: new iter package

* lint: fix linter issues

* fix: restore go1.18

* fix: rename package to "it"

* feat: assign multiple sequences of maps

* fix: panic in DropRight if n = 0

* docs: fix incorrect non-iter helper references

* feat: implement Invert helper

* feat: helpers for creating and checking empty sequences

* feat: implement Reverse helper

* feat: implement ReduceRight helper

* feat: implement Shuffle helper

* feat: implement Sample* helpers

* refactor: rename helpers with Seq convention

* feat: implement SeqToChannel2 helper

* feat: implement HasPrefix/HasSuffix helpers

* chore: port recent changes

* perf: only iterate collection once in Every

* refactor: reduce dupe code by reusing helpers internally

* perf: reuse internal Mode slice

* feat: implement Length helper

* chore: duplicate unit tests for *I helpers

* fix: omit duplicates in second Intersect list

* feat: intersect more than 2 sequences

* feat: implement Drain helper

* feat: implement Seq/Seq2 conversion helpers

* refactor: rename *Right* to *Last*

* chore: minor cleanup

* refactor: consistent predicate/transform parameter names

* perf: abort Slice/Subset once upper bound reached

* refactor: rename IsSortedByKey to IsSortedBy

* refactor: reuse more helpers internally

* feat: implement Cut* helpers

* feat: implement Trim* helpers

* perf: reduce allocations

* docs: describe iteration and allocation expectations

* Update .github/workflows/lint.yml

---------

Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
2025-10-02 19:23:16 +02:00

98 lines
1.7 KiB
Go

//go:build go1.23
package it
import (
"fmt"
"slices"
)
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", slices.Collect(result1))
fmt.Printf("%v\n", slices.Collect(result2))
fmt.Printf("%v\n", slices.Collect(result3))
fmt.Printf("%v\n", slices.Collect(result4))
fmt.Printf("%v\n", slices.Collect(result5))
fmt.Printf("%v\n", slices.Collect(result6))
fmt.Printf("%v\n", slices.Collect(result7))
fmt.Printf("%v\n", slices.Collect(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 ExampleSum() {
ints := slices.Values([]int{1, 2, 3, 4, 5})
sum := Sum(ints)
fmt.Printf("%v", sum)
// Output: 15
}
func ExampleSumBy() {
ints := slices.Values([]string{"foo", "bar"})
result := SumBy(ints, func(item string) int {
return len(item)
})
fmt.Printf("%v", result)
// Output: 6
}
func ExampleProduct() {
ints := slices.Values([]int{1, 2, 3, 4, 5})
result := Product(ints)
fmt.Printf("%v", result)
// Output: 120
}
func ExampleProductBy() {
strs := slices.Values([]string{"foo", "bar"})
result := ProductBy(strs, func(item string) int {
return len(item)
})
fmt.Printf("%v", result)
// Output: 9
}
func ExampleMean() {
ints := slices.Values([]int{1, 2, 3, 4, 5})
result := Mean(ints)
fmt.Printf("%v", result)
// Output: 3
}
func ExampleMeanBy() {
strs := slices.Values([]string{"foo", "bar"})
result := MeanBy(strs, func(item string) int {
return len(item)
})
fmt.Printf("%v", result)
// Output: 3
}