go-functional icon indicating copy to clipboard operation
go-functional copied to clipboard

[Feature 🔨]: Add `iter.Find`

Open MSE99 opened this issue 3 years ago • 2 comments

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

MSE99 avatar Jul 19 '22 11:07 MSE99

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().

BooleanCat avatar Jul 21 '22 21:07 BooleanCat

I think having a dedicated Find will lead to more readable code but it's totally okay if it's not doable.

MSE99 avatar Jul 22 '22 20:07 MSE99

@MSE99 This is done and is being released with version v0.10.0. Thanks for the idea.

BooleanCat avatar Aug 10 '23 11:08 BooleanCat