BooleanCat / go-functional

go-functional is a library of iterators to augment the standard library
MIT License
407 stars 23 forks source link

[Feature 🔨]: Transform iterator #74

Closed BooleanCat closed 1 year ago

BooleanCat commented 1 year ago

Is your feature request related to a problem? Please describe.

It's not possible to chain Map iterators because of limitations with Go's type system (you cannot define new type parameters on methods). So this is not possible:

func (iter *BaseIter[T]) Transform[U any](func(T) U) *MapIter[T, U] {
  ...
}

Describe the solution you'd like

I'd like a more limited version of the Map iterator where the return type is the same as the input type.

Provide code snippets to show how this new feature might be used.

The following should be possible and will allow chaining on simpler Map operations.

func (iter *BaseIter[T]) Transform(func(T) T) *TransformIter[T] {
  ...
}
evens := iter.Count().Transform(double).Take(5).Collect()

Does this incur a breaking change?

No.

Do you intend to build this feature yourself?

Maybe, be a good one for first time contributors to pick up.

BooleanCat commented 1 year ago

You may be able o short cut this by defining the Transform function as returning MapIter[T, T] and avoid writing a whole new iterator