Bases: Provide all() and any() methods for lists

Use case or problem

Testing all elements of a list against a predicate requires writing long, complicated chains of methods that are inefficient (because they usually require building intermediate lists and/or don’t shortcut).

Proposed solution

Introduce list.all(expression) and list.any(expression)that evaluate the given expression against the elements in the list and return true if the expression is true of all, or, respectively, at least one element in the list. Said methods should return early as soon as their result cannot change (all() returns false as soon as an element evaluates false; any() returns true as soon as an element evaluates true.

Current workaround (optional)

Longer, inefficient method chains that don’t shortcut and are less intuitive can be used.

1 Like

What would be the difference compared to .map() and .filter() ?

I would suggest they be named every() and some() to match the actual JavaScript Array methods.

.map() and .filter() return a list, whereas the functions I propose return a single boolean.

You can implement those functions in terms of .map() or .filter() with some extra work, but it’s both more verbose and less efficient.

I think you are missing .reduce().

I am well aware of .reduce(). Again, it’s more verbose, and it doesn’t shortcut.

New base functions are a nice-to-have, but for me this use case is already well covered by the current set of functions without becoming significantly more verbose. Personally, I’d rather see other things prioritized.