Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ForEach slice modify(optional) #153

Merged
merged 1 commit into from Dec 26, 2022
Merged

ForEach slice modify(optional) #153

merged 1 commit into from Dec 26, 2022

Conversation

ethahae
Copy link

@ethahae ethahae commented May 25, 2022

Maybe ForEach can modify elements is a good idea, I don't know. But some real scene need this feature.
Someone prefers
results := []int{}
funk.ForEach([]int{1,2,3}, func(x int){ results = append(results, x * 2})
resultsAnother := []int{}
funk.ForEach(results, func(x int){ resultsAnother = append(resultsAnother, x * 3})
but someone prefers
data := []int{1,2,3}
funk.ForEach(data, func(x *int){ *x = *x * 2})
funk.ForEach(data, func(x *int){ *x = *x * 3})

go's map cannot be modified by the reflect value. reflect#Value#MapIndex#CanSet is false. Use SetMapIndex is a way, but it need a func(x *any) any as predicate func breaks go-funk's consensual, so I left it.

@thoas thoas merged commit 42808ee into thoas:master Dec 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants