Skip to content
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

proposal: x/exp/xiter: add Nop, Nop2, Value, Value2 #68947

Open
earthboundkid opened this issue Aug 19, 2024 · 4 comments
Open

proposal: x/exp/xiter: add Nop, Nop2, Value, Value2 #68947

earthboundkid opened this issue Aug 19, 2024 · 4 comments
Labels
Milestone

Comments

@earthboundkid
Copy link
Contributor

earthboundkid commented Aug 19, 2024

Proposal Details

This could probably go in golang.org/x/exp/xiter for now, but it should also be considered for plain package iter.

Nop yields an empty (no op) sequence. Nop2 yields an empty iter.Seq2.

// Value return an iter.Seq yielding the single value v.
func Value[T any](v T) iter.Seq[T] {
    return func(yield func(T) bool) {
        _ = yield(v)
    }
}

Value2 isn't a great name, but seems the most straightforward.

See #68931, #65629 and I forget where iter.Value first came up.

@gopherbot gopherbot added this to the Proposal milestone Aug 19, 2024
@seankhliao seankhliao changed the title proposal: iter: add Nop, Nop2, Value, Value2 proposal: x/exp/xiter: add Nop, Nop2, Value, Value2 Aug 19, 2024
@timothy-king
Copy link
Contributor

timothy-king commented Aug 19, 2024

Quick idea is to generalize to +0 elements. A func Values[T any](v ...T) iter.Seq[T] seems like it could be a decent primitive. Especially for testing/always initializing an iter.Seq. It would cover Nop and Value. Significant overlap with slices.All though.

I am not sure there is a good matching generalization for +0 pairs. maps.All is an okay [but imperfect] substitute for Nop2 and Value2. It requires comparable, and looses order with more elements. Not clear how much these restrictions matter though.

@jimmyfrasche
Copy link
Member

In my experiments with iterators I called these (type parameters and packages elided for brevity) Just(T) Seq[T], Just2(K, V) seq2[K, V], and From(...T) Seq[T]. Instead of Nop I went with None. These are all useful in tests and examples. None/None2 in particular kept popping up when implementing other iterators.

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Aug 19, 2024
@earthboundkid
Copy link
Contributor Author

earthboundkid commented Aug 19, 2024

xiter has Concat, which can be used for Nop by just doing xiter.Concat[T](). It's not as obvious as it could be though.

@josharian
Copy link
Contributor

Our xiter package contains:

// One returns an iterator that yields the single element x.
func One[T any](x T) iter.Seq[T]

// Empty returns an iterator that yields no values.
func Empty[T any]() iter.Seq[T]

Just more evidence that these are widespread and useful.

Looking through our uses, a single variadic function as proposed by @timothy-king would work fine as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

5 participants