go-functional
go-functional copied to clipboard
[Feature 🔨]: Add `iter.Find`
Is your feature request related to a problem? Please describe.
Add function Find to iter package.
Describe the solution you'd like
iter package should have a Find function that takes an iterator and a predicate, Find will
repeatedly call the underlying iterator and test the returned values using the predicate, if the
predicate returns true then the value is returned wrapped in a Some variant if the underlying
iterator is exhausted option.None is returned.
Provide code snippets to show how this new feature might be used.
package main
import "github.com/BooleanCat/go-functional/iter"
func main() {
isFour := func (x int) bool { return x == 4 }
isHundred = func (x int) bool { return x == 100 }
numbers := []int{1, 2, 3, 4, 5}
fmt.Println(iter.Find(iter.Lift(numbers)), isFour) // Some(4)
fmt.Println(iter.Find(iter.Lift(numbers)), isHundred) // None
}
Does this incur a breaking change?
No
Do you intend to build this feature yourself?
Yes
Thanks for the suggestion.
I'm in two minds about this, I can see this being used, however I'm not sure it's worth adding since users could achieve the same thing with iter.Filter(numbers).Next().
I think having a dedicated Find will lead to more readable code but it's totally okay if it's not doable.
@MSE99 This is done and is being released with version v0.10.0. Thanks for the idea.