diff --git a/pkg/cmp/README.md b/pkg/xcmp/README.md similarity index 74% rename from pkg/cmp/README.md rename to pkg/xcmp/README.md index 2b695c3..72db7a6 100644 --- a/pkg/cmp/README.md +++ b/pkg/xcmp/README.md @@ -1,9 +1,9 @@ -# cmp +# xcmp ```go -import "github.com/dashjay/xiter/pkg/cmp" +import "github.com/dashjay/xiter/pkg/xcmp" ``` ## Index @@ -15,7 +15,7 @@ import "github.com/dashjay/xiter/pkg/cmp" -## func [Compare]() +## func [Compare]() ```go func Compare[T Ordered](x, y T) int @@ -35,14 +35,14 @@ import ( "fmt" "math" - "github.com/dashjay/xiter/pkg/cmp" + "github.com/dashjay/xiter/pkg/xcmp" ) 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)) + 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)) } ``` @@ -59,7 +59,7 @@ func main() { -## func [Less]() +## func [Less]() ```go func Less[T Ordered](x, y T) bool @@ -79,14 +79,14 @@ import ( "fmt" "math" - "github.com/dashjay/xiter/pkg/cmp" + "github.com/dashjay/xiter/pkg/xcmp" ) 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)) + 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)) } ``` @@ -103,7 +103,7 @@ true -## func [Or]() +## func [Or]() ```go func Or[T comparable](vals ...T) T @@ -122,7 +122,7 @@ package main import ( "fmt" - "github.com/dashjay/xiter/pkg/cmp" + "github.com/dashjay/xiter/pkg/xcmp" ) func main() { @@ -131,9 +131,9 @@ func main() { 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")) } ``` @@ -161,7 +161,7 @@ import ( "sort" "strings" - "github.com/dashjay/xiter/pkg/cmp" + "github.com/dashjay/xiter/pkg/xcmp" ) type Order struct { @@ -177,10 +177,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 @@ -233,7 +233,7 @@ baz carol 4.00 -## type [Ordered]() +## type [Ordered]() diff --git a/pkg/cmp/cmd_old.go b/pkg/xcmp/xcmd_old.go similarity index 96% rename from pkg/cmp/cmd_old.go rename to pkg/xcmp/xcmd_old.go index 316f19c..b1285fb 100644 --- a/pkg/cmp/cmd_old.go +++ b/pkg/xcmp/xcmd_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/xcmp.go similarity index 95% rename from pkg/cmp/cmp.go rename to pkg/xcmp/xcmp.go index 31c1b86..5071060 100644 --- a/pkg/cmp/cmp.go +++ b/pkg/xcmp/xcmp.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/xcmp_test.go similarity index 77% rename from pkg/cmp/cmp_test.go rename to pkg/xcmp/xcmp_test.go index f075515..375767f 100644 --- a/pkg/cmp/cmp_test.go +++ b/pkg/xcmp/xcmp_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..b28bf97 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,7 +95,7 @@ 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>) @@ -1137,7 +1137,7 @@ func main() { ### 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. @@ -1504,7 +1504,7 @@ fmt.Println(ToMap(transformed)) ### 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. 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..32ba966 100644 --- a/pkg/xiter/xiter_old.go +++ b/pkg/xiter/xiter_old.go @@ -9,11 +9,11 @@ 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" "github.com/dashjay/xiter/pkg/union" + "github.com/dashjay/xiter/pkg/xcmp" ) type Seq[V any] func(yield func(V) bool) @@ -177,8 +177,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 +207,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] {