Skip to content

add SliceToSeq2#776

Open
yangjinheng wants to merge 1 commit intosamber:masterfrom
yangjinheng:master
Open

add SliceToSeq2#776
yangjinheng wants to merge 1 commit intosamber:masterfrom
yangjinheng:master

Conversation

@yangjinheng
Copy link

a great method

// 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] {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename it Pairwise.

See the samber/ro implem: https://ro.samber.dev/docs/operator/combining#pairwise

Please write 2 implementations:

  • core package -> returns [][]T or []T2[T, T]
  • it/ package -> returns iter.Seq[[]T] or iter.Seq[T2[T, T]]

Using iter.Seq2 does not seem a good idea here, since first argument of Seq2 is expected to be a key.

I would use T2 tuples instead of simple slices []T.

module github.com/samber/lo

go 1.18
go 1.25
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants