- Method filter
Filters the elements in
arris treated as a set of elements to be filtered, as follows:
Each element is filtered with
fun. The return value is of the same type as
arrand it contains the elements that
funis applied in order to each element, and that order is retained between the kept elements.
funis an array, it should have the same length as
arr. In this case, the elements in
arrare kept where the corresponding positions in
funare nonzero. Otherwise
funis used as described below.
The values are filtered with
fun, and the index/value pairs it accepts are kept in the returned mapping.
The program is treated as a mapping containing the identifiers that are indexable from it and their values.
If there is a lfun::cast method in the object, it's called to try to cast the object to an array, a mapping, or a multiset, in that order, which is then filtered as described above.
Unless something else is mentioned above,
funis used as filter like this:
funis called for each element. It gets the current element as the first argument and
extraas the rest. The element is kept if it returns true, otherwise it's filtered out.
The object is used as a function like above, i.e. the lfun::`() method in it is called.
funis indexed with each element. The element is kept if the result is nonzero, otherwise it's filtered out.
- "zero or left out"
Each element that is callable is called with
extraas arguments. The element is kept if the result of the call is nonzero, otherwise it's filtered out. Elements that aren't callable are also filtered out.
Each element is indexed with the given string. If the result of that is zero then the element is filtered out, otherwise the result is called with
extraas arguments. The element is kept if the return value is nonzero, otherwise it's filtered out.
This is typically used when
arris a collection of objects, and
funis the name of some predicate function in them.
The function is never destructive on
- See also