From f22d41573504e23ddc79c6edbc882ddb09531804 Mon Sep 17 00:00:00 2001 From: apatniv Date: Sat, 29 Aug 2026 08:14:14 -0400 Subject: [PATCH 1/2] Add Map on itx and deprecate Transform --- it/itx/map.go | 12 +++++++++--- it/itx/map_go_127.go | 20 ++++++++++++++++++++ it/itx/map_go_127_test.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 it/itx/map_go_127.go create mode 100644 it/itx/map_go_127_test.go diff --git a/it/itx/map.go b/it/itx/map.go index 65ff037..db3c79b 100644 --- a/it/itx/map.go +++ b/it/itx/map.go @@ -6,7 +6,9 @@ import "github.com/BooleanCat/go-functional/v2/it" // the provided functions argument type is the same as its return type. // // This is a limited version of [it.Map] due to a limitation on Go's type -// system whereby new generic type parameters cannot be defined on methods. +// system before Go 1.27 whereby new generic type parameters cannot be defined on methods. +// +// Deprecated: This will be removed in later versions and clients should transition to [Iterator.Map]. func (iterator Iterator[V]) Transform(f func(V) V) Iterator[V] { return Iterator[V](it.Map(iterator, f)) } @@ -15,7 +17,9 @@ func (iterator Iterator[V]) Transform(f func(V) V) Iterator[V] { // where the provided functions argument type is the same as its return type. // // This is a limited version of [it.Map2] due to a limitation on Go's type -// system whereby new generic type parameters cannot be defined on methods. +// system before Go 1.27 whereby new generic type parameters cannot be defined on methods. +// +// Deprecated: This will be removed in later versions and clients should transition to [Iterator2.Map]. func (iterator Iterator2[V, W]) Transform(f func(V, W) (V, W)) Iterator2[V, W] { return Iterator2[V, W](it.Map2(iterator, f)) } @@ -25,7 +29,9 @@ func (iterator Iterator2[V, W]) Transform(f func(V, W) (V, W)) Iterator2[V, W] { // return type. // // This is a limited version of [it.MapError] due to a limitation on Go's type -// system whereby new generic type parameters cannot be defined on methods. +// system before Go 1.27 whereby new generic type parameters cannot be defined on methods. +// +// Deprecated: This will be removed in later versions and clients should transition to [Iterator.MapError]. func (iterator Iterator[V]) TransformError(f func(V) (V, error)) Iterator2[V, error] { return Iterator2[V, error](it.MapError(iterator, f)) } diff --git a/it/itx/map_go_127.go b/it/itx/map_go_127.go new file mode 100644 index 0000000..aa82ee8 --- /dev/null +++ b/it/itx/map_go_127.go @@ -0,0 +1,20 @@ +//go:build go1.27 + +package itx + +import "github.com/BooleanCat/go-functional/v2/it" + +// Map is a convenience method for chaining [it.Map] on [Iterator]s. +func (iterator Iterator[V]) Map[W any](f func(V) W) Iterator[W] { + return Iterator[W](it.Map(iterator, f)) +} + +// Map is a convenience method for chaining [it.Map2] on [Iterator2]s. +func (iterator Iterator2[V, W]) Map[X, Y any](f func(V, W) (X, Y)) Iterator2[X, Y] { + return Iterator2[X, Y](it.Map2(iterator, f)) +} + +// MapError is a convenience method for chaining [it.MapError] on [Iterator]s. +func (iterator Iterator[V]) MapError[W any](f func(V) (W, error)) Iterator2[W, error] { + return Iterator2[W, error](it.MapError(iterator, f)) +} diff --git a/it/itx/map_go_127_test.go b/it/itx/map_go_127_test.go new file mode 100644 index 0000000..f6c20e3 --- /dev/null +++ b/it/itx/map_go_127_test.go @@ -0,0 +1,34 @@ +//go:build go1.27 + +package itx_test + +import ( + "fmt" + "maps" + + "github.com/BooleanCat/go-functional/v2/it" + "github.com/BooleanCat/go-functional/v2/it/itx" +) + +func ExampleIterator_Map() { + fmt.Println(itx.FromSlice([]string{"", "a", "aa"}).Map(func(v string) int { + return len(v) + }).Collect()) + // Output: [0 1 2] +} + +func ExampleIterator2_Map() { + count := func(a, b string) (string, int) { + return a, len(b) + } + + fmt.Println(maps.Collect(itx.FromMap(map[string]string{"k1": "a", "k2": "aa"}).Map(count).Seq())) + // Output: map[k1:1 k2:2] +} + +func ExampleIterator_MapError() { + fmt.Println(it.TryCollect(itx.FromSlice([]string{"", "a", "aa"}).MapError(func(v string) (int, error) { + return len(v), nil + }))) + // Output: [0 1 2] +} From 636d197737fb2bf2263706442cb2e1c8befaa7e4 Mon Sep 17 00:00:00 2001 From: apatniv Date: Sat, 5 Sep 2026 00:22:23 -0400 Subject: [PATCH 2/2] Update README --- README.md | 53 ++++++++++++----------------------------------------- 1 file changed, 12 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 07ba523..bec2101 100644 --- a/README.md +++ b/README.md @@ -683,8 +683,7 @@ for line, err := range it.LinesString(buffer) {

Map & Transform

-> [!NOTE] -> The `itx` package does not contain `Map` due to limitations with Go's type system. Instead a -> limited from of `Map` called `Transform` is provided where the type returned from operations is -> the same as a type of the iterator's values. -> -> A chainable Map will be added should Go's type system ever support new generic type parameters on -> methods. - - -> [!TIP] -> If you wish to chain operations on `Map`, you can do so by first converting it to an -> `itx.Iterator` like so: -> -> ```go -> itx.From(it.Map(slices.Values([]int{1, 2, 3}), double)).Collect() -> ``` +- +> [!WARNING] +> Transform will be removed in later versions and clients should transition to Map. ### MapError & TransformError @@ -731,27 +716,13 @@ double := func(n int, err error) int { return n * 2, nil } it.MapError(slices.Values([]int{1, 2, 3}), double) -// Limited chainable flavour of MapError -itx.FromSlice([]int{1, 2, 3}).TransformError(double) +// chainable flavour of MapError +itx.FromSlice([]int{1, 2, 3}).MapError(double) ``` - -> [!NOTE] -> The `itx` package does not contain `MapError` due to limitations with Go's type system. Instead a -> limited from of `MapError` called `TransformError` is provided where the type returned from -> operations is the same as a type of the iterator's values. -> -> A chainable MapError will be added should Go's type system ever support new generic type -> parameters on methods. - - -> [!TIP] -> If you wish to chain operations on `MapError`, you can do so by first converting it to an -> `itx.Iterator2` like so: -> -> ```go -> itx.From2(it.MapError(slices.Values([]int{1, 2, 3}), double)).Collect() -> ``` +- +> [!WARNING] +> TransformError will be removed in later versions and clients should transition to MapError. ### NaturalNumbers