From b483094660fdc2f9474a70610c0c940d07d22a86 Mon Sep 17 00:00:00 2001 From: "kevin.zhao" Date: Fri, 10 Oct 2025 14:27:16 +0800 Subject: [PATCH 1/3] rename cmp->xcmp --- pkg/cmp/README.md | 244 ---------------------------------- pkg/xcmp/README.md | 194 +++++++++++++++++++++++++++ pkg/{cmp => xcmp}/cmd_old.go | 4 +- pkg/{cmp => xcmp}/cmp.go | 2 +- pkg/{cmp => xcmp}/cmp_test.go | 52 ++++---- pkg/xiter/README.md | 150 ++++++++++----------- pkg/xiter/xiter.go | 10 +- pkg/xiter/xiter_old.go | 9 +- 8 files changed, 307 insertions(+), 358 deletions(-) delete mode 100644 pkg/cmp/README.md create mode 100644 pkg/xcmp/README.md rename pkg/{cmp => xcmp}/cmd_old.go (96%) rename pkg/{cmp => xcmp}/cmp.go (95%) rename pkg/{cmp => xcmp}/cmp_test.go (77%) diff --git a/pkg/cmp/README.md b/pkg/cmp/README.md deleted file mode 100644 index 2b695c3..0000000 --- a/pkg/cmp/README.md +++ /dev/null @@ -1,244 +0,0 @@ - - -# cmp - -```go -import "github.com/dashjay/xiter/pkg/cmp" -``` - -## Index - -- [func Compare\[T Ordered\]\(x, y T\) int](<#Compare>) -- [func Less\[T Ordered\]\(x, y T\) bool](<#Less>) -- [func Or\[T comparable\]\(vals ...T\) T](<#Or>) -- [type Ordered](<#Ordered>) - - - -## func [Compare]() - -```go -func Compare[T Ordered](x, y T) int -``` - - - -
Example -

- - - -```go -package main - -import ( - "fmt" - "math" - - "github.com/dashjay/xiter/pkg/cmp" -) - -func main() { - fmt.Println(cmp.Compare(1, 2)) - fmt.Println(cmp.Compare("a", "aa")) - fmt.Println(cmp.Compare(1.5, 1.5)) - fmt.Println(cmp.Compare(math.NaN(), 1.0)) -} -``` - -#### Output - -``` --1 --1 -0 --1 -``` - -

-
- - -## func [Less]() - -```go -func Less[T Ordered](x, y T) bool -``` - - - -
Example -

- - - -```go -package main - -import ( - "fmt" - "math" - - "github.com/dashjay/xiter/pkg/cmp" -) - -func main() { - fmt.Println(cmp.Less(1, 2)) - fmt.Println(cmp.Less("a", "aa")) - fmt.Println(cmp.Less(1.0, math.NaN())) - fmt.Println(cmp.Less(math.NaN(), 1.0)) -} -``` - -#### Output - -``` -true -true -false -true -``` - -

-
- - -## func [Or]() - -```go -func Or[T comparable](vals ...T) T -``` - - - -
Example -

- - - -```go -package main - -import ( - "fmt" - - "github.com/dashjay/xiter/pkg/cmp" -) - -func main() { - // Suppose we have some user input - // that may or may not be an empty string - userInput1 := "" - userInput2 := "some text" - - fmt.Println(cmp.Or(userInput1, "default")) - fmt.Println(cmp.Or(userInput2, "default")) - fmt.Println(cmp.Or(userInput1, userInput2, "default")) -} -``` - -#### Output - -``` -default -some text -some text -``` - -

-
- -
Example (Sort) -

- - - -```go -package main - -import ( - "fmt" - "sort" - "strings" - - "github.com/dashjay/xiter/pkg/cmp" -) - -type Order struct { - Product string - Customer string - Price float64 -} - -type Orders []Order - -func (o Orders) Len() int { - return len(o) -} -func (o Orders) Less(i, j int) bool { - a, b := o[i], o[j] - if cmp.Or( - strings.Compare(a.Customer, b.Customer), - strings.Compare(a.Product, b.Product), - cmp.Compare(b.Price, a.Price)) < 0 { - return true - } else { - return false - } -} - -func (o Orders) Swap(i, j int) { - o[i], o[j] = o[j], o[i] -} - -func main() { - orders := []Order{ - {"foo", "alice", 1.00}, - {"bar", "bob", 3.00}, - {"baz", "carol", 4.00}, - {"foo", "alice", 2.00}, - {"bar", "carol", 1.00}, - {"foo", "bob", 4.00}, - } - //Sort by customer first, product second, and last by higher price - sort.Sort(Orders(orders)) - - // wait for the implement of slices.SortFunc - //SortFunc(orders, func(a, b Order) int { - // return cmp.Or( - // strings.Compare(a.Customer, b.Customer), - // strings.Compare(a.Product, b.Product), - // cmp.Compare(b.Price, a.Price), - // ) - //}) - for _, order := range orders { - fmt.Printf("%s %s %.2f\n", order.Product, order.Customer, order.Price) - } - -} -``` - -#### Output - -``` -foo alice 2.00 -foo alice 1.00 -bar bob 3.00 -foo bob 4.00 -bar carol 1.00 -baz carol 4.00 -``` - -

-
- - -## type [Ordered]() - - - -```go -type Ordered cmp.Ordered -``` - -Generated by [gomarkdoc]() diff --git a/pkg/xcmp/README.md b/pkg/xcmp/README.md new file mode 100644 index 0000000..3a5e99c --- /dev/null +++ b/pkg/xcmp/README.md @@ -0,0 +1,194 @@ + + +# xcmp + +```go +import "github.com/dashjay/xiter/pkg/xcmp" +``` + +## Index + +- [func Compare\[T Ordered\]\(x, y T\) int](<#Compare>) +- [func Less\[T Ordered\]\(x, y T\) bool](<#Less>) +- [func Or\[T comparable\]\(vals ...T\) T](<#Or>) +- [type Ordered](<#Ordered>) + + + +## func [Compare]() + +```go +func Compare[T Ordered](x, y T) int +``` + + + +
Example +

+ + + +```go +fmt.Println(xcmp.Compare(1, 2)) +fmt.Println(xcmp.Compare("a", "aa")) +fmt.Println(xcmp.Compare(1.5, 1.5)) +fmt.Println(xcmp.Compare(math.NaN(), 1.0)) +// Output: +// -1 +// -1 +// 0 +// -1 +``` + +#### Output + +``` +-1 +-1 +0 +-1 +``` + +

+
+ + +## func [Less]() + +```go +func Less[T Ordered](x, y T) bool +``` + + + +
Example +

+ + + +```go +fmt.Println(xcmp.Less(1, 2)) +fmt.Println(xcmp.Less("a", "aa")) +fmt.Println(xcmp.Less(1.0, math.NaN())) +fmt.Println(xcmp.Less(math.NaN(), 1.0)) +// Output: +// true +// true +// false +// true +``` + +#### Output + +``` +true +true +false +true +``` + +

+
+ + +## func [Or]() + +```go +func Or[T comparable](vals ...T) T +``` + + + +
Example +

+ + + +```go +// Suppose we have some user input +// that may or may not be an empty string +userInput1 := "" +userInput2 := "some text" + +fmt.Println(xcmp.Or(userInput1, "default")) +fmt.Println(xcmp.Or(userInput2, "default")) +fmt.Println(xcmp.Or(userInput1, userInput2, "default")) +// Output: +// default +// some text +// some text +``` + +#### Output + +``` +default +some text +some text +``` + +

+
+ +
Example (Sort) +

+ + + +```go +orders := []Order{ + {"foo", "alice", 1.00}, + {"bar", "bob", 3.00}, + {"baz", "carol", 4.00}, + {"foo", "alice", 2.00}, + {"bar", "carol", 1.00}, + {"foo", "bob", 4.00}, +} +//Sort by customer first, product second, and last by higher price +sort.Sort(Orders(orders)) + +// wait for the implement of slices.SortFunc +//SortFunc(orders, func(a, b Order) int { +// return cmp.Or( +// strings.Compare(a.Customer, b.Customer), +// strings.Compare(a.Product, b.Product), +// cmp.Compare(b.Price, a.Price), +// ) +//}) +for _, order := range orders { + fmt.Printf("%s %s %.2f\n", order.Product, order.Customer, order.Price) +} + +// Output: +// foo alice 2.00 +// foo alice 1.00 +// bar bob 3.00 +// foo bob 4.00 +// bar carol 1.00 +// baz carol 4.00 +``` + +#### Output + +``` +foo alice 2.00 +foo alice 1.00 +bar bob 3.00 +foo bob 4.00 +bar carol 1.00 +baz carol 4.00 +``` + +

+
+ + +## type [Ordered]() + + + +```go +type Ordered cmp.Ordered +``` + +Generated by [gomarkdoc]() diff --git a/pkg/cmp/cmd_old.go b/pkg/xcmp/cmd_old.go similarity index 96% rename from pkg/cmp/cmd_old.go rename to pkg/xcmp/cmd_old.go index 316f19c..b1285fb 100644 --- a/pkg/cmp/cmd_old.go +++ b/pkg/xcmp/cmd_old.go @@ -6,9 +6,9 @@ //go:build !go1.23 // +build !go1.23 -// Package cmp provides types and functions related to comparing +// Package xcmp provides types and functions related to comparing // ordered values. -package cmp +package xcmp // Ordered is a constraint that permits any ordered type: any type // that supports the operators < <= >= >. diff --git a/pkg/cmp/cmp.go b/pkg/xcmp/cmp.go similarity index 95% rename from pkg/cmp/cmp.go rename to pkg/xcmp/cmp.go index 31c1b86..5071060 100644 --- a/pkg/cmp/cmp.go +++ b/pkg/xcmp/cmp.go @@ -1,7 +1,7 @@ //go:build go1.23 // +build go1.23 -package cmp +package xcmp import "cmp" diff --git a/pkg/cmp/cmp_test.go b/pkg/xcmp/cmp_test.go similarity index 77% rename from pkg/cmp/cmp_test.go rename to pkg/xcmp/cmp_test.go index f075515..375767f 100644 --- a/pkg/cmp/cmp_test.go +++ b/pkg/xcmp/cmp_test.go @@ -3,7 +3,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package cmp_test +package xcmp_test import ( "fmt" @@ -13,7 +13,7 @@ import ( "testing" "unsafe" - "github.com/dashjay/xiter/pkg/cmp" + "github.com/dashjay/xiter/pkg/xcmp" ) var negzero = math.Copysign(0, -1) @@ -60,13 +60,13 @@ func TestLess(t *testing.T) { var b bool switch test.x.(type) { case int: - b = cmp.Less(test.x.(int), test.y.(int)) + b = xcmp.Less(test.x.(int), test.y.(int)) case string: - b = cmp.Less(test.x.(string), test.y.(string)) + b = xcmp.Less(test.x.(string), test.y.(string)) case float64: - b = cmp.Less(test.x.(float64), test.y.(float64)) + b = xcmp.Less(test.x.(float64), test.y.(float64)) case uintptr: - b = cmp.Less(test.x.(uintptr), test.y.(uintptr)) + b = xcmp.Less(test.x.(uintptr), test.y.(uintptr)) } if b != (test.compare < 0) { t.Errorf("Less(%v, %v) == %t, want %t", test.x, test.y, b, test.compare < 0) @@ -79,13 +79,13 @@ func TestCompare(t *testing.T) { var c int switch test.x.(type) { case int: - c = cmp.Compare(test.x.(int), test.y.(int)) + c = xcmp.Compare(test.x.(int), test.y.(int)) case string: - c = cmp.Compare(test.x.(string), test.y.(string)) + c = xcmp.Compare(test.x.(string), test.y.(string)) case float64: - c = cmp.Compare(test.x.(float64), test.y.(float64)) + c = xcmp.Compare(test.x.(float64), test.y.(float64)) case uintptr: - c = cmp.Compare(test.x.(uintptr), test.y.(uintptr)) + c = xcmp.Compare(test.x.(uintptr), test.y.(uintptr)) } if c != test.compare { t.Errorf("Compare(%v, %v) == %d, want %d", test.x, test.y, c, test.compare) @@ -99,10 +99,10 @@ func TestSort(t *testing.T) { input := []float64{1.0, 0.0, negzero, math.Inf(1), math.Inf(-1), math.NaN()} sort.Float64s(input) for i := 0; i < len(input)-1; i++ { - if cmp.Less(input[i+1], input[i]) { + if xcmp.Less(input[i+1], input[i]) { t.Errorf("Less sort mismatch at %d in %v", i, input) } - if cmp.Compare(input[i], input[i+1]) > 0 { + if xcmp.Compare(input[i], input[i+1]) > 0 { t.Errorf("Compare sort mismatch at %d in %v", i, input) } } @@ -122,7 +122,7 @@ func TestOr(t *testing.T) { {[]int{0, 6, 7}, 6}, } for _, tc := range cases { - if got := cmp.Or(tc.in...); got != tc.want { + if got := xcmp.Or(tc.in...); got != tc.want { t.Errorf("cmp.Or(%v) = %v; want %v", tc.in, got, tc.want) } } @@ -134,9 +134,9 @@ func ExampleOr() { userInput1 := "" userInput2 := "some text" - fmt.Println(cmp.Or(userInput1, "default")) - fmt.Println(cmp.Or(userInput2, "default")) - fmt.Println(cmp.Or(userInput1, userInput2, "default")) + fmt.Println(xcmp.Or(userInput1, "default")) + fmt.Println(xcmp.Or(userInput2, "default")) + fmt.Println(xcmp.Or(userInput1, userInput2, "default")) // Output: // default // some text @@ -156,10 +156,10 @@ func (o Orders) Len() int { } func (o Orders) Less(i, j int) bool { a, b := o[i], o[j] - if cmp.Or( + if xcmp.Or( strings.Compare(a.Customer, b.Customer), strings.Compare(a.Product, b.Product), - cmp.Compare(b.Price, a.Price)) < 0 { + xcmp.Compare(b.Price, a.Price)) < 0 { return true } else { return false @@ -204,10 +204,10 @@ func ExampleOr_sort() { } func ExampleLess() { - fmt.Println(cmp.Less(1, 2)) - fmt.Println(cmp.Less("a", "aa")) - fmt.Println(cmp.Less(1.0, math.NaN())) - fmt.Println(cmp.Less(math.NaN(), 1.0)) + fmt.Println(xcmp.Less(1, 2)) + fmt.Println(xcmp.Less("a", "aa")) + fmt.Println(xcmp.Less(1.0, math.NaN())) + fmt.Println(xcmp.Less(math.NaN(), 1.0)) // Output: // true // true @@ -216,10 +216,10 @@ func ExampleLess() { } func ExampleCompare() { - fmt.Println(cmp.Compare(1, 2)) - fmt.Println(cmp.Compare("a", "aa")) - fmt.Println(cmp.Compare(1.5, 1.5)) - fmt.Println(cmp.Compare(math.NaN(), 1.0)) + fmt.Println(xcmp.Compare(1, 2)) + fmt.Println(xcmp.Compare("a", "aa")) + fmt.Println(xcmp.Compare(1.5, 1.5)) + fmt.Println(xcmp.Compare(math.NaN(), 1.0)) // Output: // -1 // -1 diff --git a/pkg/xiter/README.md b/pkg/xiter/README.md index 3f7fc48..a12dd50 100644 --- a/pkg/xiter/README.md +++ b/pkg/xiter/README.md @@ -72,7 +72,7 @@ WARNING: golang 1.23 has higher performance on iterating Seq/Seq2 which boost by - [func Intersect\[T comparable\]\(left Seq\[T\], right Seq\[T\]\) Seq\[T\]](<#Intersect>) - [func Limit\[V any\]\(seq Seq\[V\], n int\) Seq\[V\]](<#Limit>) - [func Map\[In, Out any\]\(f func\(In\) Out, seq Seq\[In\]\) Seq\[Out\]](<#Map>) - - [func Merge\[V cmp.Ordered\]\(x, y Seq\[V\]\) Seq\[V\]](<#Merge>) + - [func Merge\[V xcmp.Ordered\]\(x, y Seq\[V\]\) Seq\[V\]](<#Merge>) - [func MergeFunc\[V any\]\(x, y Seq\[V\], f func\(V, V\) int\) Seq\[V\]](<#MergeFunc>) - [func Repeat\[T any\]\(seq Seq\[T\], count int\) Seq\[T\]](<#Repeat>) - [func Replace\[T comparable\]\(seq Seq\[T\], from, to T, n int\) Seq\[T\]](<#Replace>) @@ -95,14 +95,14 @@ WARNING: golang 1.23 has higher performance on iterating Seq/Seq2 which boost by - [func Map2\[KIn, VIn, KOut, VOut any\]\(f func\(KIn, VIn\) \(KOut, VOut\), seq Seq2\[KIn, VIn\]\) Seq2\[KOut, VOut\]](<#Map2>) - [func MapToSeq2\[T any, K comparable\]\(in Seq\[T\], mapFn func\(ele T\) K\) Seq2\[K, T\]](<#MapToSeq2>) - [func MapToSeq2Value\[T any, K comparable, V any\]\(in Seq\[T\], mapFn func\(ele T\) \(K, V\)\) Seq2\[K, V\]](<#MapToSeq2Value>) - - [func Merge2\[K cmp.Ordered, V any\]\(x, y Seq2\[K, V\]\) Seq2\[K, V\]](<#Merge2>) + - [func Merge2\[K xcmp.Ordered, V any\]\(x, y Seq2\[K, V\]\) Seq2\[K, V\]](<#Merge2>) - [func MergeFunc2\[K, V any\]\(x, y Seq2\[K, V\], f func\(K, K\) int\) Seq2\[K, V\]](<#MergeFunc2>) - [type Zipped](<#Zipped>) - [type Zipped2](<#Zipped2>) -## func [AllFromSeq]() +## func [AllFromSeq]() ```go func AllFromSeq[T any](seq Seq[T], f func(T) bool) bool @@ -111,7 +111,7 @@ func AllFromSeq[T any](seq Seq[T], f func(T) bool) bool AllFromSeq return true if all elements from seq satisfy the condition evaluated by f. -## func [AnyFromSeq]() +## func [AnyFromSeq]() ```go func AnyFromSeq[T any](seq Seq[T], f func(T) bool) bool @@ -129,7 +129,7 @@ func At[T any](seq Seq[T], index int) optional.O[T] At return the element at index from seq. -## func [AvgByFromSeq]() +## func [AvgByFromSeq]() ```go func AvgByFromSeq[V any, T constraints.Number](seq Seq[V], f func(V) T) float64 @@ -138,7 +138,7 @@ func AvgByFromSeq[V any, T constraints.Number](seq Seq[V], f func(V) T) float64 AvgByFromSeq return the average value of all elements from seq, evaluated by f. -## func [AvgFromSeq]() +## func [AvgFromSeq]() ```go func AvgFromSeq[T constraints.Number](seq Seq[T]) float64 @@ -147,7 +147,7 @@ func AvgFromSeq[T constraints.Number](seq Seq[T]) float64 AvgFromSeq return the average value of all elements from seq. -## func [Contains]() +## func [Contains]() ```go func Contains[T comparable](seq Seq[T], in T) bool @@ -156,7 +156,7 @@ func Contains[T comparable](seq Seq[T], in T) bool Contains return true if v is in seq. -## func [ContainsAll]() +## func [ContainsAll]() ```go func ContainsAll[T comparable](seq Seq[T], in []T) bool @@ -165,7 +165,7 @@ func ContainsAll[T comparable](seq Seq[T], in []T) bool ContainsAll return true if all elements from seq is in vs. -## func [ContainsAny]() +## func [ContainsAny]() ```go func ContainsAny[T comparable](seq Seq[T], in []T) bool @@ -174,7 +174,7 @@ func ContainsAny[T comparable](seq Seq[T], in []T) bool ContainsAny return true if any element from seq is in vs. -## func [ContainsBy]() +## func [ContainsBy]() ```go func ContainsBy[T any](seq Seq[T], f func(T) bool) bool @@ -183,7 +183,7 @@ func ContainsBy[T any](seq Seq[T], f func(T) bool) bool ContainsBy return true if any element from seq satisfies the condition evaluated by f. -## func [Count]() +## func [Count]() ```go func Count[T any](seq Seq[T]) int @@ -211,7 +211,7 @@ onlyLeft, onlyRight := Difference(FromSlice(left), FromSlice(right)) ``` -## func [Equal]() +## func [Equal]() ```go func Equal[V comparable](x, y Seq[V]) bool @@ -266,7 +266,7 @@ false -## func [Equal2]() +## func [Equal2]() ```go func Equal2[K, V comparable](x, y Seq2[K, V]) bool @@ -275,7 +275,7 @@ func Equal2[K, V comparable](x, y Seq2[K, V]) bool Equal2 returns whether the two Seq2 are equal. Like Equal but run with Seq2 -## func [EqualFunc]() +## func [EqualFunc]() ```go func EqualFunc[V1, V2 any](x Seq[V1], y Seq[V2], f func(V1, V2) bool) bool @@ -335,7 +335,7 @@ false -## func [EqualFunc2]() +## func [EqualFunc2]() ```go func EqualFunc2[K1, V1, K2, V2 any](x Seq2[K1, V1], y Seq2[K2, V2], f func(K1, V1, K2, V2) bool) bool @@ -344,7 +344,7 @@ func EqualFunc2[K1, V1, K2, V2 any](x Seq2[K1, V1], y Seq2[K2, V2], f func(K1, V EqualFunc2 returns whether the two sequences are equal according to the function f. Like EqualFunc but run with Seq2 -## func [Find]() +## func [Find]() ```go func Find[T any](seq Seq[T], f func(T) bool) (val T, found bool) @@ -353,7 +353,7 @@ func Find[T any](seq Seq[T], f func(T) bool) (val T, found bool) Find return the first element from seq that satisfies the condition evaluated by f with a boolean representing whether it exists. -## func [FindO]() +## func [FindO]() ```go func FindO[T any](seq Seq[T], f func(T) bool) optional.O[T] @@ -362,7 +362,7 @@ func FindO[T any](seq Seq[T], f func(T) bool) optional.O[T] FindO return the first element from seq that satisfies the condition evaluated by f. -## func [First]() +## func [First]() ```go func First[T any](in Seq[T]) (T, bool) @@ -377,7 +377,7 @@ first, ok := First(seq) ``` -## func [FirstO]() +## func [FirstO]() ```go func FirstO[T any](in Seq[T]) optional.O[T] @@ -392,7 +392,7 @@ first, ok := FirstO(seq) ``` -## func [ForEach]() +## func [ForEach]() ```go func ForEach[T any](seq Seq[T], f func(T) bool) @@ -401,7 +401,7 @@ func ForEach[T any](seq Seq[T], f func(T) bool) ForEach execute f for each element in seq. -## func [ForEachIdx]() +## func [ForEachIdx]() ```go func ForEachIdx[T any](seq Seq[T], f func(idx int, v T) bool) @@ -410,7 +410,7 @@ func ForEachIdx[T any](seq Seq[T], f func(idx int, v T) bool) ForEachIdx execute f for each element in seq with its index. -## func [Head]() +## func [Head]() ```go func Head[T any](seq Seq[T]) (v T, hasOne bool) @@ -419,7 +419,7 @@ func Head[T any](seq Seq[T]) (v T, hasOne bool) Head return the first element from seq with a boolean representing whether it is at least one element in seq. -## func [HeadO]() +## func [HeadO]() ```go func HeadO[T any](seq Seq[T]) optional.O[T] @@ -428,7 +428,7 @@ func HeadO[T any](seq Seq[T]) optional.O[T] HeadO return the first element from seq. -## func [Index]() +## func [Index]() ```go func Index[T comparable](seq Seq[T], v T) int @@ -445,7 +445,7 @@ idx := xiter.Index(seq, 3) ``` -## func [Join]() +## func [Join]() ```go func Join[T ~string](seq Seq[T], sep T) T @@ -454,7 +454,7 @@ func Join[T ~string](seq Seq[T], sep T) T Join return the concatenation of all elements in seq with sep. -## func [Last]() +## func [Last]() ```go func Last[T any](in Seq[T]) (T, bool) @@ -469,7 +469,7 @@ last, ok := Last(seq) ``` -## func [LastO]() +## func [LastO]() ```go func LastO[T any](in Seq[T]) optional.O[T] @@ -484,7 +484,7 @@ last, ok := LastO(seq) ``` -## func [Max]() +## func [Max]() ```go func Max[T constraints.Ordered](seq Seq[T]) (r optional.O[T]) @@ -493,7 +493,7 @@ func Max[T constraints.Ordered](seq Seq[T]) (r optional.O[T]) Max returns the maximum element in seq. -## func [MaxBy]() +## func [MaxBy]() ```go func MaxBy[T any](seq Seq[T], less func(T, T) bool) (r optional.O[T]) @@ -536,7 +536,7 @@ mean := MeanBy(FromSlice([]int{1, 2, 3, 4, 5}), func(v int) int { ``` -## func [Min]() +## func [Min]() ```go func Min[T constraints.Ordered](seq Seq[T]) (r optional.O[T]) @@ -545,7 +545,7 @@ func Min[T constraints.Ordered](seq Seq[T]) (r optional.O[T]) Min return the minimum element in seq. -## func [MinBy]() +## func [MinBy]() ```go func MinBy[T any](seq Seq[T], less func(T, T) bool) (r optional.O[T]) @@ -586,7 +586,7 @@ moderate := ModerateO(FromSlice([]int{1, 2, 3, 4, 5, 5, 5, 6, 6, 6, 6})) ``` -## func [Pull]() +## func [Pull]() ```go func Pull[V any](seq Seq[V]) (next func() (V, bool), stop func()) @@ -637,7 +637,7 @@ func main() { -## func [Pull2]() +## func [Pull2]() ```go func Pull2[K, V any](seq Seq2[K, V]) (next func() (K, V, bool), stop func()) @@ -646,7 +646,7 @@ func Pull2[K, V any](seq Seq2[K, V]) (next func() (K, V, bool), stop func()) -## func [Reduce]() +## func [Reduce]() ```go func Reduce[Sum, V any](f func(Sum, V) Sum, sum Sum, seq Seq[V]) Sum @@ -705,7 +705,7 @@ func main() { -## func [Reduce2]() +## func [Reduce2]() ```go func Reduce2[Sum, K, V any](f func(Sum, K, V) Sum, sum Sum, seq Seq2[K, V]) Sum @@ -714,7 +714,7 @@ func Reduce2[Sum, K, V any](f func(Sum, K, V) Sum, sum Sum, seq Seq2[K, V]) Sum Reduce2 combines the values in seq using f. For each pair k, v in seq, it updates sum = f\(sum, k, v\) and then returns the final sum. For example, if iterating over seq yields \(k1, v1\), \(k2, v2\), \(k3, v3\) Reduce returns f\(f\(f\(sum, k1, v1\), k2, v2\), k3, v3\). -## func [Sum]() +## func [Sum]() ```go func Sum[T constraints.Number](seq Seq[T]) T @@ -731,7 +731,7 @@ sum := xiter.Sum(seq) ``` -## func [ToMap]() +## func [ToMap]() ```go func ToMap[K comparable, V any](seq Seq2[K, V]) (out map[K]V) @@ -740,7 +740,7 @@ func ToMap[K comparable, V any](seq Seq2[K, V]) (out map[K]V) -## func [ToMapFromSeq]() +## func [ToMapFromSeq]() ```go func ToMapFromSeq[K comparable, V any](seq Seq[K], fn func(k K) V) (out map[K]V) @@ -749,7 +749,7 @@ func ToMapFromSeq[K comparable, V any](seq Seq[K], fn func(k K) V) (out map[K]V) -## func [ToSlice]() +## func [ToSlice]() ```go func ToSlice[T any](seq Seq[T]) (out []T) @@ -758,7 +758,7 @@ func ToSlice[T any](seq Seq[T]) (out []T) ToSlice returns the elements in seq as a slice. -## func [ToSliceN]() +## func [ToSliceN]() ```go func ToSliceN[T any](seq Seq[T], n int) (out []T) @@ -767,7 +767,7 @@ func ToSliceN[T any](seq Seq[T], n int) (out []T) ToSliceN pull out n elements from seq. -## func [ToSliceSeq2Key]() +## func [ToSliceSeq2Key]() ```go func ToSliceSeq2Key[K, V any](seq Seq2[K, V]) (out []K) @@ -784,7 +784,7 @@ keys := ToSliceSeq2Key(seq) ``` -## func [ToSliceSeq2Value]() +## func [ToSliceSeq2Value]() ```go func ToSliceSeq2Value[K, V any](seq Seq2[K, V]) (out []V) @@ -801,7 +801,7 @@ values := ToSliceSeq2Value(seq) ``` -## type [Seq]() +## type [Seq]() Seq is a sequence of elements provided by an iterator\-like function. We made this alias Seq to iter.Seq for providing a compatible interface in lower go versions. @@ -810,7 +810,7 @@ type Seq[V any] iter.Seq[V] ``` -### func [Chunk]() +### func [Chunk]() ```go func Chunk[T any](seq Seq[T], n int) Seq[[]T] @@ -827,7 +827,7 @@ chunkedSeq := xiter.Chunk(seq, 2) ``` -### func [Compact]() +### func [Compact]() ```go func Compact[T comparable](in Seq[T]) Seq[T] @@ -842,7 +842,7 @@ Compact([]int{0, 1, 2, 3, 4}) 👉 [1 2 3 4] ``` -### func [Concat]() +### func [Concat]() ```go func Concat[V any](seqs ...Seq[V]) Seq[V] @@ -895,7 +895,7 @@ func main() { -### func [Filter]() +### func [Filter]() ```go func Filter[V any](f func(V) bool, seq Seq[V]) Seq[V] @@ -965,7 +965,7 @@ _ = ToSlice(seq) // Returns []int{1, 2} ``` -### func [FromMapKeys]() +### func [FromMapKeys]() ```go func FromMapKeys[K comparable, V any](m map[K]V) Seq[K] @@ -974,7 +974,7 @@ func FromMapKeys[K comparable, V any](m map[K]V) Seq[K] -### func [FromMapValues]() +### func [FromMapValues]() ```go func FromMapValues[K comparable, V any](m map[K]V) Seq[V] @@ -1001,7 +1001,7 @@ func FromSliceReverse[T any, Slice ~[]T](in Slice) Seq[T] -### func [FromSliceShuffle]() +### func [FromSliceShuffle]() ```go func FromSliceShuffle[T any](in []T) Seq[T] @@ -1036,7 +1036,7 @@ intersect := Intersect(FromSlice(left), FromSlice(right)) ``` -### func [Limit]() +### func [Limit]() ```go func Limit[V any](seq Seq[V], n int) Seq[V] @@ -1085,7 +1085,7 @@ func main() { -### func [Map]() +### func [Map]() ```go func Map[In, Out any](f func(In) Out, seq Seq[In]) Seq[Out] @@ -1134,10 +1134,10 @@ func main() { -### func [Merge]() +### func [Merge]() ```go -func Merge[V cmp.Ordered](x, y Seq[V]) Seq[V] +func Merge[V xcmp.Ordered](x, y Seq[V]) Seq[V] ``` Merge merges two sequences of ordered values. Values appear in the output once for each time they appear in x and once for each time they appear in y. If the two input sequences are not ordered, the output sequence will not be ordered, but it will still contain every value from x and y exactly once. @@ -1176,7 +1176,7 @@ func main() { -### func [MergeFunc]() +### func [MergeFunc]() ```go func MergeFunc[V any](x, y Seq[V], f func(V, V) int) Seq[V] @@ -1194,7 +1194,7 @@ func Repeat[T any](seq Seq[T], count int) Seq[T] Repeat return a seq that repeat seq for count times. -### func [Replace]() +### func [Replace]() ```go func Replace[T comparable](seq Seq[T], from, to T, n int) Seq[T] @@ -1211,7 +1211,7 @@ replacedSeq := Replace(seq, 2, 99, -1) // Replace all 2s with 99 ``` -### func [ReplaceAll]() +### func [ReplaceAll]() ```go func ReplaceAll[T comparable](seq Seq[T], from, to T) Seq[T] @@ -1237,7 +1237,7 @@ func Reverse[T any](seq Seq[T]) Seq[T] Reverse return a reversed seq. -### func [Seq2KeyToSeq]() +### func [Seq2KeyToSeq]() ```go func Seq2KeyToSeq[K, V any](in Seq2[K, V]) Seq[K] @@ -1246,7 +1246,7 @@ func Seq2KeyToSeq[K, V any](in Seq2[K, V]) Seq[K] Seq2KeyToSeq return a seq that only contain keys in seq2. -### func [Seq2ToSeqUnion]() +### func [Seq2ToSeqUnion]() ```go func Seq2ToSeqUnion[K, V any](seq Seq2[K, V]) Seq[union.U2[K, V]] @@ -1264,7 +1264,7 @@ for v := range Seq2ToSeqUnion(seq2) { ``` -### func [Seq2ValueToSeq]() +### func [Seq2ValueToSeq]() ```go func Seq2ValueToSeq[K, V any](in Seq2[K, V]) Seq[V] @@ -1273,7 +1273,7 @@ func Seq2ValueToSeq[K, V any](in Seq2[K, V]) Seq[V] Seq2ValueToSeq return a seq that only contain values in seq2. -### func [Skip]() +### func [Skip]() ```go func Skip[T any](seq Seq[T], n int) Seq[T] @@ -1300,7 +1300,7 @@ union := Union(FromSlice(left), FromSlice(right)) ``` -### func [Uniq]() +### func [Uniq]() ```go func Uniq[T comparable](seq Seq[T]) Seq[T] @@ -1317,7 +1317,7 @@ uniqSeq := xiter.Uniq(seq) ``` -### func [Zip]() +### func [Zip]() ```go func Zip[V1, V2 any](x Seq[V1], y Seq[V2]) Seq[Zipped[V1, V2]] @@ -1375,7 +1375,7 @@ func main() { -### func [Zip2]() +### func [Zip2]() ```go func Zip2[K1, V1, K2, V2 any](x Seq2[K1, V1], y Seq2[K2, V2]) Seq[Zipped2[K1, V1, K2, V2]] @@ -1397,7 +1397,7 @@ func Equal2[K, V comparable](x, y Seq2[K, V]) bool { ``` -## type [Seq2]() +## type [Seq2]() Seq2 is a sequence of key/value pair provided by an iterator\-like function. We made this alias Seq2 to iter.Seq2 for providing a compatible interface in lower go versions. @@ -1406,7 +1406,7 @@ type Seq2[K, V any] iter.Seq2[K, V] ``` -### func [Concat2]() +### func [Concat2]() ```go func Concat2[K, V any](seqs ...Seq2[K, V]) Seq2[K, V] @@ -1415,7 +1415,7 @@ func Concat2[K, V any](seqs ...Seq2[K, V]) Seq2[K, V] Concat2 returns an Seq2 over the concatenation of the given Seq2s. Like Concat but run with Seq2 -### func [Filter2]() +### func [Filter2]() ```go func Filter2[K, V any](f func(K, V) bool, seq Seq2[K, V]) Seq2[K, V] @@ -1424,7 +1424,7 @@ func Filter2[K, V any](f func(K, V) bool, seq Seq2[K, V]) Seq2[K, V] Filter2 returns an Seq over seq that only includes the key\-value pairs k, v for which f\(k, v\) is true. Like Filter but run with Seq2 -### func [FromMapKeyAndValues]() +### func [FromMapKeyAndValues]() ```go func FromMapKeyAndValues[K comparable, V any](m map[K]V) Seq2[K, V] @@ -1442,7 +1442,7 @@ func FromSliceIdx[T any](in []T) Seq2[int, T] FromSliceIdx received a slice and returned a Seq2 for this slice, key is index. -### func [Limit2]() +### func [Limit2]() ```go func Limit2[K, V any](seq Seq2[K, V], n int) Seq2[K, V] @@ -1451,7 +1451,7 @@ func Limit2[K, V any](seq Seq2[K, V], n int) Seq2[K, V] Limit2 returns a Seq over Seq2 that stops after n key\-value pairs. Like Limit but run with Seq2 -### func [Map2]() +### func [Map2]() ```go func Map2[KIn, VIn, KOut, VOut any](f func(KIn, VIn) (KOut, VOut), seq Seq2[KIn, VIn]) Seq2[KOut, VOut] @@ -1460,7 +1460,7 @@ func Map2[KIn, VIn, KOut, VOut any](f func(KIn, VIn) (KOut, VOut), seq Seq2[KIn, Map2 returns a Seq2 over the results of applying f to each key\-value pair in seq. Like Map but run with Seq2 -### func [MapToSeq2]() +### func [MapToSeq2]() ```go func MapToSeq2[T any, K comparable](in Seq[T], mapFn func(ele T) K) Seq2[K, T] @@ -1481,7 +1481,7 @@ fmt.Println(ToMap(lenMap)) ``` -### func [MapToSeq2Value]() +### func [MapToSeq2Value]() ```go func MapToSeq2Value[T any, K comparable, V any](in Seq[T], mapFn func(ele T) (K, V)) Seq2[K, V] @@ -1501,10 +1501,10 @@ fmt.Println(ToMap(transformed)) ``` -### func [Merge2]() +### func [Merge2]() ```go -func Merge2[K cmp.Ordered, V any](x, y Seq2[K, V]) Seq2[K, V] +func Merge2[K xcmp.Ordered, V any](x, y Seq2[K, V]) Seq2[K, V] ``` Merge2 merges two sequences of key\-value pairs ordered by their keys. Pairs appear in the output once for each time they appear in x and once for each time they appear in y. If the two input sequences are not ordered by their keys, the output sequence will not be ordered by its keys, but it will still contain every pair from x and y exactly once. @@ -1512,7 +1512,7 @@ Merge2 merges two sequences of key\-value pairs ordered by their keys. Pairs app Merge2 is equivalent to calling MergeFunc2 with cmp.Compare\[K\] as the ordering function. -### func [MergeFunc2]() +### func [MergeFunc2]() ```go func MergeFunc2[K, V any](x, y Seq2[K, V], f func(K, K) int) Seq2[K, V] diff --git a/pkg/xiter/xiter.go b/pkg/xiter/xiter.go index f3b2708..bf1f654 100644 --- a/pkg/xiter/xiter.go +++ b/pkg/xiter/xiter.go @@ -9,11 +9,11 @@ import ( "math/rand" "strings" - "github.com/dashjay/xiter/pkg/cmp" "github.com/dashjay/xiter/pkg/internal/constraints" "github.com/dashjay/xiter/pkg/internal/utils" "github.com/dashjay/xiter/pkg/optional" "github.com/dashjay/xiter/pkg/union" + "github.com/dashjay/xiter/pkg/xcmp" ) // Seq is a sequence of elements provided by an iterator-like function. @@ -250,8 +250,8 @@ func Map2[KIn, VIn, KOut, VOut any]( // // Merge is equivalent to calling MergeFunc with cmp.Compare[V] // as the ordering function. -func Merge[V cmp.Ordered](x, y Seq[V]) Seq[V] { - return MergeFunc(x, y, cmp.Compare[V]) +func Merge[V xcmp.Ordered](x, y Seq[V]) Seq[V] { + return MergeFunc(x, y, xcmp.Compare[V]) } // MergeFunc merges two sequences of values ordered by the function f. @@ -296,8 +296,8 @@ func MergeFunc[V any](x, y Seq[V], f func(V, V) int) Seq[V] { // // Merge2 is equivalent to calling MergeFunc2 with cmp.Compare[K] // as the ordering function. -func Merge2[K cmp.Ordered, V any](x, y Seq2[K, V]) Seq2[K, V] { - return MergeFunc2(x, y, cmp.Compare[K]) +func Merge2[K xcmp.Ordered, V any](x, y Seq2[K, V]) Seq2[K, V] { + return MergeFunc2(x, y, xcmp.Compare[K]) } // MergeFunc2 merges two sequences of key-value pairs ordered by the function f. diff --git a/pkg/xiter/xiter_old.go b/pkg/xiter/xiter_old.go index caa79c9..e0491d1 100644 --- a/pkg/xiter/xiter_old.go +++ b/pkg/xiter/xiter_old.go @@ -9,7 +9,6 @@ import ( "strconv" "strings" - "github.com/dashjay/xiter/pkg/cmp" "github.com/dashjay/xiter/pkg/internal/constraints" "github.com/dashjay/xiter/pkg/internal/utils" "github.com/dashjay/xiter/pkg/optional" @@ -177,8 +176,8 @@ func Map2[KIn, VIn, KOut, VOut any](f func(KIn, VIn) (KOut, VOut), seq Seq2[KIn, } } -func Merge[V cmp.Ordered](x, y Seq[V]) Seq[V] { - return MergeFunc(x, y, cmp.Compare[V]) +func Merge[V xcmp.Ordered](x, y Seq[V]) Seq[V] { + return MergeFunc(x, y, xcmp.Compare[V]) } func MergeFunc[V any](x, y Seq[V], f func(V, V) int) Seq[V] { @@ -207,8 +206,8 @@ func MergeFunc[V any](x, y Seq[V], f func(V, V) int) Seq[V] { } } -func Merge2[K cmp.Ordered, V any](x, y Seq2[K, V]) Seq2[K, V] { - return MergeFunc2(x, y, cmp.Compare[K]) +func Merge2[K xcmp.Ordered, V any](x, y Seq2[K, V]) Seq2[K, V] { + return MergeFunc2(x, y, xcmp.Compare[K]) } func MergeFunc2[K, V any](x, y Seq2[K, V], f func(K, K) int) Seq2[K, V] { From 660e5f71a32c4ebd4e526ce78d7d8498cb55264c Mon Sep 17 00:00:00 2001 From: "kevin.zhao" Date: Fri, 10 Oct 2025 14:28:02 +0800 Subject: [PATCH 2/3] rename --- pkg/xcmp/README.md | 170 ++++++++++++++++--------- pkg/xcmp/{cmd_old.go => xcmd_old.go} | 0 pkg/xcmp/{cmp.go => xcmp.go} | 0 pkg/xcmp/{cmp_test.go => xcmp_test.go} | 0 pkg/xiter/README.md | 142 ++++++++++----------- 5 files changed, 181 insertions(+), 131 deletions(-) rename pkg/xcmp/{cmd_old.go => xcmd_old.go} (100%) rename pkg/xcmp/{cmp.go => xcmp.go} (100%) rename pkg/xcmp/{cmp_test.go => xcmp_test.go} (100%) diff --git a/pkg/xcmp/README.md b/pkg/xcmp/README.md index 3a5e99c..72db7a6 100644 --- a/pkg/xcmp/README.md +++ b/pkg/xcmp/README.md @@ -15,7 +15,7 @@ import "github.com/dashjay/xiter/pkg/xcmp" -## func [Compare]() +## func [Compare]() ```go func Compare[T Ordered](x, y T) int @@ -29,15 +29,21 @@ func Compare[T Ordered](x, y T) int ```go -fmt.Println(xcmp.Compare(1, 2)) -fmt.Println(xcmp.Compare("a", "aa")) -fmt.Println(xcmp.Compare(1.5, 1.5)) -fmt.Println(xcmp.Compare(math.NaN(), 1.0)) -// Output: -// -1 -// -1 -// 0 -// -1 +package main + +import ( + "fmt" + "math" + + "github.com/dashjay/xiter/pkg/xcmp" +) + +func main() { + fmt.Println(xcmp.Compare(1, 2)) + fmt.Println(xcmp.Compare("a", "aa")) + fmt.Println(xcmp.Compare(1.5, 1.5)) + fmt.Println(xcmp.Compare(math.NaN(), 1.0)) +} ``` #### Output @@ -53,7 +59,7 @@ fmt.Println(xcmp.Compare(math.NaN(), 1.0)) -## func [Less]() +## func [Less]() ```go func Less[T Ordered](x, y T) bool @@ -67,15 +73,21 @@ func Less[T Ordered](x, y T) bool ```go -fmt.Println(xcmp.Less(1, 2)) -fmt.Println(xcmp.Less("a", "aa")) -fmt.Println(xcmp.Less(1.0, math.NaN())) -fmt.Println(xcmp.Less(math.NaN(), 1.0)) -// Output: -// true -// true -// false -// true +package main + +import ( + "fmt" + "math" + + "github.com/dashjay/xiter/pkg/xcmp" +) + +func main() { + fmt.Println(xcmp.Less(1, 2)) + fmt.Println(xcmp.Less("a", "aa")) + fmt.Println(xcmp.Less(1.0, math.NaN())) + fmt.Println(xcmp.Less(math.NaN(), 1.0)) +} ``` #### Output @@ -91,7 +103,7 @@ true -## func [Or]() +## func [Or]() ```go func Or[T comparable](vals ...T) T @@ -105,18 +117,24 @@ func Or[T comparable](vals ...T) T ```go -// Suppose we have some user input -// that may or may not be an empty string -userInput1 := "" -userInput2 := "some text" +package main + +import ( + "fmt" + + "github.com/dashjay/xiter/pkg/xcmp" +) + +func main() { + // Suppose we have some user input + // that may or may not be an empty string + userInput1 := "" + userInput2 := "some text" -fmt.Println(xcmp.Or(userInput1, "default")) -fmt.Println(xcmp.Or(userInput2, "default")) -fmt.Println(xcmp.Or(userInput1, userInput2, "default")) -// Output: -// default -// some text -// some text + fmt.Println(xcmp.Or(userInput1, "default")) + fmt.Println(xcmp.Or(userInput2, "default")) + fmt.Println(xcmp.Or(userInput1, userInput2, "default")) +} ``` #### Output @@ -136,36 +154,68 @@ some text ```go -orders := []Order{ - {"foo", "alice", 1.00}, - {"bar", "bob", 3.00}, - {"baz", "carol", 4.00}, - {"foo", "alice", 2.00}, - {"bar", "carol", 1.00}, - {"foo", "bob", 4.00}, +package main + +import ( + "fmt" + "sort" + "strings" + + "github.com/dashjay/xiter/pkg/xcmp" +) + +type Order struct { + Product string + Customer string + Price float64 } -//Sort by customer first, product second, and last by higher price -sort.Sort(Orders(orders)) - -// wait for the implement of slices.SortFunc -//SortFunc(orders, func(a, b Order) int { -// return cmp.Or( -// strings.Compare(a.Customer, b.Customer), -// strings.Compare(a.Product, b.Product), -// cmp.Compare(b.Price, a.Price), -// ) -//}) -for _, order := range orders { - fmt.Printf("%s %s %.2f\n", order.Product, order.Customer, order.Price) + +type Orders []Order + +func (o Orders) Len() int { + return len(o) +} +func (o Orders) Less(i, j int) bool { + a, b := o[i], o[j] + if xcmp.Or( + strings.Compare(a.Customer, b.Customer), + strings.Compare(a.Product, b.Product), + xcmp.Compare(b.Price, a.Price)) < 0 { + return true + } else { + return false + } +} + +func (o Orders) Swap(i, j int) { + o[i], o[j] = o[j], o[i] } -// Output: -// foo alice 2.00 -// foo alice 1.00 -// bar bob 3.00 -// foo bob 4.00 -// bar carol 1.00 -// baz carol 4.00 +func main() { + orders := []Order{ + {"foo", "alice", 1.00}, + {"bar", "bob", 3.00}, + {"baz", "carol", 4.00}, + {"foo", "alice", 2.00}, + {"bar", "carol", 1.00}, + {"foo", "bob", 4.00}, + } + //Sort by customer first, product second, and last by higher price + sort.Sort(Orders(orders)) + + // wait for the implement of slices.SortFunc + //SortFunc(orders, func(a, b Order) int { + // return cmp.Or( + // strings.Compare(a.Customer, b.Customer), + // strings.Compare(a.Product, b.Product), + // cmp.Compare(b.Price, a.Price), + // ) + //}) + for _, order := range orders { + fmt.Printf("%s %s %.2f\n", order.Product, order.Customer, order.Price) + } + +} ``` #### Output @@ -183,7 +233,7 @@ baz carol 4.00 -## type [Ordered]() +## type [Ordered]() diff --git a/pkg/xcmp/cmd_old.go b/pkg/xcmp/xcmd_old.go similarity index 100% rename from pkg/xcmp/cmd_old.go rename to pkg/xcmp/xcmd_old.go diff --git a/pkg/xcmp/cmp.go b/pkg/xcmp/xcmp.go similarity index 100% rename from pkg/xcmp/cmp.go rename to pkg/xcmp/xcmp.go diff --git a/pkg/xcmp/cmp_test.go b/pkg/xcmp/xcmp_test.go similarity index 100% rename from pkg/xcmp/cmp_test.go rename to pkg/xcmp/xcmp_test.go diff --git a/pkg/xiter/README.md b/pkg/xiter/README.md index a12dd50..b28bf97 100644 --- a/pkg/xiter/README.md +++ b/pkg/xiter/README.md @@ -102,7 +102,7 @@ WARNING: golang 1.23 has higher performance on iterating Seq/Seq2 which boost by -## func [AllFromSeq]() +## func [AllFromSeq]() ```go func AllFromSeq[T any](seq Seq[T], f func(T) bool) bool @@ -111,7 +111,7 @@ func AllFromSeq[T any](seq Seq[T], f func(T) bool) bool AllFromSeq return true if all elements from seq satisfy the condition evaluated by f. -## func [AnyFromSeq]() +## func [AnyFromSeq]() ```go func AnyFromSeq[T any](seq Seq[T], f func(T) bool) bool @@ -129,7 +129,7 @@ func At[T any](seq Seq[T], index int) optional.O[T] At return the element at index from seq. -## func [AvgByFromSeq]() +## func [AvgByFromSeq]() ```go func AvgByFromSeq[V any, T constraints.Number](seq Seq[V], f func(V) T) float64 @@ -138,7 +138,7 @@ func AvgByFromSeq[V any, T constraints.Number](seq Seq[V], f func(V) T) float64 AvgByFromSeq return the average value of all elements from seq, evaluated by f. -## func [AvgFromSeq]() +## func [AvgFromSeq]() ```go func AvgFromSeq[T constraints.Number](seq Seq[T]) float64 @@ -147,7 +147,7 @@ func AvgFromSeq[T constraints.Number](seq Seq[T]) float64 AvgFromSeq return the average value of all elements from seq. -## func [Contains]() +## func [Contains]() ```go func Contains[T comparable](seq Seq[T], in T) bool @@ -156,7 +156,7 @@ func Contains[T comparable](seq Seq[T], in T) bool Contains return true if v is in seq. -## func [ContainsAll]() +## func [ContainsAll]() ```go func ContainsAll[T comparable](seq Seq[T], in []T) bool @@ -165,7 +165,7 @@ func ContainsAll[T comparable](seq Seq[T], in []T) bool ContainsAll return true if all elements from seq is in vs. -## func [ContainsAny]() +## func [ContainsAny]() ```go func ContainsAny[T comparable](seq Seq[T], in []T) bool @@ -174,7 +174,7 @@ func ContainsAny[T comparable](seq Seq[T], in []T) bool ContainsAny return true if any element from seq is in vs. -## func [ContainsBy]() +## func [ContainsBy]() ```go func ContainsBy[T any](seq Seq[T], f func(T) bool) bool @@ -183,7 +183,7 @@ func ContainsBy[T any](seq Seq[T], f func(T) bool) bool ContainsBy return true if any element from seq satisfies the condition evaluated by f. -## func [Count]() +## func [Count]() ```go func Count[T any](seq Seq[T]) int @@ -211,7 +211,7 @@ onlyLeft, onlyRight := Difference(FromSlice(left), FromSlice(right)) ``` -## func [Equal]() +## func [Equal]() ```go func Equal[V comparable](x, y Seq[V]) bool @@ -266,7 +266,7 @@ false -## func [Equal2]() +## func [Equal2]() ```go func Equal2[K, V comparable](x, y Seq2[K, V]) bool @@ -275,7 +275,7 @@ func Equal2[K, V comparable](x, y Seq2[K, V]) bool Equal2 returns whether the two Seq2 are equal. Like Equal but run with Seq2 -## func [EqualFunc]() +## func [EqualFunc]() ```go func EqualFunc[V1, V2 any](x Seq[V1], y Seq[V2], f func(V1, V2) bool) bool @@ -335,7 +335,7 @@ false -## func [EqualFunc2]() +## func [EqualFunc2]() ```go func EqualFunc2[K1, V1, K2, V2 any](x Seq2[K1, V1], y Seq2[K2, V2], f func(K1, V1, K2, V2) bool) bool @@ -344,7 +344,7 @@ func EqualFunc2[K1, V1, K2, V2 any](x Seq2[K1, V1], y Seq2[K2, V2], f func(K1, V EqualFunc2 returns whether the two sequences are equal according to the function f. Like EqualFunc but run with Seq2 -## func [Find]() +## func [Find]() ```go func Find[T any](seq Seq[T], f func(T) bool) (val T, found bool) @@ -353,7 +353,7 @@ func Find[T any](seq Seq[T], f func(T) bool) (val T, found bool) Find return the first element from seq that satisfies the condition evaluated by f with a boolean representing whether it exists. -## func [FindO]() +## func [FindO]() ```go func FindO[T any](seq Seq[T], f func(T) bool) optional.O[T] @@ -362,7 +362,7 @@ func FindO[T any](seq Seq[T], f func(T) bool) optional.O[T] FindO return the first element from seq that satisfies the condition evaluated by f. -## func [First]() +## func [First]() ```go func First[T any](in Seq[T]) (T, bool) @@ -377,7 +377,7 @@ first, ok := First(seq) ``` -## func [FirstO]() +## func [FirstO]() ```go func FirstO[T any](in Seq[T]) optional.O[T] @@ -392,7 +392,7 @@ first, ok := FirstO(seq) ``` -## func [ForEach]() +## func [ForEach]() ```go func ForEach[T any](seq Seq[T], f func(T) bool) @@ -401,7 +401,7 @@ func ForEach[T any](seq Seq[T], f func(T) bool) ForEach execute f for each element in seq. -## func [ForEachIdx]() +## func [ForEachIdx]() ```go func ForEachIdx[T any](seq Seq[T], f func(idx int, v T) bool) @@ -410,7 +410,7 @@ func ForEachIdx[T any](seq Seq[T], f func(idx int, v T) bool) ForEachIdx execute f for each element in seq with its index. -## func [Head]() +## func [Head]() ```go func Head[T any](seq Seq[T]) (v T, hasOne bool) @@ -419,7 +419,7 @@ func Head[T any](seq Seq[T]) (v T, hasOne bool) Head return the first element from seq with a boolean representing whether it is at least one element in seq. -## func [HeadO]() +## func [HeadO]() ```go func HeadO[T any](seq Seq[T]) optional.O[T] @@ -428,7 +428,7 @@ func HeadO[T any](seq Seq[T]) optional.O[T] HeadO return the first element from seq. -## func [Index]() +## func [Index]() ```go func Index[T comparable](seq Seq[T], v T) int @@ -445,7 +445,7 @@ idx := xiter.Index(seq, 3) ``` -## func [Join]() +## func [Join]() ```go func Join[T ~string](seq Seq[T], sep T) T @@ -454,7 +454,7 @@ func Join[T ~string](seq Seq[T], sep T) T Join return the concatenation of all elements in seq with sep. -## func [Last]() +## func [Last]() ```go func Last[T any](in Seq[T]) (T, bool) @@ -469,7 +469,7 @@ last, ok := Last(seq) ``` -## func [LastO]() +## func [LastO]() ```go func LastO[T any](in Seq[T]) optional.O[T] @@ -484,7 +484,7 @@ last, ok := LastO(seq) ``` -## func [Max]() +## func [Max]() ```go func Max[T constraints.Ordered](seq Seq[T]) (r optional.O[T]) @@ -493,7 +493,7 @@ func Max[T constraints.Ordered](seq Seq[T]) (r optional.O[T]) Max returns the maximum element in seq. -## func [MaxBy]() +## func [MaxBy]() ```go func MaxBy[T any](seq Seq[T], less func(T, T) bool) (r optional.O[T]) @@ -536,7 +536,7 @@ mean := MeanBy(FromSlice([]int{1, 2, 3, 4, 5}), func(v int) int { ``` -## func [Min]() +## func [Min]() ```go func Min[T constraints.Ordered](seq Seq[T]) (r optional.O[T]) @@ -545,7 +545,7 @@ func Min[T constraints.Ordered](seq Seq[T]) (r optional.O[T]) Min return the minimum element in seq. -## func [MinBy]() +## func [MinBy]() ```go func MinBy[T any](seq Seq[T], less func(T, T) bool) (r optional.O[T]) @@ -586,7 +586,7 @@ moderate := ModerateO(FromSlice([]int{1, 2, 3, 4, 5, 5, 5, 6, 6, 6, 6})) ``` -## func [Pull]() +## func [Pull]() ```go func Pull[V any](seq Seq[V]) (next func() (V, bool), stop func()) @@ -637,7 +637,7 @@ func main() { -## func [Pull2]() +## func [Pull2]() ```go func Pull2[K, V any](seq Seq2[K, V]) (next func() (K, V, bool), stop func()) @@ -646,7 +646,7 @@ func Pull2[K, V any](seq Seq2[K, V]) (next func() (K, V, bool), stop func()) -## func [Reduce]() +## func [Reduce]() ```go func Reduce[Sum, V any](f func(Sum, V) Sum, sum Sum, seq Seq[V]) Sum @@ -705,7 +705,7 @@ func main() { -## func [Reduce2]() +## func [Reduce2]() ```go func Reduce2[Sum, K, V any](f func(Sum, K, V) Sum, sum Sum, seq Seq2[K, V]) Sum @@ -714,7 +714,7 @@ func Reduce2[Sum, K, V any](f func(Sum, K, V) Sum, sum Sum, seq Seq2[K, V]) Sum Reduce2 combines the values in seq using f. For each pair k, v in seq, it updates sum = f\(sum, k, v\) and then returns the final sum. For example, if iterating over seq yields \(k1, v1\), \(k2, v2\), \(k3, v3\) Reduce returns f\(f\(f\(sum, k1, v1\), k2, v2\), k3, v3\). -## func [Sum]() +## func [Sum]() ```go func Sum[T constraints.Number](seq Seq[T]) T @@ -731,7 +731,7 @@ sum := xiter.Sum(seq) ``` -## func [ToMap]() +## func [ToMap]() ```go func ToMap[K comparable, V any](seq Seq2[K, V]) (out map[K]V) @@ -740,7 +740,7 @@ func ToMap[K comparable, V any](seq Seq2[K, V]) (out map[K]V) -## func [ToMapFromSeq]() +## func [ToMapFromSeq]() ```go func ToMapFromSeq[K comparable, V any](seq Seq[K], fn func(k K) V) (out map[K]V) @@ -749,7 +749,7 @@ func ToMapFromSeq[K comparable, V any](seq Seq[K], fn func(k K) V) (out map[K]V) -## func [ToSlice]() +## func [ToSlice]() ```go func ToSlice[T any](seq Seq[T]) (out []T) @@ -758,7 +758,7 @@ func ToSlice[T any](seq Seq[T]) (out []T) ToSlice returns the elements in seq as a slice. -## func [ToSliceN]() +## func [ToSliceN]() ```go func ToSliceN[T any](seq Seq[T], n int) (out []T) @@ -767,7 +767,7 @@ func ToSliceN[T any](seq Seq[T], n int) (out []T) ToSliceN pull out n elements from seq. -## func [ToSliceSeq2Key]() +## func [ToSliceSeq2Key]() ```go func ToSliceSeq2Key[K, V any](seq Seq2[K, V]) (out []K) @@ -784,7 +784,7 @@ keys := ToSliceSeq2Key(seq) ``` -## func [ToSliceSeq2Value]() +## func [ToSliceSeq2Value]() ```go func ToSliceSeq2Value[K, V any](seq Seq2[K, V]) (out []V) @@ -801,7 +801,7 @@ values := ToSliceSeq2Value(seq) ``` -## type [Seq]() +## type [Seq]() Seq is a sequence of elements provided by an iterator\-like function. We made this alias Seq to iter.Seq for providing a compatible interface in lower go versions. @@ -810,7 +810,7 @@ type Seq[V any] iter.Seq[V] ``` -### func [Chunk]() +### func [Chunk]() ```go func Chunk[T any](seq Seq[T], n int) Seq[[]T] @@ -827,7 +827,7 @@ chunkedSeq := xiter.Chunk(seq, 2) ``` -### func [Compact]() +### func [Compact]() ```go func Compact[T comparable](in Seq[T]) Seq[T] @@ -842,7 +842,7 @@ Compact([]int{0, 1, 2, 3, 4}) 👉 [1 2 3 4] ``` -### func [Concat]() +### func [Concat]() ```go func Concat[V any](seqs ...Seq[V]) Seq[V] @@ -895,7 +895,7 @@ func main() { -### func [Filter]() +### func [Filter]() ```go func Filter[V any](f func(V) bool, seq Seq[V]) Seq[V] @@ -965,7 +965,7 @@ _ = ToSlice(seq) // Returns []int{1, 2} ``` -### func [FromMapKeys]() +### func [FromMapKeys]() ```go func FromMapKeys[K comparable, V any](m map[K]V) Seq[K] @@ -974,7 +974,7 @@ func FromMapKeys[K comparable, V any](m map[K]V) Seq[K] -### func [FromMapValues]() +### func [FromMapValues]() ```go func FromMapValues[K comparable, V any](m map[K]V) Seq[V] @@ -1001,7 +1001,7 @@ func FromSliceReverse[T any, Slice ~[]T](in Slice) Seq[T] -### func [FromSliceShuffle]() +### func [FromSliceShuffle]() ```go func FromSliceShuffle[T any](in []T) Seq[T] @@ -1036,7 +1036,7 @@ intersect := Intersect(FromSlice(left), FromSlice(right)) ``` -### func [Limit]() +### func [Limit]() ```go func Limit[V any](seq Seq[V], n int) Seq[V] @@ -1085,7 +1085,7 @@ func main() { -### func [Map]() +### func [Map]() ```go func Map[In, Out any](f func(In) Out, seq Seq[In]) Seq[Out] @@ -1134,7 +1134,7 @@ func main() { -### func [Merge]() +### func [Merge]() ```go func Merge[V xcmp.Ordered](x, y Seq[V]) Seq[V] @@ -1176,7 +1176,7 @@ func main() { -### func [MergeFunc]() +### func [MergeFunc]() ```go func MergeFunc[V any](x, y Seq[V], f func(V, V) int) Seq[V] @@ -1194,7 +1194,7 @@ func Repeat[T any](seq Seq[T], count int) Seq[T] Repeat return a seq that repeat seq for count times. -### func [Replace]() +### func [Replace]() ```go func Replace[T comparable](seq Seq[T], from, to T, n int) Seq[T] @@ -1211,7 +1211,7 @@ replacedSeq := Replace(seq, 2, 99, -1) // Replace all 2s with 99 ``` -### func [ReplaceAll]() +### func [ReplaceAll]() ```go func ReplaceAll[T comparable](seq Seq[T], from, to T) Seq[T] @@ -1237,7 +1237,7 @@ func Reverse[T any](seq Seq[T]) Seq[T] Reverse return a reversed seq. -### func [Seq2KeyToSeq]() +### func [Seq2KeyToSeq]() ```go func Seq2KeyToSeq[K, V any](in Seq2[K, V]) Seq[K] @@ -1246,7 +1246,7 @@ func Seq2KeyToSeq[K, V any](in Seq2[K, V]) Seq[K] Seq2KeyToSeq return a seq that only contain keys in seq2. -### func [Seq2ToSeqUnion]() +### func [Seq2ToSeqUnion]() ```go func Seq2ToSeqUnion[K, V any](seq Seq2[K, V]) Seq[union.U2[K, V]] @@ -1264,7 +1264,7 @@ for v := range Seq2ToSeqUnion(seq2) { ``` -### func [Seq2ValueToSeq]() +### func [Seq2ValueToSeq]() ```go func Seq2ValueToSeq[K, V any](in Seq2[K, V]) Seq[V] @@ -1273,7 +1273,7 @@ func Seq2ValueToSeq[K, V any](in Seq2[K, V]) Seq[V] Seq2ValueToSeq return a seq that only contain values in seq2. -### func [Skip]() +### func [Skip]() ```go func Skip[T any](seq Seq[T], n int) Seq[T] @@ -1300,7 +1300,7 @@ union := Union(FromSlice(left), FromSlice(right)) ``` -### func [Uniq]() +### func [Uniq]() ```go func Uniq[T comparable](seq Seq[T]) Seq[T] @@ -1317,7 +1317,7 @@ uniqSeq := xiter.Uniq(seq) ``` -### func [Zip]() +### func [Zip]() ```go func Zip[V1, V2 any](x Seq[V1], y Seq[V2]) Seq[Zipped[V1, V2]] @@ -1375,7 +1375,7 @@ func main() { -### func [Zip2]() +### func [Zip2]() ```go func Zip2[K1, V1, K2, V2 any](x Seq2[K1, V1], y Seq2[K2, V2]) Seq[Zipped2[K1, V1, K2, V2]] @@ -1397,7 +1397,7 @@ func Equal2[K, V comparable](x, y Seq2[K, V]) bool { ``` -## type [Seq2]() +## type [Seq2]() Seq2 is a sequence of key/value pair provided by an iterator\-like function. We made this alias Seq2 to iter.Seq2 for providing a compatible interface in lower go versions. @@ -1406,7 +1406,7 @@ type Seq2[K, V any] iter.Seq2[K, V] ``` -### func [Concat2]() +### func [Concat2]() ```go func Concat2[K, V any](seqs ...Seq2[K, V]) Seq2[K, V] @@ -1415,7 +1415,7 @@ func Concat2[K, V any](seqs ...Seq2[K, V]) Seq2[K, V] Concat2 returns an Seq2 over the concatenation of the given Seq2s. Like Concat but run with Seq2 -### func [Filter2]() +### func [Filter2]() ```go func Filter2[K, V any](f func(K, V) bool, seq Seq2[K, V]) Seq2[K, V] @@ -1424,7 +1424,7 @@ func Filter2[K, V any](f func(K, V) bool, seq Seq2[K, V]) Seq2[K, V] Filter2 returns an Seq over seq that only includes the key\-value pairs k, v for which f\(k, v\) is true. Like Filter but run with Seq2 -### func [FromMapKeyAndValues]() +### func [FromMapKeyAndValues]() ```go func FromMapKeyAndValues[K comparable, V any](m map[K]V) Seq2[K, V] @@ -1442,7 +1442,7 @@ func FromSliceIdx[T any](in []T) Seq2[int, T] FromSliceIdx received a slice and returned a Seq2 for this slice, key is index. -### func [Limit2]() +### func [Limit2]() ```go func Limit2[K, V any](seq Seq2[K, V], n int) Seq2[K, V] @@ -1451,7 +1451,7 @@ func Limit2[K, V any](seq Seq2[K, V], n int) Seq2[K, V] Limit2 returns a Seq over Seq2 that stops after n key\-value pairs. Like Limit but run with Seq2 -### func [Map2]() +### func [Map2]() ```go func Map2[KIn, VIn, KOut, VOut any](f func(KIn, VIn) (KOut, VOut), seq Seq2[KIn, VIn]) Seq2[KOut, VOut] @@ -1460,7 +1460,7 @@ func Map2[KIn, VIn, KOut, VOut any](f func(KIn, VIn) (KOut, VOut), seq Seq2[KIn, Map2 returns a Seq2 over the results of applying f to each key\-value pair in seq. Like Map but run with Seq2 -### func [MapToSeq2]() +### func [MapToSeq2]() ```go func MapToSeq2[T any, K comparable](in Seq[T], mapFn func(ele T) K) Seq2[K, T] @@ -1481,7 +1481,7 @@ fmt.Println(ToMap(lenMap)) ``` -### func [MapToSeq2Value]() +### func [MapToSeq2Value]() ```go func MapToSeq2Value[T any, K comparable, V any](in Seq[T], mapFn func(ele T) (K, V)) Seq2[K, V] @@ -1501,7 +1501,7 @@ fmt.Println(ToMap(transformed)) ``` -### func [Merge2]() +### func [Merge2]() ```go func Merge2[K xcmp.Ordered, V any](x, y Seq2[K, V]) Seq2[K, V] @@ -1512,7 +1512,7 @@ Merge2 merges two sequences of key\-value pairs ordered by their keys. Pairs app Merge2 is equivalent to calling MergeFunc2 with cmp.Compare\[K\] as the ordering function. -### func [MergeFunc2]() +### func [MergeFunc2]() ```go func MergeFunc2[K, V any](x, y Seq2[K, V], f func(K, K) int) Seq2[K, V] From 02988857625f6dc378e83913cf0ee9b70d02911d Mon Sep 17 00:00:00 2001 From: "kevin.zhao" Date: Fri, 10 Oct 2025 14:29:19 +0800 Subject: [PATCH 3/3] goimports --- pkg/xiter/xiter_old.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/xiter/xiter_old.go b/pkg/xiter/xiter_old.go index e0491d1..32ba966 100644 --- a/pkg/xiter/xiter_old.go +++ b/pkg/xiter/xiter_old.go @@ -13,6 +13,7 @@ import ( "github.com/dashjay/xiter/pkg/internal/utils" "github.com/dashjay/xiter/pkg/optional" "github.com/dashjay/xiter/pkg/union" + "github.com/dashjay/xiter/pkg/xcmp" ) type Seq[V any] func(yield func(V) bool)