Iterable Class
Functional programming support for iterable classes. The class must
implement the iterator
and newInstance
methods.
For most methods, the iterator only needs to implement next
and
atEnd
. Backwards iterators like reduceRight
require prev
and
atBeginning
.
Iterable classes must mix these functions: `Y.mix(SomeClass, Y.Iterable, false, null, 4);` Passing false as the third argument allows your class to provide optimized implementations of individual functions.
Methods
each
-
f
-
c
Executes the supplied function on each item in the list. The function receives the value, the index, and the list itself as parameters (in that order).
Parameters:
-
f
Functionthe function to execute on each item
-
c
Objectoptional context object
every
-
f
-
c
Executes the supplied function on each item in the list. Iteration stops if the supplied function does not return a truthy value. The function receives the value, the index, and the list itself as parameters (in that order).
Parameters:
-
f
Functionthe function to execute on each item
-
c
Objectoptional context object
Returns:
true if every item in the array returns true from the supplied function, false otherwise
filter
-
f
-
c
Executes the supplied function on each item in the list. Returns a new list containing the items for which the supplied function returned a truthy value. The function receives the value, the index, and the object itself as parameters (in that order).
Parameters:
-
f
Functionthe function to execute on each item
-
c
Objectoptional context object
Returns:
list of items for which the supplied function returned a truthy value (empty if it never returned a truthy value)
find
-
f
-
c
Executes the supplied function on each item in the list, searching for the first item that matches the supplied function. The function receives the value, the index, and the object itself as parameters (in that order).
Parameters:
-
f
Functionthe function to execute on each item
-
c
Objectoptional context object
Returns:
the first item for which the supplied function returns true, or null if it never returns true
map
-
f
-
c
Executes the supplied function on each item in the list and returns a new list with the results. The function receives the value, the index, and the object itself as parameters (in that order).
Parameters:
-
f
Stringthe function to invoke
-
c
Objectoptional context object
Returns:
list of all return values
partition
-
f
-
c
Partitions an list into two new list, one with the items for which the supplied function returns true, and one with the items for which the function returns false. The function receives the value, the index, and the object itself as parameters (in that order).
Parameters:
-
f
Functionthe function to execute on each item
-
c
Objectoptional context object
Returns:
object with two properties: matches and rejects. Each is a list containing the items that were selected or rejected by the test function (or an empty object if none).
reduce
-
init
-
f
-
c
Executes the supplied function on each item in the list, folding the list into a single value. The function receives the value returned by the previous iteration (or the initial value if this is the first iteration), the value being iterated, the index, and the list itself as parameters (in that order). The function must return the updated value.
Parameters:
-
init
Mixedthe initial value
-
f
Stringthe function to invoke
-
c
Objectoptional context object
Returns:
final result from iteratively applying the given function to each item in the list
reduceRight
-
init
-
f
-
c
Executes the supplied function on each item in the list, starting from the end and folding the list into a single value. The function receives the value returned by the previous iteration (or the initial value if this is the first iteration), the value being iterated, the index, and the list itself as parameters (in that order). The function must return the updated value.
Parameters:
-
init
Mixedthe initial value
-
f
Stringthe function to invoke
-
c
Objectoptional context object
Returns:
final result from iteratively applying the given function to each item in the list
reject
-
f
-
c
Executes the supplied function on each item in the list. Returns a new list containing the items for which the supplied function returned a falsey value. The function receives the value, the index, and the object itself as parameters (in that order).
Parameters:
-
f
Functionthe function to execute on each item
-
c
Objectoptional context object
Returns:
array or object of items for which the supplied function returned a falsey value (empty if it never returned a falsey value)
some
-
f
-
c
Executes the supplied function on each item in the list. Iteration stops if the supplied function returns a truthy value. The function receives the value, the index, and the list itself as parameters (in that order).
Parameters:
-
f
Functionthe function to execute on each item
-
c
Objectoptional context object
Returns:
true if the function returns a truthy value on any of the items in the array, false otherwise