Algorithms

This part of Dooku contains various algorithms that are missed in Python’s standard library and here they are:

all_of

dooku.algorithm.all_of(pred, iterable)

Returns True if pred returns True for all the elements in the iterable range or if the range is empty, and False otherwise.

>>> all_of(lambda x: x % 2 == 0, [2, 4, 6, 8])
True
Parameters:
  • pred – a predicate function to check a value from the iterable range
  • iterable – an iterable range to check in
Returns:

True if pred returns True for all the elements

any_of

dooku.algorithm.any_of(pred, iterable)

Returns True if pred returns True for any of the elements in the iterable range, and False otherwise.

>>> any_of(lambda x: x == 4, [1, 2, 3, 4])
True
Parameters:
  • pred – a predicate function to check a value from the iterable range
  • iterable – an iterable range to check in
Returns:

True if pred returns True for any of the elements

find_if

dooku.algorithm.find_if(pred, iterable, default=None)

Returns a reference to the first element in the iterable range for which pred returns True. If no such element is found, the function returns default.

>>> find_if(lambda x: x == 3, [1, 2, 3, 4])
3
Parameters:
  • pred – a predicate function to check a value from the iterable range
  • iterable – an iterable range to check in
  • default – a value that will be returned if no elements were found
Returns:

a reference to the first found element or default

none_of

dooku.algorithm.none_of(pred, iterable)

Returns True if pred returns False for all the elements in the iterable range or if the range is empty, and False otherwise.

>>> none_of(lambda x: x % 2 == 0, [1, 3, 5, 7])
True
Parameters:
  • pred – a predicate function to check a value from the iterable range
  • iterable – an iterable range to check in
Returns:

True if all elements don’t satfisfy pred