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

Odd behaviour of Sequence.all and Sequence.any #893

Closed
clsource opened this issue Dec 27, 2020 · 5 comments
Closed

Odd behaviour of Sequence.all and Sequence.any #893

clsource opened this issue Dec 27, 2020 · 5 comments

Comments

@clsource
Copy link

There is something odd in Sequence.all and Sequence.any.

if (!result) return result

The docs says https://wren.io/modules/core/sequence.html

Iterates over the sequence, passing each element to the function predicate. If it returns something false, stops iterating and returns the value. Otherwise, returns true.

  all(f) {
    var result = true
    for (element in this) {
      result = f.call(element)
      if (!result) return result
    }
    return result
  }

By "If it returns something false, stops iterating and returns the value" should be if (!result) return element instead of result?

@mhermier
Copy link
Contributor

mhermier commented Dec 27, 2020 via email

@clsource
Copy link
Author

So the behaviour is correct, but the docs need improvement

@mhermier
Copy link
Contributor

Yes. The behavior you describe is most likely related to something like a find method (which does not exist and would need a PR) that would return an iterator on the Sequence.

@clsource
Copy link
Author

clsource commented Dec 27, 2020

@ruby0x1 told that PR #803 is related.

Would be this wording more inline with the actual behaviour?

all(predicate)

Tests whether all the elements in the sequence pass the predicate.

Iterates over the sequence, passing each element to the function predicate. If it returns something false, stops iterating and returns false. Otherwise, returns true.

any(predicate)

Tests whether any element in the sequence passes the predicate.

Iterates over the sequence, passing each element to the function predicate. If it returns something true, stops iterating and returns true. Otherwise, returns false.

find

Maybe the find method would be similar to

find(predicate) { WhereSequence.new(this, predicate).take(1) }

@mhermier
Copy link
Contributor

Even if more more valid than before, the statement is technically wrong, the predicate only needs to return truthy/falsy values. This was made so that predicate could also act as/work with a map function.

Don't add find documentation, I handled it in the push request I issued.

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

Successfully merging a pull request may close this issue.

2 participants