complement

class iteration_utilities.complement(func)

Invert a predicate function. There is a homonymous function in the toolz package ([R45]) but significantly modified.

Parameters:

func : callable

The function to complement.

Returns:

complemented_func : callable

The complement to func.

References

[R45](1, 2) https://toolz.readthedocs.io/en/latest/index.html

Examples

complement is equivalent to lambda x: not x() but significantly faster:

>>> from iteration_utilities import complement
>>> from iteration_utilities import is_None
>>> is_not_None = complement(is_None)
>>> list(filter(is_not_None, [1,2,None,3,4,None]))
[1, 2, 3, 4]

Note

The example code could also be done with itertools.filterfalse() or iteration_utilities.is_not_None().

__call__(*args, **kwargs)

Returns not func(*args, **kwargs) using func.

func

(callable) The function that is complemented (readonly).

New in version 0.6.