query
On this page

ifilter0

lib.ifilter0

Docs pulled from | This Revision | about 1 hour ago


Filter a list for elements that satisfy a predicate function. The predicate function is called with both the index and value for each element. It must return true/false to include/exclude a given element in the result. This function is strict in the result of the predicate function for each element. This function has O(n) complexity.

Also see builtins.filter (available as lib.lists.filter), which can be used instead when the index isn't needed.

Inputs

ipred

The predicate function, it takes two arguments:

    1. (int): the index of the element.
    1. (a): the value of the element.

It must return true/false to include/exclude a given element from the result.

list

The list to filter using the predicate.

Type

ifilter0 :: (int -> a -> bool) -> [a] -> [a]

Examples

lib.lists.ifilter0 usage example

ifilter0 (i: v: i == 0 || v > 2) [ 1 2 3 ]
=> [ 1 3 ]
(lib.lists.ifilter0)

Noogle detected

Aliases

Implementation

The following is the current implementation of this function.

ifilter0 =
    ipred:
    input:
    map (idx: elemAt input idx) (
      filter (idx: ipred idx (elemAt input idx)) (
        genList (x: x) (length input)
      )
    );