Skip to content

Commit

Permalink
Add op.Add accumulator
Browse files Browse the repository at this point in the history
  • Loading branch information
BooleanCat committed Mar 1, 2024
1 parent 0f5196b commit 815b5a2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
6 changes: 2 additions & 4 deletions iter/iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/BooleanCat/go-functional/v2/internal/assert"
"github.com/BooleanCat/go-functional/v2/iter"
"github.com/BooleanCat/go-functional/v2/iter/op"
)

func ExampleCollect() {
Expand Down Expand Up @@ -155,10 +156,7 @@ func TestForEach2Empty(t *testing.T) {
}

func ExampleReduce() {
fmt.Println(iter.Reduce(iter.Lift([]int{1, 2, 3}), func(a, b int) int {
return a + b
}, 0))

fmt.Println(iter.Reduce(iter.Lift([]int{1, 2, 3}), op.Add, 0))
// Output: 6
}

Expand Down
6 changes: 6 additions & 0 deletions iter/op/op.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package op

// Add returns the sum of `a` and `b`.
func Add[V ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~string | ~float32 | ~float64](a, b V) V {
return a + b
}
18 changes: 18 additions & 0 deletions iter/op/op_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package op_test

import (
"fmt"

"github.com/BooleanCat/go-functional/v2/iter"
"github.com/BooleanCat/go-functional/v2/iter/op"
)

func ExampleAdd() {
fmt.Println(iter.Reduce(iter.Lift([]int{1, 2, 3}), op.Add, 0))
// Output: 6
}

func ExampleAdd_string() {
fmt.Println(iter.Reduce(iter.Lift([]string{"a", "b", "c"}), op.Add, ""))
// Output: abc
}

0 comments on commit 815b5a2

Please sign in to comment.