pythonic_fp.iterables API¶
Iterables Module¶
Module fp.iterables - Iterator related tools
Library of iterator related functions and enumerations.
Concatenating and merging iterables
Dropping and taking values from iterables
Reducing and accumulating iterables
Assumptions
iterables are not necessarily iterators
at all times iterator protocol is assumed to be followed
all iterators are assumed to be iterable
for all iterators
foowe assumeiter(foo) is foo
- class pythonic_fp.iterables.FM(*values)¶
Types of iterable blending,
CONCAT: Concatenate first to last
MERGE: Merge until one is exhausted
EXHAUST: Merge until all are exhausted
- pythonic_fp.iterables.accumulate(iterable, f, initial=NoValue(), /)¶
Returns an iterator of accumulated values.
pure Python version of standard library’s
itertools.accumulatefunction
fdoes not default to addition (for typing flexibility)begins accumulation with an optional
initialvalue
- Return type:
Iterator[L]
- pythonic_fp.iterables.concat(*iterables)¶
Sequentially concatenate multiple iterables together.
pure Python version of standard library’s
itertools.chainiterator sequentially yields each iterable until all are exhausted
an infinite iterable will prevent subsequent iterables from yielding any values
performant to
itertools.chain
- Return type:
Iterator[D]
- pythonic_fp.iterables.drop(iterable, n, /)¶
Drop the next
nvalues fromiterable.- Return type:
Iterator[D]
- pythonic_fp.iterables.drop_while(iterable, pred, /)¶
Drop initial values from
iterablewhile predicate is true.- Return type:
Iterator[D]
- pythonic_fp.iterables.exhaust(*iterables)¶
Shuffle together multiple iterables until all are exhausted. Iterator yields until all iterables are exhausted.
- Return type:
Iterator[D]
- pythonic_fp.iterables.foldl(iterable, f, initial, /)¶
Folds an iterable left with a function and initial value.
traditional FP type order given for function
fdoes not catch any exceptions
fmay raiselike builtin
sumfor Python >=3.8 exceptnot restricted to
__add__for the folding functioninitial value required, does not default to
0for initial valuehandles non-numeric data just find
never returns if
iterablegenerates an infinite iterator
- Return type:
Union[L,Never]
- pythonic_fp.iterables.mb_fold_left(iterable, f, initial=NoValue())¶
Folds an iterable left with optional initial value.
traditional FP type order given for function
fwhen an initial value is not given then
~L = ~Dif iterable empty and no
initialvalue given, returnMB()never returns if iterable generates an infinite iterator
- Return type:
MayBe[L]
- pythonic_fp.iterables.merge(*iterables, yield_partials=False)¶
Shuffle together the
iterablesuntil one is exhausted.iterator yields until one of the iterables is exhausted
if
yield_partialsis true,yield any unmatched yielded values from other iterables
prevents data lose
if any of the iterables are iterators with external references
- Return type:
Iterator[D]
- pythonic_fp.iterables.reduce_left(iterable, f, /)¶
Fold an iterable left with a function.
traditional FP type order given for function
fif iterable empty,
StopIterationexception raiseddoes not catch any exceptions
fmay raisenever returns if
iterablegenerates an infinite iterator
- Return type:
Union[D,Never]
- pythonic_fp.iterables.sc_reduce_left(iterable, f, /, start=<function '<lambda>'>, stop=<function '<lambda>'>, include_start=True, include_stop=True)¶
Short circuit version of a left reduce. Useful for infinite or iterables.
Behavior for default arguments will
left reduce finite iterable
start folding immediately
continue folding until end (of a possibly infinite iterable)
Callable
startdelays starting the left reduceCallable
stopprematurely stop the left reduce
- Return type:
tuple[MayBe[D],Iterator[D]]
- pythonic_fp.iterables.sc_reduce_right(iterable, f, /, start=<function '<lambda>'>, stop=<function '<lambda>'>, include_start=True, include_stop=True)¶
Short circuit version of a right reduce. Useful for infinite or non-reversible iterables.
Behavior for default arguments will
right reduce finite iterable
start reducing at end (of a possibly infinite iterable)
continue reducing right until beginning
Callable
startprematurely starts the right reduceCallable
stopprematurely stops the right reduce
- Return type:
tuple[MayBe[D],Iterator[D]]
- pythonic_fp.iterables.take(iterable, n, /)¶
Return an iterator of up to
ninitial values of an iterable- Return type:
Iterator[D]
- pythonic_fp.iterables.take_split(iterable, n, /)¶
Same as take except also return an iterator of the remaining values.
return a tuple of
an iterator of up to
ninitial valuesan iterator of the remaining vales of the
iterable
Contract: do not access second iterator until first is exhausted
- Return type:
tuple[Iterator[D],Iterator[D]]
- pythonic_fp.iterables.take_while(iterable, pred, /)¶
Yield values from
iterablewhile predicate is true.Warning
Risk of value loss if iterable is multiple referenced iterator.
- Return type:
Iterator[D]
- pythonic_fp.iterables.take_while_split(iterable, pred, /)¶
Yield values from
iterablewhilepredicateis true.return a tuple of two iterators
first of initial values where predicate is true, followed by first to fail
second of the remaining values of the iterable after first failed value
Contract: do not access second iterator until first is exhausted
- Return type:
tuple[Iterator[D],Iterator[D]]