-
-
Notifications
You must be signed in to change notification settings - Fork 942
add SliceToSeq2 #776
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
base: master
Are you sure you want to change the base?
add SliceToSeq2 #776
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| module github.com/samber/lo | ||
|
|
||
| go 1.18 | ||
| go 1.25 | ||
|
|
||
| // | ||
| // Dev dependencies are excluded from releases. Please check CI. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,32 @@ | ||
| package lo | ||
|
|
||
| import ( | ||
| "iter" | ||
| "sort" | ||
|
|
||
| "github.com/samber/lo/internal/constraints" | ||
| "github.com/samber/lo/mutable" | ||
| ) | ||
|
|
||
| // SliceToSeq2 converts a slice into a sequence of pairs (key, value). | ||
| // Elements are paired consecutively: s[0] with s[1], s[2] with s[3], etc. | ||
| // If the slice has an odd number of elements, the last element is paired with the zero value of type E. | ||
| func SliceToSeq2[Slice ~[]E, E any](collection Slice) iter.Seq2[E, E] { | ||
|
Owner
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 would rename it See the Please write 2 implementations:
Using iter.Seq2 does not seem a good idea here, since first argument of Seq2 is expected to be a key. I would use |
||
| return func(yield func(E, E) bool) { | ||
| n := len(collection) | ||
| var zero E | ||
| for i := 0; i < n; i += 2 { | ||
| v := zero | ||
| if i+1 < n { | ||
| v = collection[i+1] | ||
| } | ||
| if !yield(collection[i], v) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Filter iterates over elements of collection, returning a slice of all elements predicate returns true for. | ||
| // Play: https://go.dev/play/p/Apjg3WeSi7K | ||
| func Filter[T any, Slice ~[]T](collection Slice, predicate func(item T, index int) bool) Slice { | ||
|
|
||
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.
Please revert. I try to keep a good compatiibility with older go versions.