hof - Higher-Order Functions

hof is a module collects some useful higher-order functions. The module is implemented as a script and a C extension.

import hof    # import Python extension or script
import hof_c  # import Python extension directly
import hof_py # import Python script directly

The extension is 15-70% faster and 700% bigger than the script.

Module functions

First argument type is function, second argument type is sequence.

find(f, seq)
Returns the all element in the sequence matching the function, or raise LookupError if there is no such element.
find_index(f, seq)
Returns index of the first element in the sequence matching the function, or raise LookupError if there is no such element.
find_indices(f, seq)
Returns indices of all elements in the sequence matching the function, or raise LookupError if there is no such element.
all(f, seq)
Return true if all element of the sequence that satisfy the function.
any(f, seq)
Return true if any element of the sequence that satisfy the function.
take(f, seq)
Returns the longest prefix (possibly empty) of elements that satisfy the function.
take(lambda x: x < 3, [0,1,2,3,4,5]) #=> [0,1,2]
drop(f, seq)
Returns the suffix remaining after take(f, seq).
drop(lambda x: x < 3, [0,1,2,3,4,5]) #=> [3,4,5]
span(f, seq)
Returns a tuple pair of take(f, seq) and drop(f, seq).
span(lambda x: x < 3, [0,1,2,3,4,5]) #=> ([0,1,2], [3,4,5])
partition(f, seq)
Returns a tuple pair of the sequences which do and do not satisfy the function.
partition(lambda x: x % 2 == 0, [0,1,2,3,4,5]) #=> ([0,2,4], [1,3,5])