-
-
Notifications
You must be signed in to change notification settings - Fork 887
feat: range-over-func POC #439
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
Open
NathanBaulch
wants to merge
1
commit into
samber:master
Choose a base branch
from
NathanBaulch:rangefunc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//go:build goexperiment.rangefunc | ||
|
||
package lo | ||
|
||
// Iterator TODO | ||
type Iterator[V any] func(func(int, V) bool) | ||
|
||
// ToIterator TODO | ||
func ToIterator[V any](collection ...V) Iterator[V] { | ||
return func(yield func(int, V) bool) { | ||
for i, item := range collection { | ||
yield(i, item) | ||
} | ||
} | ||
} | ||
|
||
// Len TODO | ||
func (iter Iterator[V]) Len() int { | ||
var n int | ||
for _, _ = range iter { | ||
n++ | ||
} | ||
return n | ||
} | ||
|
||
// Slice TODO | ||
func (iter Iterator[V]) Slice() []V { | ||
var result []V | ||
for _, item := range iter { | ||
result = append(result, item) | ||
} | ||
return result | ||
} | ||
|
||
// FilterI returns an iterator of all elements that match the specified predicate callback. | ||
// Play: https://go.dev/play/p/TODO | ||
func FilterI[V any](iter Iterator[V], predicate func(item V, index int) bool) Iterator[V] { | ||
return func(yield func(int, V) bool) { | ||
for i, item := range iter { | ||
if predicate(item, i) { | ||
if !yield(i, item) { | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// 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] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, we should receive and return iterators. |
||
return func(yield func(int, R) bool) { | ||
for i, item := range iter { | ||
if !yield(i, iteratee(item, i)) { | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
// FilterMapI returns an iterator obtained after both filtering and mapping using the given callback function. | ||
// The callback function should return two values: | ||
// - the result of the mapping operation and | ||
// - whether the result element should be included or not. | ||
// | ||
// Play: https://go.dev/play/p/TODO | ||
func FilterMapI[T any, R any](iter Iterator[T], callback func(item T, index int) (R, bool)) Iterator[R] { | ||
return func(yield func(int, R) bool) { | ||
for i, item := range iter { | ||
if r, ok := callback(item, i); ok { | ||
if !yield(i, r) { | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//go:build goexperiment.rangefunc | ||
|
||
package lo | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
func ExampleFilterI() { | ||
iter := ToIterator(1, 2, 3, 4) | ||
|
||
result := FilterI(iter, func(nbr int, index int) bool { | ||
return nbr%2 == 0 | ||
}) | ||
|
||
fmt.Printf("%v", result.Slice()) | ||
// Output: [2 4] | ||
} | ||
|
||
func ExampleMapI() { | ||
iter := ToIterator(1, 2, 3, 4) | ||
|
||
result := MapI(iter, func(nbr int, index int) string { | ||
return strconv.FormatInt(int64(nbr)*2, 10) | ||
}) | ||
|
||
fmt.Printf("%v", result.Slice()) | ||
// Output: [2 4 6 8] | ||
} | ||
|
||
func ExampleFilterMapI() { | ||
iter := ToIterator(1, 2, 3, 4) | ||
|
||
result := FilterMapI(iter, func(nbr int, index int) (string, bool) { | ||
return strconv.FormatInt(int64(nbr)*2, 10), nbr%2 == 0 | ||
}) | ||
|
||
fmt.Printf("%v", result.Slice()) | ||
// Output: [4 8] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//go:build goexperiment.rangefunc | ||
|
||
package lo | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFilterI(t *testing.T) { | ||
t.Parallel() | ||
is := assert.New(t) | ||
|
||
r1 := FilterI(ToIterator(1, 2, 3, 4), func(x int, _ int) bool { | ||
return x%2 == 0 | ||
}) | ||
|
||
is.Equal(r1.Slice(), []int{2, 4}) | ||
|
||
r2 := FilterI(ToIterator("", "foo", "", "bar", ""), func(x string, _ int) bool { | ||
return len(x) > 0 | ||
}) | ||
|
||
is.Equal(r2.Slice(), []string{"foo", "bar"}) | ||
} | ||
|
||
func TestMapI(t *testing.T) { | ||
t.Parallel() | ||
is := assert.New(t) | ||
|
||
result1 := MapI(ToIterator(1, 2, 3, 4), func(x int, _ int) string { | ||
return "Hello" | ||
}) | ||
result2 := MapI(ToIterator(1, 2, 3, 4), func(x int, _ int) string { | ||
return strconv.FormatInt(int64(x), 10) | ||
}) | ||
|
||
is.Equal(result1.Len(), 4) | ||
is.Equal(result2.Len(), 4) | ||
is.Equal(result1.Slice(), []string{"Hello", "Hello", "Hello", "Hello"}) | ||
is.Equal(result2.Slice(), []string{"1", "2", "3", "4"}) | ||
} | ||
|
||
func TestFilterMapI(t *testing.T) { | ||
t.Parallel() | ||
is := assert.New(t) | ||
|
||
r1 := FilterMapI(ToIterator(1, 2, 3, 4), func(x int, _ int) (string, bool) { | ||
if x%2 == 0 { | ||
return strconv.FormatInt(int64(x), 10), true | ||
} | ||
return "", false | ||
}) | ||
r2 := FilterMapI(ToIterator("cpu", "gpu", "mouse", "keyboard"), func(x string, _ int) (string, bool) { | ||
if strings.HasSuffix(x, "pu") { | ||
return "xpu", true | ||
} | ||
return "", false | ||
}) | ||
|
||
is.Equal(r1.Len(), 2) | ||
is.Equal(r2.Len(), 2) | ||
is.Equal(r1.Slice(), []string{"2", "4"}) | ||
is.Equal(r2.Slice(), []string{"xpu", "xpu"}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 asslices.All
.As per this comment: gomoni/it#12 (comment)