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

feat: range-over-func POC #439

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

NathanBaulch
Copy link
Contributor

I was surprised to see no mention of the upcoming range-over-func experimental feature in the issues/PR list. I haven't measured anything but in theory this feature could dramatically reduce slice allocations when composing "queries" over large collections since no intermediate slices need to be created.

Is there any interest in this? This POC PR demonstrates what iterator support might look like, including tests and examples. I went with an "I" suffix (to mirror the "F" convention) to differentiate the iterator functions from their slice.go counterparts.

For example:

iter := lo.ToIterator(1, 2, 3, 4)
result := lo.FilterI(iter, func(nbr int, index int) bool { return nbr%2 == 0 })
fmt.Printf("%v", result.Slice())

@NathanBaulch
Copy link
Contributor Author

Probably worth thinking about if/how this would integrate with the new iter package in the stdlib.

Copy link
Owner

@samber samber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm considering a new "iter" subpackage.

samber/lo is widely used and upgrading to Go 1.22 seems rough.

I think we can keep the same naming for every helpers, but under samber/lo/iter.

WDYT ?


// MapI transforms an iterator into an iterator of another type.
// Play: https://go.dev/play/p/TODO
func MapI[T any, R any](iter Iterator[T], iteratee func(item T, index int) R) Iterator[R] {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, we should receive and return iterators.

type Iterator[V any] func(func(int, V) bool)

// ToIterator TODO
func ToIterator[V any](collection ...V) Iterator[V] {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToIterator might already be implemented in the go standard library as slices.All.

As per this comment: gomoni/it#12 (comment)

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.

3 participants