Skip to content

Commit 5535e83

Browse files
committed
the package is based on go 1.23
1 parent 20f2e7c commit 5535e83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+434
-432
lines changed

aggregate_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"iter"
77
"reflect"
8+
"slices"
89
"strings"
910
"testing"
1011
)
@@ -283,7 +284,7 @@ func ExampleAggregate() {
283284
words := strings.Fields(sentence)
284285
// Prepend each word to the beginning of the new sentence to reverse the word order.
285286
reversed, _ := Aggregate(
286-
SliceToSeq(words),
287+
slices.Values(words),
287288
func(workingSentence, next string) string { return next + " " + workingSentence },
288289
)
289290
fmt.Println(reversed)
@@ -297,7 +298,7 @@ func ExampleAggregateSeed() {
297298
ints := []int{4, 8, 8, 3, 9, 0, 7, 8, 2}
298299
// Count the even numbers in the array, using a seed value of 0.
299300
numEven, _ := AggregateSeed(
300-
SliceToSeq(ints),
301+
slices.Values(ints),
301302
0,
302303
func(total, next int) int {
303304
if next%2 == 0 {
@@ -317,7 +318,7 @@ func ExampleAggregateSeedSel() {
317318
fruits := []string{"apple", "mango", "orange", "passionfruit", "grape"}
318319
// Determine whether any string in the array is longer than "banana".
319320
longestName, _ := AggregateSeedSel(
320-
SliceToSeq(fruits),
321+
slices.Values(fruits),
321322
"banana",
322323
func(longest, next string) string {
323324
if len(next) > len(longest) {

all_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"iter"
7+
"slices"
78
"strings"
89
"testing"
910

@@ -143,7 +144,7 @@ func ExampleAll_ex1() {
143144
}
144145
// Determine whether all Pet names in the array start with 'B'.
145146
allStartWithB, _ := All(
146-
SliceToSeq(pets),
147+
slices.Values(pets),
147148
func(pet Pet) bool { return strings.HasPrefix(pet.Name, "B") },
148149
)
149150
var what string
@@ -191,10 +192,10 @@ func ExampleAll_ex2() {
191192
}
192193
// Determine which people have Pets that are all older than 5.
193194
where, _ := Where(
194-
SliceToSeq(people),
195+
slices.Values(people),
195196
func(person Person) bool {
196197
return errorhelper.Must(All(
197-
SliceToSeq(person.Pets),
198+
slices.Values(person.Pets),
198199
func(pet Pet) bool { return pet.Age > 5 },
199200
))
200201
},
@@ -220,10 +221,10 @@ func ExampleAll_ex3() {
220221
{Name: "Adam's", Items: []string{"kiwi", "apple", "orange"}},
221222
}
222223
where, _ := Where(
223-
SliceToSeq(markets),
224+
slices.Values(markets),
224225
func(m Market) bool {
225226
return errorhelper.Must(All(
226-
SliceToSeq(m.Items),
227+
slices.Values(m.Items),
227228
func(item string) bool { return len(item) == 5 },
228229
))
229230
},

any_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"iter"
7+
"slices"
78
"strings"
89
"testing"
910

@@ -206,8 +207,8 @@ func ExampleAny_ex2() {
206207
}
207208
// Determine which people have a non-empty Pet array.
208209
where, _ := Where(
209-
SliceToSeq(people),
210-
func(person Person) bool { return errorhelper.Must(Any(SliceToSeq(person.Pets))) },
210+
slices.Values(people),
211+
func(person Person) bool { return errorhelper.Must(Any(slices.Values(person.Pets))) },
211212
)
212213
names, _ := Select(
213214
where,
@@ -232,7 +233,7 @@ func ExampleAnyPred_ex1() {
232233
}
233234
// Determine whether any pets over Age 1 are also unvaccinated.
234235
unvaccinated, _ := AnyPred(
235-
SliceToSeq(pets),
236+
slices.Values(pets),
236237
func(pet Pet) bool { return pet.Age > 1 && pet.Vaccinated == false },
237238
)
238239
var what string
@@ -255,10 +256,10 @@ func ExampleAnyPred_ex2() {
255256
{Name: "Adam's", Items: []string{"kiwi", "apple", "orange"}},
256257
}
257258
where, _ := Where(
258-
SliceToSeq(markets),
259+
slices.Values(markets),
259260
func(m Market) bool {
260261
return errorhelper.Must(AnyPred(
261-
SliceToSeq(m.Items),
262+
slices.Values(m.Items),
262263
func(item string) bool { return strings.HasPrefix(item, "o") },
263264
))
264265
},

append.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@ func Append[Source any](source iter.Seq[Source], element Source) (iter.Seq[Sourc
1414
return nil, errorhelper.CallerError(ErrNilSource)
1515
}
1616
repeat1, _ := Repeat(element, 1)
17-
return Concat(source, repeat1)
17+
r, err := Concat(source, repeat1)
18+
if err != nil {
19+
return nil, errorhelper.CallerError(err)
20+
}
21+
return r, nil
1822
}

chunk_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"iter"
7+
"slices"
78
"testing"
89
)
910

@@ -43,7 +44,7 @@ func TestChunk_int(t *testing.T) {
4344
source: Empty[int](),
4445
size: 2,
4546
},
46-
want: SliceToSeq([][]int{}),
47+
want: slices.Values([][]int{}),
4748
},
4849
{name: "1",
4950
args: args{

concat_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package go2linq
33
import (
44
"fmt"
55
"iter"
6+
"slices"
67
"testing"
78

89
"github.com/solsw/errorhelper"
@@ -198,11 +199,11 @@ func ExampleConcat() {
198199
}
199200
concat, _ := Concat(
200201
errorhelper.Must(Select(
201-
SliceToSeq(cats),
202+
slices.Values(cats),
202203
func(cat Pet) string { return cat.Name },
203204
)),
204205
errorhelper.Must(Select(
205-
SliceToSeq(dogs),
206+
slices.Values(dogs),
206207
func(dog Pet) string { return dog.Name },
207208
)),
208209
)

contains.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ func Contains[Source any](source iter.Seq[Source], value Source) (bool, error) {
1414
if source == nil {
1515
return false, errorhelper.CallerError(ErrNilSource)
1616
}
17-
return ContainsEq(source, value, generichelper.DeepEqual[Source])
17+
r, err := ContainsEq(source, value, generichelper.DeepEqual[Source])
18+
if err != nil {
19+
return false, errorhelper.CallerError(err)
20+
}
21+
return r, nil
1822
}
1923

2024
// [ContainsEq] determines whether a sequence contains a specified element using a specified 'equal'.

contains_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package go2linq
33
import (
44
"fmt"
55
"iter"
6+
"slices"
67
"strings"
78
"testing"
89

@@ -119,7 +120,7 @@ func TestContainsEq_int(t *testing.T) {
119120
func ExampleContains_ex1() {
120121
fruits := []string{"apple", "banana", "mango", "orange", "passionfruit", "grape"}
121122
fruit := "mango"
122-
hasMango, _ := Contains(SliceToSeq(fruits), fruit)
123+
hasMango, _ := Contains(slices.Values(fruits), fruit)
123124
var what string
124125
if hasMango {
125126
what = "does"
@@ -140,9 +141,9 @@ func ExampleContains_ex2() {
140141
{Name: "Adam's", Items: []string{"kiwi", "apple", "orange"}},
141142
}
142143
where, _ := Where(
143-
SliceToSeq(markets),
144+
slices.Values(markets),
144145
func(m Market) bool {
145-
return errorhelper.Must(Contains(SliceToSeq(m.Items), "kiwi"))
146+
return errorhelper.Must(Contains(slices.Values(m.Items), "kiwi"))
146147
},
147148
)
148149
names, _ := Select(where, func(m Market) string { return m.Name })

count_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"iter"
7+
"slices"
78
"testing"
89

910
"github.com/solsw/errorhelper"
@@ -185,7 +186,7 @@ func TestCountPred_string(t *testing.T) {
185186
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.count
186187
func ExampleCount() {
187188
fruits := []string{"apple", "banana", "mango", "orange", "passionfruit", "grape"}
188-
numberOfFruits, _ := Count(SliceToSeq(fruits))
189+
numberOfFruits, _ := Count(slices.Values(fruits))
189190
fmt.Printf("There are %d fruits in the collection.\n", numberOfFruits)
190191
// Output:
191192
// There are 6 fruits in the collection.
@@ -200,7 +201,7 @@ func ExampleCountPred_ex1() {
200201
{Name: "Whiskers", Vaccinated: false},
201202
}
202203
numberUnvaccinated, _ := CountPred(
203-
SliceToSeq(pets),
204+
slices.Values(pets),
204205
func(p Pet) bool { return p.Vaccinated == false },
205206
)
206207
fmt.Printf("There are %d unvaccinated animals.\n", numberUnvaccinated)
@@ -218,7 +219,7 @@ func ExampleCountPred_ex2() {
218219
}
219220
const Age = 3
220221
count, _ := CountPred(
221-
SliceToSeq(pets),
222+
slices.Values(pets),
222223
func(pet Pet) bool { return pet.Age > Age },
223224
)
224225
fmt.Printf("There are %d animals over age %d.\n", count, Age)

defaultifempty.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ func DefaultIfEmpty[Source any](source iter.Seq[Source]) (iter.Seq[Source], erro
1616
if source == nil {
1717
return nil, errorhelper.CallerError(ErrNilSource)
1818
}
19-
return DefaultIfEmptyDef(source, generichelper.ZeroValue[Source]())
19+
r, err := DefaultIfEmptyDef(source, generichelper.ZeroValue[Source]())
20+
if err != nil {
21+
return nil, errorhelper.CallerError(err)
22+
}
23+
return r, nil
2024
}
2125

2226
// [DefaultIfEmptyDef] returns the elements of a specified sequence

defaultifempty_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package go2linq
33
import (
44
"fmt"
55
"iter"
6+
"slices"
67
"testing"
78
)
89

@@ -80,7 +81,7 @@ func TestDefaultIfEmptyDef_int(t *testing.T) {
8081
// last example from
8182
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.defaultifempty
8283
func ExampleDefaultIfEmpty_ex1() {
83-
numbers, _ := DefaultIfEmpty(SliceToSeq([]int{}))
84+
numbers, _ := DefaultIfEmpty(slices.Values([]int{}))
8485
for number := range numbers {
8586
fmt.Println(number)
8687
}
@@ -96,7 +97,7 @@ func ExampleDefaultIfEmpty_ex2() {
9697
{Name: "Boots", Age: 4},
9798
{Name: "Whiskers", Age: 1},
9899
}
99-
defaultIfEmpty, _ := DefaultIfEmpty(SliceToSeq(pets))
100+
defaultIfEmpty, _ := DefaultIfEmpty(slices.Values(pets))
100101
for pet := range defaultIfEmpty {
101102
fmt.Println(pet.Name)
102103
}
@@ -115,12 +116,12 @@ func ExampleDefaultIfEmptyDef() {
115116
{Name: "Boots", Age: 4},
116117
{Name: "Whiskers", Age: 1},
117118
}
118-
defaultIfEmptyDef1, _ := DefaultIfEmptyDef(SliceToSeq(pets1), defaultPet)
119+
defaultIfEmptyDef1, _ := DefaultIfEmptyDef(slices.Values(pets1), defaultPet)
119120
for pet := range defaultIfEmptyDef1 {
120121
fmt.Printf("Name: %s\n", pet.Name)
121122
}
122123
pets2 := []Pet{}
123-
defaultIfEmptyDef2, _ := DefaultIfEmptyDef(SliceToSeq(pets2), defaultPet)
124+
defaultIfEmptyDef2, _ := DefaultIfEmptyDef(slices.Values(pets2), defaultPet)
124125
for pet := range defaultIfEmptyDef2 {
125126
fmt.Printf("\nName: %s\n", pet.Name)
126127
}

distinct.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,29 @@ func Distinct[Source any](source iter.Seq[Source]) (iter.Seq[Source], error) {
1515
if source == nil {
1616
return nil, errorhelper.CallerError(ErrNilSource)
1717
}
18-
return DistinctEq(source, generichelper.DeepEqual[Source])
18+
r, err := DistinctEq(source, generichelper.DeepEqual[Source])
19+
if err != nil {
20+
return nil, errorhelper.CallerError(err)
21+
}
22+
return r, nil
1923
}
2024

2125
// [DistinctEq] returns distinct elements from a sequence using a specified 'equal' to compare values.
2226
// Order of elements in the result corresponds to the order of elements in 'source'.
2327
//
2428
// [DistinctEq]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.distinct
2529
func DistinctEq[Source any](source iter.Seq[Source], equal func(Source, Source) bool) (iter.Seq[Source], error) {
26-
return DistinctByEq(source, Identity[Source], equal)
30+
if source == nil {
31+
return nil, errorhelper.CallerError(ErrNilSource)
32+
}
33+
if equal == nil {
34+
return nil, errorhelper.CallerError(ErrNilEqual)
35+
}
36+
r, err := DistinctByEq(source, Identity[Source], equal)
37+
if err != nil {
38+
return nil, errorhelper.CallerError(err)
39+
}
40+
return r, nil
2741
}
2842

2943
// [DistinctCmp] returns distinct elements from a sequence using a specified 'compare' to compare values.
@@ -36,5 +50,15 @@ func DistinctEq[Source any](source iter.Seq[Source], equal func(Source, Source)
3650
//
3751
// [DistinctCmp]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.distinct
3852
func DistinctCmp[Source any](source iter.Seq[Source], compare func(Source, Source) int) (iter.Seq[Source], error) {
39-
return DistinctByCmp(source, Identity[Source], compare)
53+
if source == nil {
54+
return nil, errorhelper.CallerError(ErrNilSource)
55+
}
56+
if compare == nil {
57+
return nil, errorhelper.CallerError(ErrNilCompare)
58+
}
59+
r, err := DistinctByCmp(source, Identity[Source], compare)
60+
if err != nil {
61+
return nil, errorhelper.CallerError(err)
62+
}
63+
return r, nil
4064
}

distinct_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"iter"
88
"math/rand"
99
"reflect"
10+
"slices"
1011
"strings"
1112
"testing"
1213

@@ -241,9 +242,9 @@ func TestDistinctCmp_int(t *testing.T) {
241242
func BenchmarkDistinctEq(b *testing.B) {
242243
N := 10000
243244
rng := errorhelper.Must(Range(1, N))
244-
slc, _ := ToSlice(errorhelper.Must(Range(1, N)))
245+
slc := slices.Collect(errorhelper.Must(Range(1, N)))
245246
rand.Shuffle(N, reflect.Swapper(slc))
246-
concat, _ := Concat(rng, SliceToSeq(slc))
247+
concat, _ := Concat(rng, slices.Values(slc))
247248
b.ResetTimer()
248249
for i := 0; i < b.N; i++ {
249250
got, _ := DistinctEq(concat, generichelper.DeepEqual[int])
@@ -258,9 +259,9 @@ func BenchmarkDistinctEq(b *testing.B) {
258259
func BenchmarkDistinctCmp(b *testing.B) {
259260
N := 10000
260261
rng := errorhelper.Must(Range(1, N))
261-
slc, _ := ToSlice(errorhelper.Must(Range(1, N)))
262+
slc := slices.Collect(errorhelper.Must(Range(1, N)))
262263
rand.Shuffle(N, reflect.Swapper(slc))
263-
concat, _ := Concat(rng, SliceToSeq(slc))
264+
concat, _ := Concat(rng, slices.Values(slc))
264265
b.ResetTimer()
265266
for i := 0; i < b.N; i++ {
266267
got, _ := DistinctCmp(concat, cmp.Compare[int])
@@ -276,7 +277,7 @@ func BenchmarkDistinctCmp(b *testing.B) {
276277
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.distinct
277278
func ExampleDistinct() {
278279
ages := []int{21, 46, 46, 55, 17, 21, 55, 55}
279-
distinct, _ := Distinct(SliceToSeq(ages))
280+
distinct, _ := Distinct(slices.Values(ages))
280281
fmt.Println("Distinct ages:")
281282
for age := range distinct {
282283
fmt.Println(age)
@@ -299,7 +300,7 @@ func ExampleDistinctEq() {
299300
{Name: "lemon", Code: 12},
300301
}
301302
//Exclude duplicates.
302-
distinctEq, _ := DistinctEq(SliceToSeq(products), func(p1, p2 Product) bool {
303+
distinctEq, _ := DistinctEq(slices.Values(products), func(p1, p2 Product) bool {
303304
return p1.Code == p2.Code && strings.EqualFold(p1.Name, p2.Name)
304305
})
305306
for product := range distinctEq {

0 commit comments

Comments
 (0)