Skip to content

Commit

Permalink
Merge pull request #153 from ethahae/master
Browse files Browse the repository at this point in the history
ForEach slice modify(optional)
  • Loading branch information
thoas committed Dec 26, 2022
2 parents e7efff4 + 8de891d commit 42808ee
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.rst
Expand Up @@ -601,12 +601,18 @@ funk.ForEach

Range over an iteratee (map, slice).

Or update element in slice(Not map, reflect#Value#MapIndex#CanSet is false).

.. code-block:: go
funk.ForEach([]int{1, 2, 3, 4}, func(x int) {
fmt.Println(x)
})
foo := []int{1,2,3}
funk.ForEach(foo, func(x *int){ *x = *x * 2})
fmt.Println(foo) // []int{2, 4, 6}
funk.ForEachRight
............

Expand Down
21 changes: 17 additions & 4 deletions scan.go
Expand Up @@ -25,14 +25,20 @@ func ForEach(arr interface{}, predicate interface{}) {
}

arrElemType := arrValue.Type().Elem()
arrElemPointerType := reflect.New(arrElemType).Type()
usePointer := arrElemPointerType.ConvertibleTo(funcType.In(0))

// Checking whether element type is convertible to function's first argument's type.
if !arrElemType.ConvertibleTo(funcType.In(0)) {
if !arrElemType.ConvertibleTo(funcType.In(0)) && !usePointer {
panic("Map function's argument is not compatible with type of array.")
}

for i := 0; i < arrValue.Len(); i++ {
funcValue.Call([]reflect.Value{arrValue.Index(i)})
if usePointer {
funcValue.Call([]reflect.Value{arrValue.Index(i).Addr()})
} else {
funcValue.Call([]reflect.Value{arrValue.Index(i)})
}
}
}

Expand Down Expand Up @@ -79,14 +85,21 @@ func ForEachRight(arr interface{}, predicate interface{}) {
}

arrElemType := arrValue.Type().Elem()
arrElemPointerType := reflect.New(arrElemType).Type()
usePointer := arrElemPointerType.ConvertibleTo(funcType.In(0))

// Checking whether element type is convertible to function's first argument's type.
if !arrElemType.ConvertibleTo(funcType.In(0)) {
if !arrElemType.ConvertibleTo(funcType.In(0)) && !usePointer {
panic("Map function's argument is not compatible with type of array.")
}

for i := arrValue.Len() - 1; i >= 0; i-- {
funcValue.Call([]reflect.Value{arrValue.Index(i)})
if usePointer {
funcValue.Call([]reflect.Value{arrValue.Index(i).Addr()})
} else {
funcValue.Call([]reflect.Value{arrValue.Index(i)})
}

}
}

Expand Down
34 changes: 34 additions & 0 deletions scan_test.go
Expand Up @@ -19,6 +19,23 @@ func TestForEach(t *testing.T) {

is.Equal(results, []int{2, 4})

toModify := []int{1, 2, 3}
ForEach(toModify, func(x *int) { *x = *x * 2 })

is.Equal(toModify, []int{2, 4, 6})

toModify = []int{}
ForEach(toModify, func(x *int) {})

is.Equal(toModify, []int{})

strModify := []string{"a", "b"}
ForEach(strModify, func(s *string) {
*s = *s + *s
})

is.Equal(strModify, []string{"aa", "bb"})

mapping := map[int]string{
1: "Florent",
2: "Gilles",
Expand All @@ -40,6 +57,23 @@ func TestForEachRight(t *testing.T) {

is.Equal(results, []int{8, 6, 4, 2})

toModify := []int{1, 2, 3}
ForEach(toModify, func(x *int) { *x = *x * 2 })

is.Equal(toModify, []int{2, 4, 6})

toModify = []int{}
ForEach(toModify, func(x *int) {})

is.Equal(toModify, []int{})

strModify := []string{"a", "b"}
ForEach(strModify, func(s *string) {
*s = *s + *s
})

is.Equal(strModify, []string{"aa", "bb"})

mapping := map[int]string{
1: "Florent",
2: "Gilles",
Expand Down

0 comments on commit 42808ee

Please sign in to comment.