Skip to content

Commit 8cb324a

Browse files
committed
slice.Where added
1 parent c843d36 commit 8cb324a

File tree

6 files changed

+104
-14
lines changed

6 files changed

+104
-14
lines changed

enumerator.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import (
88
"strings"
99
)
1010

11-
// Enumerator supports a simple iteration over a generic sequence. T - the type of objects to enumerate.
12-
// (https://docs.microsoft.com/dotnet/api/system.collections.generic.ienumerator-1)
11+
// Enumerator supports a simple iteration over a generic sequence. T - the type of objects to enumerate
12+
// (https://docs.microsoft.com/dotnet/api/system.collections.generic.ienumerator-1).
1313
type Enumerator[T any] interface {
1414

15-
// MoveNext advances the enumerator to the next element of the sequence.
16-
// (https://docs.microsoft.com/dotnet/api/system.collections.ienumerator.movenext)
15+
// MoveNext advances the enumerator to the next element of the sequence
16+
// (https://docs.microsoft.com/dotnet/api/system.collections.ienumerator.movenext).
1717
MoveNext() bool
1818

19-
// Current returns the element in the sequence at the current position of the enumerator.
20-
// (https://docs.microsoft.com/dotnet/api/system.collections.generic.ienumerator-1.current)
19+
// Current returns the element in the sequence at the current position of the enumerator
20+
// (https://docs.microsoft.com/dotnet/api/system.collections.generic.ienumerator-1.current).
2121
Current() T
2222

2323
// Reset sets the enumerator to its initial position, which is before the first element in the sequence
@@ -27,15 +27,15 @@ type Enumerator[T any] interface {
2727
Reset()
2828
}
2929

30-
// enrToSlice creates a slice from an Enumerator. enrToSlice returns nil if 'enr' is nil.
30+
// enrToSlice creates a slice from Enumerator (returns nil if 'enr' is nil).
3131
func enrToSlice[T any](enr Enumerator[T]) []T {
3232
if enr == nil {
3333
return nil
3434
}
3535
if slicer, ok := enr.(Slicer[T]); ok {
3636
return slicer.Slice()
3737
}
38-
var r []T
38+
r := []T{}
3939
for enr.MoveNext() {
4040
r = append(r, enr.Current())
4141
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module github.com/solsw/go2linq/v2
22

33
go 1.18
44

5-
require golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
5+
require golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0
66

7-
require golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d
7+
require golang.org/x/exp v0.0.0-20221002003631-540bb7301a08

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d h1:vtUKgx8dahOomfFzLREU8nSv25YHnTgLBn4rDnWZdU0=
2-
golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
3-
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f h1:Ax0t5p6N38Ga0dThY21weqDEyz2oklo4IvDkpigvkD8=
4-
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
1+
golang.org/x/exp v0.0.0-20221002003631-540bb7301a08 h1:LtBIgSqNhkuC9gA3BFjGy5obHQT1lnmNsMDFSqWzQ5w=
2+
golang.org/x/exp v0.0.0-20221002003631-540bb7301a08/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
3+
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 h1:cu5kTvlzcw1Q5S9f5ip1/cpiB4nXvw1XYzFPGgzLUOY=
4+
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

slice/go_test.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
go test

slice/where.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build go1.18
2+
3+
package slice
4+
5+
import (
6+
"github.com/solsw/go2linq/v2"
7+
)
8+
9+
// Where filters a slice of T based on a predicate.
10+
// If 'source' is nil, nil is returned.
11+
// If 'source' is empty, new empty slice is returned.
12+
func Where[T any](source []T, predicate func(T) bool) ([]T, error) {
13+
if source == nil {
14+
return nil, nil
15+
}
16+
if len(source) == 0 {
17+
return []T{}, nil
18+
}
19+
en, err := go2linq.Where(
20+
go2linq.NewEnSlice(source...),
21+
predicate,
22+
)
23+
if err != nil {
24+
return nil, err
25+
}
26+
return go2linq.ToSlice(en)
27+
}

slice/where_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//go:build go1.18
2+
3+
package slice
4+
5+
import (
6+
"reflect"
7+
"testing"
8+
)
9+
10+
func TestWhere_int(t *testing.T) {
11+
type args struct {
12+
source []int
13+
predicate func(int) bool
14+
}
15+
tests := []struct {
16+
name string
17+
args args
18+
want []int
19+
wantErr bool
20+
}{
21+
{name: "NilSource",
22+
args: args{
23+
source: nil,
24+
predicate: func(i int) bool { return i > 5 },
25+
},
26+
want: nil,
27+
},
28+
{name: "EmptySource",
29+
args: args{
30+
source: []int{},
31+
predicate: func(i int) bool { return i > 5 },
32+
},
33+
want: []int{},
34+
},
35+
{name: "NilPredicate",
36+
args: args{
37+
source: []int{1, 3, 4, 2, 8, 1},
38+
predicate: nil,
39+
},
40+
wantErr: true,
41+
},
42+
{name: "SimpleFiltering",
43+
args: args{
44+
source: []int{1, 3, 4, 2, 8, 1},
45+
predicate: func(i int) bool { return i < 4 },
46+
},
47+
want: []int{1, 3, 2, 1},
48+
},
49+
}
50+
for _, tt := range tests {
51+
t.Run(tt.name, func(t *testing.T) {
52+
got, err := Where(tt.args.source, tt.args.predicate)
53+
if (err != nil) != tt.wantErr {
54+
t.Errorf("Where() error = %v, wantErr %v", err, tt.wantErr)
55+
return
56+
}
57+
if !reflect.DeepEqual(got, tt.want) {
58+
t.Errorf("Where() = %v, want %v", got, tt.want)
59+
}
60+
})
61+
}
62+
}

0 commit comments

Comments
 (0)