Skip to content

Commit c53d468

Browse files
committed
refactor: migrate to Go 1.22 slices/cmp, update linters
Replace usage of golang.org/x/exp/constraints with Go 1.22 cmp/slices. Update .golangci.yml to new v2 format and enable gofmt/goimports. Refactor imports and type constraints across codebase for consistency.
1 parent 39be942 commit c53d468

File tree

14 files changed

+83
-69
lines changed

14 files changed

+83
-69
lines changed

.golangci.yml

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,54 @@
1-
skip-dirs-use-default: true
2-
3-
run:
4-
timeout: 5m
5-
1+
version: "2"
62
linters:
73
enable:
84
- bodyclose
9-
- deadcode
105
- depguard
116
- dogsled
12-
- errcheck
137
- errorlint
14-
- exportloopref
158
- gocritic
169
- gocyclo
17-
- gofmt
18-
- goimports
1910
- goprintffuncname
20-
- gosimple
2111
- gosec
22-
- govet
23-
- ineffassign
2412
- misspell
2513
- noctx
2614
- nolintlint
2715
- prealloc
2816
- rowserrcheck
2917
- staticcheck
30-
- structcheck
31-
- stylecheck
32-
- typecheck
3318
- unconvert
3419
- unparam
35-
- unused
36-
- varcheck
3720
- whitespace
38-
fast: true
39-
40-
linters-settings:
41-
goimports:
42-
local-prefixes: github.com/rjNemo/underscore
21+
exclusions:
22+
generated: lax
23+
presets:
24+
- comments
25+
- common-false-positives
26+
- legacy
27+
- std-error-handling
28+
paths:
29+
- third_party$
30+
- builtin$
31+
- examples$
32+
settings:
33+
depguard:
34+
rules:
35+
main:
36+
list-mode: lax
37+
files:
38+
- $all
39+
allow:
40+
- $gostd
41+
- github.com/rjNemo/underscore
42+
- github.com/rjNemo/underscore/...
43+
- github.com/stretchr/testify/...
44+
- golang.org/x/exp/constraints
45+
formatters:
46+
enable:
47+
- gofmt
48+
- goimports
49+
exclusions:
50+
generated: lax
51+
paths:
52+
- third_party$
53+
- builtin$
54+
- examples$

any.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package underscore
22

3+
import "slices"
4+
35
// Any returns true if any of the values in the slice pass the predicate truth test.
46
// Short-circuits and stops traversing the slice if a true element is found.
57
func Any[T any](values []T, predicate func(T) bool) bool {
6-
for _, v := range values {
7-
if predicate(v) {
8-
return true
9-
}
10-
}
11-
return false
8+
return slices.ContainsFunc(values, predicate)
129
}

drop_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
)
1010

1111
func TestDrop(t *testing.T) {
12-
1312
nums := []int{1, 9, 2, 8, 3, 7, 4, 6, 5}
1413
want := []int{1, 9, 2, 3, 7, 4, 6, 5}
1514

find_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package underscore_test
33
import (
44
"testing"
55

6-
u "github.com/rjNemo/underscore"
76
"github.com/stretchr/testify/assert"
7+
8+
u "github.com/rjNemo/underscore"
89
)
910

1011
func TestFind(t *testing.T) {

flatmap_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package underscore_test
33
import (
44
"testing"
55

6-
u "github.com/rjNemo/underscore"
76
"github.com/stretchr/testify/assert"
7+
8+
u "github.com/rjNemo/underscore"
89
)
910

1011
func TestFlatmap(t *testing.T) {

intersection_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package underscore_test
33
import (
44
"testing"
55

6-
u "github.com/rjNemo/underscore"
7-
86
"github.com/stretchr/testify/assert"
7+
8+
u "github.com/rjNemo/underscore"
99
)
1010

1111
func TestIntersection(t *testing.T) {

join.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ func Join[T, P any, S comparable](
66
left []T,
77
right []P,
88
leftSelector func(T) S,
9-
rightSelector func(P) S) []Tuple[T, []P] {
10-
11-
var results = make([]Tuple[T, []P], 0, len(left))
9+
rightSelector func(P) S,
10+
) []Tuple[T, []P] {
11+
results := make([]Tuple[T, []P], 0, len(left))
1212
for _, l := range left {
13-
var matches = Filter(right, func(r P) bool { return leftSelector(l) == rightSelector(r) })
14-
var tuple = Tuple[T, []P]{Left: l, Right: matches}
13+
matches := Filter(right, func(r P) bool { return leftSelector(l) == rightSelector(r) })
14+
tuple := Tuple[T, []P]{Left: l, Right: matches}
1515
results = append(results, tuple)
1616
}
1717

@@ -28,8 +28,8 @@ func JoinProject[L, R, O any, S comparable](
2828
right []R,
2929
leftSelector func(L) S,
3030
rightSelector func(R) S,
31-
projection func(Tuple[L, []R]) O) (results []O) {
32-
31+
projection func(Tuple[L, []R]) O,
32+
) (results []O) {
3333
for _, x := range Join(left, right, leftSelector, rightSelector) {
3434
results = append(results, projection(x))
3535
}

join_test.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,26 @@ package underscore_test
33
import (
44
"testing"
55

6-
u "github.com/rjNemo/underscore"
76
"github.com/stretchr/testify/assert"
7+
8+
u "github.com/rjNemo/underscore"
89
)
910

10-
var zero = u.Tuple[int, string]{Left: 0, Right: "Zero"}
11-
var one = u.Tuple[int, string]{Left: 1, Right: "One"}
12-
var two = u.Tuple[int, string]{Left: 2, Right: "Two"}
13-
var three = u.Tuple[int, string]{Left: 3, Right: "Three"}
11+
var (
12+
zero = u.Tuple[int, string]{Left: 0, Right: "Zero"}
13+
one = u.Tuple[int, string]{Left: 1, Right: "One"}
14+
two = u.Tuple[int, string]{Left: 2, Right: "Two"}
15+
three = u.Tuple[int, string]{Left: 3, Right: "Three"}
16+
)
1417

1518
func Test_Join_Can_Join_Two_Slices_Together(t *testing.T) {
16-
var left = []u.Tuple[int, string]{zero, one, two, three}
17-
var right = []u.Tuple[int, string]{one, three, two, three, two, three}
19+
left := []u.Tuple[int, string]{zero, one, two, three}
20+
right := []u.Tuple[int, string]{one, three, two, three, two, three}
1821

1922
selector := func(x u.Tuple[int, string]) int { return x.Left }
2023

21-
var joined = u.Join(left, right, selector, selector)
22-
var want = []u.Tuple[u.Tuple[int, string], []u.Tuple[int, string]]{
24+
joined := u.Join(left, right, selector, selector)
25+
want := []u.Tuple[u.Tuple[int, string], []u.Tuple[int, string]]{
2326
{Left: zero, Right: nil},
2427
{Left: one, Right: []u.Tuple[int, string]{one}},
2528
{Left: two, Right: []u.Tuple[int, string]{two, two}},
@@ -30,16 +33,16 @@ func Test_Join_Can_Join_Two_Slices_Together(t *testing.T) {
3033
}
3134

3235
func Test_Join_Can_Join_and_Project_Two_Slices_Together(t *testing.T) {
33-
var left = []u.Tuple[int, string]{zero, one, two, three}
34-
var right = []u.Tuple[int, string]{one, three, two, three, two, three}
36+
left := []u.Tuple[int, string]{zero, one, two, three}
37+
right := []u.Tuple[int, string]{one, three, two, three, two, three}
3538

3639
selector := func(x u.Tuple[int, string]) int { return x.Left }
3740
project := func(x u.Tuple[u.Tuple[int, string], []u.Tuple[int, string]]) int {
3841
return len(x.Right) // projecting to a could of how many
3942
}
4043

41-
var joined = u.JoinProject(left, right, selector, selector, project)
42-
var want = []int{0, 1, 2, 3}
44+
joined := u.JoinProject(left, right, selector, selector, project)
45+
want := []int{0, 1, 2, 3}
4346

4447
assert.Equal(t, want, joined)
4548
}

max.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package underscore
22

3-
import "golang.org/x/exp/constraints"
3+
import "cmp"
44

55
// Max returns the maximum value in the slice.
66
// This function can currently only compare numbers reliably.
77
// This function uses operator <.
8-
func Max[T constraints.Ordered](values []T) T {
8+
func Max[T cmp.Ordered](values []T) T {
99
max := values[0]
1010
for _, v := range values {
1111
if v > max {

min.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package underscore
22

3-
import "golang.org/x/exp/constraints"
3+
import "cmp"
44

55
// Min returns the minimum value in the slice.
66
// This function can currently only compare numbers reliably.
77
// This function uses operator <.
8-
func Min[T constraints.Ordered](values []T) T {
8+
func Min[T cmp.Ordered](values []T) T {
99
min := values[0]
1010
for _, v := range values {
1111
if v < min {

0 commit comments

Comments
 (0)