folding¶
Folding and Accumulating
Functions to reduce and accumulate items from iterables.
- pythonic_fp.iterables.folding.accumulate(iterable: Iterable, f: Callable[[L, D], L], initial: L | NoValue = NoValue()) Iterator¶
accumulate
Returns an iterator of partial fold items. A pure Python version of
- param iterable:
Iterable to be folded.
- param f:
Two parameter function, first parameter for accumulated items.
- param initial:
Optional
initialitem to start fold.- yields:
The intermediate folded items.
Note
A pure Python implementation of the standard library’s
itertools.accumulatefunction
fdoes not default to addition (for typing flexibility)begins accumulation with an optional
initialitem
- pythonic_fp.iterables.folding.reduce_left(iterable: Iterable, f: Callable[[D, D], D]) D | Never¶
reduce left
Fold an iterable left with a function.
- param iterable:
Iterable to be reduced (folded).
- param f:
Two parameter function, first parameter for accumulated items.
- return:
Reduced item from the iterable.
- raises StopIteration:
When called on an empty iterable.
- raises Exception:
Does not catch any exceptions from
f.
Warning
never returns if given an infinite iterable
does not catch or re-raises exceptions raised by
f
- pythonic_fp.iterables.folding.fold_left(iterable: Iterable, f: Callable[[L, D], L], initial: L) L | Never¶
fold left
Fold an iterable left with a function and initial item.
not restricted to
__add__for the folding functioninitial item is required, does not default to
0handles non-numeric data just find
- param iterable:
iterable to be folded
- param f:
two parameter function, first parameter for accumulated item
- param initial:
mandatory initial item to start fold
- return:
the folded item
Warning
never returns if given an infinite iterable
does not catch or re-raises exceptions raised by
f
- pythonic_fp.iterables.folding.maybe_fold_left(iterable: Iterable, f: Callable[[L, D], L], initial: L | NoValue = NoValue()) MayBe | Never¶
maybe fold left
Folds an iterable left with an “optional” initial item.
when an initial item is not given then
L = Dif iterable empty and no
initialitem given, returnMayBe()
- param iterable:
The iterable to be folded.
- param f:
First argument is for the accumulated items.
- param initial:
Mandatory initial item to start fold.
- return:
MayBeof a successfully folded item, otherwise returnsMayBe().
Warning
never returns if given an infinite iterable
any exception
fraises is thrown away
- pythonic_fp.iterables.folding.sc_reduce_left(iterable: Iterable, f: Callable[[D, D], D], start: Callable[[D], bool] = lambda d: ..., stop: Callable[[D], bool] = lambda d: ..., include_start: bool = True, include_stop: bool = True) tuple[MayBe, Iterator]¶
short circuit reduce left
Short circuit version of a left fold.
- param iterable:
Iterable to be reduced from the left.
- param f:
Two parameter function, first parameter for the accumulator.
- param start:
Delay starting the fold until it returns true.
- param stop:
Prematurely stop the fold when it returns true.
- param include_start:
If true, include starting item in fold.
- param include_stop:
If true, include stopping item in fold.
- return:
Tuple of a
MayBeof the folded item and iterator of remaining iterables.
Note
Behavior for default arguments will
left reduce finite iterable
start folding immediately
continue folding until end (of a possibly infinite iterable)
Tip
Useful for infinite iterables when Callable
stopis provided.
- pythonic_fp.iterables.folding.sc_reduce_right(iterable: Iterable, f: Callable[[D, D], D], start: Callable[[D], bool] = lambda d: ..., stop: Callable[[D], bool] = lambda d: ..., include_start: bool = True, include_stop: bool = True) tuple[MayBe, Iterator]¶
short circuit reduce right
Short circuit version of a right fold.
- param iterable:
Iterable to be reduced from the right.
- param f:
Two parameter function, second parameter for the accumulator.
- param start:
Delay starting the fold until it returns true.
- param stop:
Prematurely stop the fold when it returns true.
- param include_start:
If true, include starting item.
- param include_stop:
If true, include stopping item in fold.
- return:
Tuple of a
MayBeof the folded item and iterator of remaining iterables.
Note
Behavior for default arguments will
right reduce finite iterable
start folding at end (of a possibly infinite iterable)
continue reducing right until beginning
Tip
Useful for infinite and non-reversible iterables.