Skip to content

Commit

Permalink
adding index arguments to Map, ForEach and Reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Mar 3, 2022
1 parent 45984c3 commit 60559e1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.1.0 (2022-03-03)

Adding `i int` param to lo.Map(), lo.Filter(), lo.ForEach() and lo.Reduce() predicates.

## 1.0.0 (2022-03-02)

*Initial release*
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tools:
${BIN} install github.com/psampaz/go-mod-outdated@latest
${BIN} install github.com/jondot/goweight@latest
${BIN} install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
${BIN} install golang.org/x/tools/cmd/cover
${BIN} get -t -u golang.org/x/tools/cmd/cover
${BIN} get -t -u github.com/sonatype-nexus-community/nancy@latest
go mod tidy

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Constraints:
Manipulates a slice and transforms it to a slice of another type:

```go
lo.Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64) string {
lo.Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
return strconv.FormatInt(x, 10)
})
// []string{"1", "2", "3", "4"}
Expand Down Expand Up @@ -132,7 +132,7 @@ present := lo.Contains[int]([]int{0, 1, 2, 3, 4, 5}, 5)
Reduces collection to a value which is the accumulated result of running each element in collection through accumulator, where each successive invocation is supplied the return value of the previous.

```go
sum := lo.Reduce[int, int]([]int{1, 2, 3, 4}, func(agg int, item int) int {
sum := lo.Reduce[int, int]([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int {
return agg + item
}, 0)
// 10
Expand Down
4 changes: 3 additions & 1 deletion pointers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ func ToPtr[T any](x T) *T {

// ToPtr returns a slice of pointer copy of value.
func ToSlicePtr[T any](collection []T) []*T {
return Map(collection, ToPtr[T])
return Map(collection, func (x T, _ int) *T {
return &x
})
}
18 changes: 9 additions & 9 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ func Filter[V any](collection []V, predicate func(V, int) bool) []V {
}

// Map manipulates a slice and transforms it to a slice of another type.
func Map[T any, R any](collection []T, iteratee func(T) R) []R {
func Map[T any, R any](collection []T, iteratee func(T, int) R) []R {
result := make([]R, 0, len(collection))

for _, item := range collection {
result = append(result, iteratee(item))
for i, item := range collection {
result = append(result, iteratee(item, i))
}

return result
}

// Reduce reduces collection to a value which is the accumulated result of running each element in collection
// through accumulator, where each successive invocation is supplied the return value of the previous.
func Reduce[T any, R any](collection []T, accumulator func(R, T) R, initial R) R {
for _, item := range collection {
initial = accumulator(initial, item)
func Reduce[T any, R any](collection []T, accumulator func(R, T, int) R, initial R) R {
for i, item := range collection {
initial = accumulator(initial, item, i)
}

return initial
}

// ForEach iterates over elements of collection and invokes iteratee for each element.
func ForEach[T any](collection []T, iteratee func(T)) {
for _, item := range collection {
iteratee(item)
func ForEach[T any](collection []T, iteratee func(T, int)) {
for i, item := range collection {
iteratee(item, i)
}
}

Expand Down
8 changes: 4 additions & 4 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func TestFilter(t *testing.T) {
func TestMap(t *testing.T) {
is := assert.New(t)

result1 := Map[int, string]([]int{1, 2, 3, 4}, func(x int) string {
result1 := Map[int, string]([]int{1, 2, 3, 4}, func(x int, _ int) string {
return "Hello"
})
result2 := Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64) string {
result2 := Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
return strconv.FormatInt(x, 10)
})

Expand All @@ -42,10 +42,10 @@ func TestMap(t *testing.T) {
func TestReduce(t *testing.T) {
is := assert.New(t)

result1 := Reduce[int, int]([]int{1, 2, 3, 4}, func(agg int, item int) int {
result1 := Reduce[int, int]([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int {
return agg + item
}, 0)
result2 := Reduce[int, int]([]int{1, 2, 3, 4}, func(agg int, item int) int {
result2 := Reduce[int, int]([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int {
return agg + item
}, 10)

Expand Down

0 comments on commit 60559e1

Please sign in to comment.