Skip to content

Commit

Permalink
fix: slices.Fold and slices.FoldReverse (#40)
Browse files Browse the repository at this point in the history
* Fix slices.Fold and slices.FoldReverse

* Add tests for slices.Fold and slices.FoldReverse

* Update CHANGELOG.md

* 'nil' -> 'nil slice'

* update .github/workflows/go-format.yml?

---------

Co-authored-by: Alexander Fougner <alexander.fougner@iver.se>
  • Loading branch information
Alexamakans and Alexander Fougner authored Oct 21, 2024
1 parent 1d87ed4 commit a22c8b9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
steps:
- uses: actions/setup-go@v2
with:
go-version: '1.18'
go-version: '1.23'

- name: Install goimports
run: go install golang.org/x/tools/cmd/goimports@latest
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ SPDX-License-Identifier: CC-BY-4.0

This project tries to follow [SemVer 2.0.0](https://semver.org/).

## v4.3.1 (WIP)

- Fixed `slices.Fold` and `slices.FoldReverse` bugs. (#39)

## v4.3.0 (2023-05-04)

- Added `sync2.Map.Len()` method. (#38)
Expand Down
6 changes: 3 additions & 3 deletions slices/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func Filter[S ~[]E, E any](slice S, match func(value E) bool) S {
func Fold[S ~[]E, State, E any](slice S, seed State, acc func(state State, value E) State) State {
state := seed
for _, v := range slice {
seed = acc(state, v)
state = acc(state, v)
}
return state
}
Expand All @@ -282,8 +282,8 @@ func Fold[S ~[]E, State, E any](slice S, seed State, acc func(state State, value
// seed value as-is if the slice is empty.
func FoldReverse[S ~[]E, State, E any](slice S, seed State, acc func(state State, value E) State) State {
state := seed
for i := len(slice) - 1; i >= 0; i++ {
seed = acc(state, slice[i])
for i := len(slice) - 1; i >= 0; i-- {
state = acc(state, slice[i])
}
return state
}
Expand Down
64 changes: 64 additions & 0 deletions slices/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,70 @@ func TestRemoveSlice(t *testing.T) {
}
}

func TestFold(t *testing.T) {
testCases := []struct {
name string
slice []string
seed string
want string
}{
{
name: "values",
slice: []string{"a", "b", "c"},
seed: "",
want: "abc",
},
{
name: "nil slice",
slice: nil,
seed: "seed",
want: "seed",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
gotStr := Fold(tc.slice, tc.seed, func(state string, seed string) string {
return state + seed
})
if gotStr != tc.want {
t.Errorf("want %q, got %q", tc.want, gotStr)
}
})
}
}

func TestFoldReverse(t *testing.T) {
testCases := []struct {
name string
slice []string
seed string
want string
}{
{
name: "values",
slice: []string{"a", "b", "c"},
seed: "",
want: "cba",
},
{
name: "nil slice",
slice: nil,
seed: "seed",
want: "seed",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
gotStr := FoldReverse(tc.slice, tc.seed, func(state string, seed string) string {
return state + seed
})
if gotStr != tc.want {
t.Errorf("want %q, got %q", tc.want, gotStr)
}
})
}
}

func TestConcat(t *testing.T) {
testCases := []struct {
a string
Expand Down

0 comments on commit a22c8b9

Please sign in to comment.