Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions pkg/cmp/README.md → pkg/xcmp/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!-- Code generated by gomarkdoc. DO NOT EDIT -->

# cmp
# xcmp

```go
import "github.com/dashjay/xiter/pkg/cmp"
import "github.com/dashjay/xiter/pkg/xcmp"
```

## Index
Expand All @@ -15,7 +15,7 @@ import "github.com/dashjay/xiter/pkg/cmp"


<a name="Compare"></a>
## func [Compare](<https://github.com/dashjay/xiter/blob/main/pkg/cmp/cmp.go#L14>)
## func [Compare](<https://github.com/dashjay/xiter/blob/main/pkg/xcmp/xcmp.go#L14>)

```go
func Compare[T Ordered](x, y T) int
Expand All @@ -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))
}
```

Expand All @@ -59,7 +59,7 @@ func main() {
</details>

<a name="Less"></a>
## func [Less](<https://github.com/dashjay/xiter/blob/main/pkg/cmp/cmp.go#L10>)
## func [Less](<https://github.com/dashjay/xiter/blob/main/pkg/xcmp/xcmp.go#L10>)

```go
func Less[T Ordered](x, y T) bool
Expand All @@ -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))
}
```

Expand All @@ -103,7 +103,7 @@ true
</details>

<a name="Or"></a>
## func [Or](<https://github.com/dashjay/xiter/blob/main/pkg/cmp/cmp.go#L18>)
## func [Or](<https://github.com/dashjay/xiter/blob/main/pkg/xcmp/xcmp.go#L18>)

```go
func Or[T comparable](vals ...T) T
Expand All @@ -122,7 +122,7 @@ package main
import (
"fmt"

"github.com/dashjay/xiter/pkg/cmp"
"github.com/dashjay/xiter/pkg/xcmp"
)

func main() {
Expand All @@ -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"))
}
```

Expand Down Expand Up @@ -161,7 +161,7 @@ import (
"sort"
"strings"

"github.com/dashjay/xiter/pkg/cmp"
"github.com/dashjay/xiter/pkg/xcmp"
)

type Order struct {
Expand All @@ -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
Expand Down Expand Up @@ -233,7 +233,7 @@ baz carol 4.00
</details>

<a name="Ordered"></a>
## type [Ordered](<https://github.com/dashjay/xiter/blob/main/pkg/cmp/cmp.go#L8>)
## type [Ordered](<https://github.com/dashjay/xiter/blob/main/pkg/xcmp/xcmp.go#L8>)



Expand Down
4 changes: 2 additions & 2 deletions pkg/cmp/cmd_old.go → pkg/xcmp/xcmd_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 < <= >= >.
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmp/cmp.go → pkg/xcmp/xcmp.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build go1.23
// +build go1.23

package cmp
package xcmp

import "cmp"

Expand Down
52 changes: 26 additions & 26 deletions pkg/cmp/cmp_test.go → pkg/xcmp/xcmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
}
Expand All @@ -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)
}
}
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pkg/xiter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>)
Expand All @@ -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>)
Expand Down Expand Up @@ -1137,7 +1137,7 @@ func main() {
### func [Merge](<https://github.com/dashjay/xiter/blob/main/pkg/xiter/xiter.go#L253>)

```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.
Expand Down Expand Up @@ -1504,7 +1504,7 @@ fmt.Println(ToMap(transformed))
### func [Merge2](<https://github.com/dashjay/xiter/blob/main/pkg/xiter/xiter.go#L299>)

```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.
Expand Down
10 changes: 5 additions & 5 deletions pkg/xiter/xiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions pkg/xiter/xiter_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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] {
Expand Down Expand Up @@ -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] {
Expand Down