golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.13k stars 17.46k forks source link

proposal: iter: convert between Seq and Seq2 #67334

Open leaxoy opened 3 months ago

leaxoy commented 3 months ago

Proposal Details

Recently we introduce iter package with two Seq types: Seq and Seq2.

Writing operations on both types are annoying and repetitive.

So I propose add function to convert Seq to Seq2 and Seq2 to Seq

Possible function signatures:

func Join[K, V, R any](s Seq2[K, V], f func(K, V) R) Seq[R] {
    return func(yield func(R) bool) {
        for k, v := range s {
            if !yield(f(k, V)) {
               break
            }
        }
    }
}

func Split[R, K, V any](s Seq[R], f func(R) (K, V)) Seq2[K, V] {
    return func(yield func(K, V) bool) {
        for x := range s {
            if !yield(f(x)) {
                break
            }
        }
    }
}
jimmyfrasche commented 3 months ago

These are in the xiter thread as Map21 and Map12 https://github.com/golang/go/issues/61898#issuecomment-1683969720

leaxoy commented 3 months ago

These are in the xiter thread as Map21 and Map12 #61898 (comment)

My personal thought is that this conversion is crucial for developers who write fundamental libraries, and placing it in xiter requires the basic library to introduce an additional package. In many cases, it is expected that only import the std.

jbduncan commented 3 months ago

Here's my two cents: Zip (Seq2 to Seq) and Unzip (Seq to Seq2). But Map21 and Map12 sound great to me too.

jbduncan commented 3 months ago

Oh, shoot, never mind, Zip is already taken by proposal https://github.com/golang/go/issues/61898.