-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add iterator support #5
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
lucarin91
wants to merge
6
commits into
bugst:master
Choose a base branch
from
lucarin91:add-iter-func
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
afb344a
feat: add iterator support
lucarin91 a6c3e4a
add RefIter utility
lucarin91 17e3717
Merge branch 'master' into add-iter-func
lucarin91 d64e679
fix spaces
lucarin91 488b794
add iter tests
lucarin91 b5fccdb
add tests
lucarin91 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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| module go.bug.st/f | ||
|
|
||
| go 1.22.3 | ||
| go 1.25 | ||
|
|
||
| require github.com/stretchr/testify v1.9.0 | ||
|
|
||
|
|
||
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 @@ | ||||||||
| package f | ||||||||
|
|
||||||||
| import "iter" | ||||||||
|
|
||||||||
| // FilterIter takes an iterator and a matcher and returns only those elements that satisfy the matcher. | ||||||||
|
Member
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.
Suggested change
|
||||||||
| func FilterIter[T any](values iter.Seq[T], matcher Matcher[T]) iter.Seq[T] { | ||||||||
| return func(yield func(x T) bool) { | ||||||||
| for x := range values { | ||||||||
| if matcher(x) { | ||||||||
| if !yield(x) { | ||||||||
| return | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // Map applies the Mapper function to each element of the iterator. | ||||||||
|
Member
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.
Suggested change
|
||||||||
| func MapIter[T, U any](values iter.Seq[T], mapper Mapper[T, U]) iter.Seq[U] { | ||||||||
| return func(yield func(x U) bool) { | ||||||||
| for x := range values { | ||||||||
| if !yield(mapper(x)) { | ||||||||
| return | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // Reducer is a function that reduces an iterator's elements to a single value. | ||||||||
|
Member
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.
Suggested change
|
||||||||
| func ReduceIter[T any](values iter.Seq[T], reducer Reducer[T], initialValue ...T) T { | ||||||||
| var result T | ||||||||
| if len(initialValue) > 1 { | ||||||||
| panic("initialValue must be a single value") | ||||||||
| } else if len(initialValue) == 1 { | ||||||||
| result = initialValue[0] | ||||||||
| } | ||||||||
| for v := range values { | ||||||||
| result = reducer(result, v) | ||||||||
| } | ||||||||
| return result | ||||||||
| } | ||||||||
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,117 @@ | ||
| package f | ||
|
|
||
| import ( | ||
| "slices" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestFilterIter(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input []int | ||
| matcher Matcher[int] | ||
| expected []int | ||
| }{ | ||
| { | ||
| name: "even numbers", | ||
| input: []int{1, 2, 3, 4, 5}, | ||
| matcher: func(x int) bool { return x%2 == 0 }, | ||
| expected: []int{2, 4}, | ||
| }, | ||
| { | ||
| name: "odd numbers", | ||
| input: []int{1, 2, 3, 4, 5}, | ||
| matcher: func(x int) bool { return x%2 == 1 }, | ||
| expected: []int{1, 3, 5}, | ||
| }, | ||
| { | ||
| name: "none", | ||
| input: []int{2, 4, 6}, | ||
| matcher: func(x int) bool { return x < 0 }, | ||
| expected: nil, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| values := slices.Values(tt.input) | ||
| result := slices.Collect(FilterIter(values, tt.matcher)) | ||
| assert.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestMapIter(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input []int | ||
| mapper Mapper[int, int] | ||
| expected []int | ||
| }{ | ||
| { | ||
| name: "double", | ||
| input: []int{1, 2, 3}, | ||
| mapper: func(x int) int { return x * 2 }, | ||
| expected: []int{2, 4, 6}, | ||
| }, | ||
| { | ||
| name: "negate", | ||
| input: []int{1, -2, 3}, | ||
| mapper: func(x int) int { return -x }, | ||
| expected: []int{-1, 2, -3}, | ||
| }, | ||
| { | ||
| name: "identity", | ||
| input: []int{1, 2, 3}, | ||
| mapper: func(x int) int { return x }, | ||
| expected: []int{1, 2, 3}, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| values := slices.Values(tt.input) | ||
| result := slices.Collect(MapIter(values, tt.mapper)) | ||
| assert.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestReduceIter(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input []int | ||
| reducer Reducer[int] | ||
| initialValue int | ||
| expected int | ||
| }{ | ||
| { | ||
| name: "sum", | ||
| input: []int{1, 2, 3, 4}, | ||
| reducer: func(a, b int) int { return a + b }, | ||
| initialValue: 0, | ||
| expected: 10, | ||
| }, | ||
| { | ||
| name: "product", | ||
| input: []int{1, 2, 3, 4}, | ||
| reducer: func(a, b int) int { return a * b }, | ||
| initialValue: 1, | ||
| expected: 24, | ||
| }, | ||
| { | ||
| name: "subtract", | ||
| input: []int{10, 2, 3}, | ||
| reducer: func(a, b int) int { return a - b }, | ||
| initialValue: 20, | ||
| expected: 5, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| values := slices.Values(tt.input) | ||
| result := ReduceIter(values, tt.reducer, tt.initialValue) | ||
| assert.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } |
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
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
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.
Missing license header