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 {All,Any,None}{,2} #67453

Open
leaxoy opened this issue May 17, 2024 · 5 comments
Open

proposal: x/exp/xiter: add {All,Any,None}{,2} #67453

leaxoy opened this issue May 17, 2024 · 5 comments
Labels
Milestone

Comments

@leaxoy
Copy link

leaxoy commented May 17, 2024

Proposal Details

Proposed to add below funcitons:

-- func Fold[A, E any](s Seq[E], init A, f func(A, E) A) A--
// Remove Fold because original proposal's `Reduce` takes an initial value.

func All[E any](s Seq[E], f func(E) bool) bool {
    return Fold(s, true, func(x bool, e E) bool { return x && f(e) })
}

func Any[E any](s Seq[E], f func(E) bool) bool {
    return ...
}

func None[E any](s Seq[E], f func(E) bool) bool {
    return ...
}

Same as seq2.

Different from reduce, fold needs to provide an initial value

@gopherbot gopherbot added this to the Proposal milestone May 17, 2024
@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals May 18, 2024
@jimmyfrasche
Copy link
Member

None is just !Any. Does not seem worth it.

Presumably these would short circuit since Any can stop when it gets a hit and All can stop when it gets a miss. Is that correct?

Should this be Any[B ~bool](iter.Seq[B]) B and AnyFunc[E any ](iter.Seq[E], func(E) bool? Any2 wouldn't make sense in this scheme but AnyFunc2 would.

(and likewise for All)

@adonovan
Copy link
Member

adonovan commented Jul 22, 2024

Because none of these functions declare type parameters beyond those of Seq or Seq2, they could be expressed as methods of the named types Seq and Seq2:

package iter

// All reports whether all elements of the sequence satisfy the predicate f.
func (Seq[E]) All(f func(E) bool) bool
func (Seq[E]) Any(f func(E) bool) bool // shorthand for !All(Negate(f))
func (Seq[E]) None(f func(E) bool) bool // shorthand for !Any(f)

@leaxoy
Copy link
Author

leaxoy commented Jul 23, 2024

Because none of these functions declare type parameters beyond those of Seq or Seq2, they could be expressed as methods of the named types Seq and Seq2:

package iter

// All reports whether all elements of the sequence satisfy the predicate f.
func (Seq[E]) All(f func(E) bool) bool
func (Seq[E]) Any(f func(E) bool) bool // shorthand for !All(Negate(f))
func (Seq[E]) None(f func(E) bool) bool // shorthand for !Any(f)

If Seq is a type alias as mentioned in the original proposal, we cannot add methods on it.

@adonovan
Copy link
Member

If Seq is a type alias as mentioned in the original proposal, we cannot add methods on it.

iter.Seq is not an alias; generic aliases are not even part of the go1.23 spec. The discussion about whether it should have been an alias is just words after the fact.

@adonovan adonovan changed the title proposal: x/exp/xiter: add Fold/All/Any/None{2} proposal: x/exp/xiter: add {All,Any,None}{,2} Jul 23, 2024
@adamroyjones
Copy link

adamroyjones commented Aug 19, 2024

I'm here from #68945. (Thanks for connecting the two @seankhliao—I didn't see this when searching.)

Given the existence of slices.Contains and slices.ContainsFunc, we could alternatively have (for consistency)

func Contains[E comparable](seq Seq[E], e E) bool
func ContainsFunc[E any](seq Seq[E], func(e E) bool) bool
  • Any is equivalent to ContainsFunc.
  • None is the negation of Any.
  • All(seq, f) is the same as None(seq, !f) which is the same as !Any(seq, !f).

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