Skip to content

Commit

Permalink
Support Map in itx to allow limited chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
BooleanCat committed Jul 20, 2024
1 parent e8c6305 commit 9e5dba8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions it/itx/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package itx

import "github.com/BooleanCat/go-functional/v2/it"

// Map is a convenience method [it.Map] returning an [Iterator] to support
// chaining.
func Map[V any, W any](iterator func(func(V) bool), f func(V) W) Iterator[W] {
return Iterator[W](it.Map(iterator, f))
}

// Transform is a convenience method for chaining [it.Map] on [Iterator]s where
// the provided functions argument type is the same as its return type.
//
Expand Down
8 changes: 8 additions & 0 deletions it/itx/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ package itx_test
import (
"fmt"
"maps"
"slices"

"github.com/BooleanCat/go-functional/v2/it/itx"
)

func ExampleMap() {
fmt.Println(itx.Map(slices.Values([]int{0, 1, 2}), func(v int) int {
return v + 1
}).Collect())
// Output: [1 2 3]
}

func ExampleIterator_Transform() {
fmt.Println(itx.FromSlice([]int{0, 1, 2}).Transform(func(v int) int {
return v + 1
Expand Down

0 comments on commit 9e5dba8

Please sign in to comment.