iterables.folding¶
Module pythonic_fp.iterables.folding
- pythonic_fp.iterables.folding.accumulate(iterable: Iterable, f: Callable[[L, D], L], initial: L | NoValue = NoValue()) Iterator ¶
Returns an iterator of partial fold values.
A pure Python version of standard library’s
itertools.accumulate
function
f
does not default to addition (for typing flexibility)begins accumulation with an “optional”
initial
value
- Parameters:
iterable – iterable to be folded
f – two parameter function, first parameter for accumulated value
initial – “optional” initial value to start fold
- Returns:
an iterator of the intermediate fold values
- pythonic_fp.iterables.folding.fold_left(iterable: Iterable, f: Callable[[L, D], L], initial: L) L | Never ¶
Fold an iterable left with a function and initial value.
not restricted to
__add__
for the folding functioninitial value required, does not default to
0
for initial valuehandles non-numeric data just find
Warning
This function never return if given an infinite iterable.
Warning
This function does not catch any exceptions
f
may raise.- Parameters:
iterable – iterable to be folded
f – two parameter function, first parameter for accumulated value
initial – mandatory initial value to start fold
- Returns:
the folded value
- pythonic_fp.iterables.folding.maybe_fold_left(iterable: Iterable, f: Callable[[L, D], L], initial: L | NoValue = NoValue()) MayBe | Never ¶
Folds an iterable left with an “optional” initial value..
when an initial value is not given then
L = D
if iterable empty and no
initial
value given, returnMayBe()
Warning
This function never return if given an infinite iterable.
Warning
This function returns a
MayBe()
whenf
raises any exception what-so-ever. The exception is thrown away.- Parameters:
iterable – The iterable to be folded.
f – First argument is for the accumulated value.
initial – Mandatory initial value to start fold.
- Returns:
MayBe of a successfully folded value, otherwise MayBe()
- pythonic_fp.iterables.folding.reduce_left(iterable: Iterable, f: Callable[[D, D], D]) D | Never ¶
Fold an iterable left with a function.
Warning
This function never return if given an infinite iterable.
Warning
This function does not catch or re-raises exceptions from
f
.- Parameters:
iterable – iterable to be reduced (folded)
f – two parameter function, first parameter for accumulated value
- Returns:
reduced value from the iterable
- Raises:
StopIteration – when called on an empty iterable
Exception – does not catch any exceptions from
f
- pythonic_fp.iterables.folding.sc_reduce_left(iterable: ~collections.abc.Iterable, f: ~collections.abc.Callable[[D, D], D], start: ~collections.abc.Callable[[D], bool] = <function <lambda>>, stop: ~collections.abc.Callable[[D], bool] = <function <lambda>>, include_start: bool = True, include_stop: bool = True) tuple[MayBe, Iterator] ¶
Short circuit version of a left reduce.
Useful for infinite iterables.
Behavior for default arguments will
left reduce finite iterable
start folding immediately
continue folding until end (of a possibly infinite iterable)
- Parameters:
iterable – iterable to be reduced from the left
f – two parameter function, first parameter for accumulated value
start – delay starting the fold until it returns true
stop – prematurely stop the fold when it returns true
include_start – if true, include fold starting value in fold
include_stop – if true, include stopping value in fold
- Returns:
tuple of a MayBe of the folded value and iterator of remaining iterables
- pythonic_fp.iterables.folding.sc_reduce_right(iterable: ~collections.abc.Iterable, f: ~collections.abc.Callable[[D, D], D], start: ~collections.abc.Callable[[D], bool] = <function <lambda>>, stop: ~collections.abc.Callable[[D], bool] = <function <lambda>>, include_start: bool = True, include_stop: bool = True) tuple[MayBe, Iterator] ¶
Short circuit version of a right reduce.
Useful for infinite and non-reversible iterables.
Behavior for default arguments will
right reduce finite iterable
start folding at end (of a possibly infinite iterable)
continue reducing right until beginning
- Parameters:
iterable – iterable to be reduced from the right
f – two parameter function, second parameter for accumulated value
start – delay starting the fold until it returns true
stop – prematurely stop the fold when it returns true
include_start – if true, include fold starting value in fold
include_stop – if true, include stopping value in fold
- Returns:
tuple of a MayBe of the folded value and iterator of remaining iterables