Skip to content

feat: range-over-func POC#439

Closed
NathanBaulch wants to merge 1 commit into
samber:masterfrom
NathanBaulch:rangefunc
Closed

feat: range-over-func POC#439
NathanBaulch wants to merge 1 commit into
samber:masterfrom
NathanBaulch:rangefunc

Conversation

@NathanBaulch

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

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

@samber samber left a comment

Copy link
Copy Markdown
Owner

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 ?

Comment thread iterator.go

// 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
Copy Markdown
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.

Comment thread iterator.go
type Iterator[V any] func(func(int, V) bool)

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

Copy link
Copy Markdown

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)

@NathanBaulch

Copy link
Copy Markdown
Contributor Author

Implemented in #672

@NathanBaulch NathanBaulch deleted the rangefunc branch October 2, 2025 21:35
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