nustack
ReferenceNu STD

itertools

Nu surface for Python's `itertools` module.

Module nustd.itertools.

Nu surface for Python's itertools module.

itertools is a function module - free functions that build and combine iterators, no central class - so the Nu surface mirrors that: free functions over Nu streams. Two layers behind it: functions (the wrappers) and interactions (the hand-written StreamQuery atoms each wrapper builds, hot path, e2e, no factory).

A gap-fill: members already in Nu core (map / filter / zip / sorted / enumerate / reversed / sums and folds) are not repeated here. Import it the way you would the stdlib:

from nustd.itertools import chain, islice, count
import nustd.itertools as itertools     # then itertools.product(a, b)

Call

NameCallMeaning
accumulateitertools.accumulate(iterable, func=None)Running accumulation: mirrors itertools.accumulate().
batcheditertools.batched(iterable, n)Yield tuples of up to n items: mirrors itertools.batched().
chainitertools.chain()Concatenate iterables end to end: mirrors itertools.chain().
chain_from_iterableitertools.chain_from_iterable(iterable)Flatten an iterable of iterables one level: itertools.chain.from_iterable().
combinationsitertools.combinations(iterable, r)r-length sorted subsequences: mirrors itertools.combinations().
combinations_with_replacementitertools.combinations_with_replacement(iterable, r)r-length subsequences allowing repeats: combinations_with_replacement().
compressitertools.compress(data, selectors)Keep data items where selectors is truthy: itertools.compress().
countitertools.count(start=0, step=1)Count from start by step forever: mirrors itertools.count().
cycleitertools.cycle(iterable)Repeat iterable endlessly: mirrors itertools.cycle().
dropwhileitertools.dropwhile(predicate, iterable)Skip while predicate holds, then yield the rest: itertools.dropwhile().
filterfalseitertools.filterfalse(predicate, iterable)Keep items where predicate is falsy: mirrors itertools.filterfalse().
groupbyitertools.groupby(iterable, key=None)Group consecutive items by key: mirrors itertools.groupby().
isliceitertools.islice(iterable)Slice iterable lazily: mirrors itertools.islice().
pairwiseitertools.pairwise(iterable)Yield overlapping consecutive pairs: mirrors itertools.pairwise().
permutationsitertools.permutations(iterable, r=None)r-length ordered arrangements: mirrors itertools.permutations().
productitertools.product(repeat=1)The cartesian product of iterables: mirrors itertools.product().
repeatitertools.repeat(elem, times=None)Yield elem times times, or forever: mirrors itertools.repeat().
starmapitertools.starmap(function, iterable)Apply function to unpacked items: mirrors itertools.starmap().
takewhileitertools.takewhile(predicate, iterable)Yield while predicate holds, stop at the first falsy: itertools.takewhile().
teeitertools.tee(iterable, n=2)Split iterable into n independent iterators: itertools.tee().
zip_longestitertools.zip_longest(fillvalue=None)Zip to the longest, padding with fillvalue: itertools.zip_longest().

accumulate

Running accumulation: mirrors itertools.accumulate().

itertools.accumulate(iterable, func=None)

Path nustd.itertools.accumulate. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Without func it is a running sum. With func (a Nu term) each step reads the running value via AttrRef("acc") and the item via AttrRef("item"); the first item is yielded as-is.

Arguments

NameTypeDefaultMeaning
iterableArg[Iterable]
funcNu | NoneNone

Undocumented: example.

batched

Yield tuples of up to n items: mirrors itertools.batched().

itertools.batched(iterable, n)

Path nustd.itertools.batched. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
iterableArg[Iterable]
nIntArg

Undocumented: example.

chain

Concatenate iterables end to end: mirrors itertools.chain().

itertools.chain()

Path nustd.itertools.chain. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Undocumented: example.

chain_from_iterable

Flatten an iterable of iterables one level: itertools.chain.from_iterable().

itertools.chain_from_iterable(iterable)

Path nustd.itertools.chain_from_iterable. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
iterableArg[Iterable]

Undocumented: example.

combinations

r-length sorted subsequences: mirrors itertools.combinations().

itertools.combinations(iterable, r)

Path nustd.itertools.combinations. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
iterableArg[Iterable]
rIntArg

Undocumented: example.

combinations_with_replacement

r-length subsequences allowing repeats: combinations_with_replacement().

itertools.combinations_with_replacement(iterable, r)

Path nustd.itertools.combinations_with_replacement. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
iterableArg[Iterable]
rIntArg

Undocumented: example.

compress

Keep data items where selectors is truthy: itertools.compress().

itertools.compress(data, selectors)

Path nustd.itertools.compress. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
dataArg[Iterable]
selectorsArg[Iterable]

Undocumented: example.

count

Count from start by step forever: mirrors itertools.count().

itertools.count(start=0, step=1)

Path nustd.itertools.count. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Infinite - bound it with islice (or another short consumer).

Arguments

NameTypeDefaultMeaning
startIntArg0
stepIntArg1

Undocumented: example.

cycle

Repeat iterable endlessly: mirrors itertools.cycle().

itertools.cycle(iterable)

Path nustd.itertools.cycle. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
iterableArg[Iterable]

Undocumented: example.

dropwhile

Skip while predicate holds, then yield the rest: itertools.dropwhile().

itertools.dropwhile(predicate, iterable)

Path nustd.itertools.dropwhile. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

predicate reads the current item via AttrRef("item").

Arguments

NameTypeDefaultMeaning
predicateNu
iterableArg[Iterable]

Undocumented: example.

filterfalse

Keep items where predicate is falsy: mirrors itertools.filterfalse().

itertools.filterfalse(predicate, iterable)

Path nustd.itertools.filterfalse. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

predicate reads the current item via AttrRef("item").

Arguments

NameTypeDefaultMeaning
predicateNu
iterableArg[Iterable]

Undocumented: example.

groupby

Group consecutive items by key: mirrors itertools.groupby().

itertools.groupby(iterable, key=None)

Path nustd.itertools.groupby. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Yields (key_value, tuple(group)) pairs. With key (a Nu term) the key reads the item via AttrRef("item"); without it items group by identity.

Arguments

NameTypeDefaultMeaning
iterableArg[Iterable]
keyNu | NoneNone

Undocumented: example.

islice

Slice iterable lazily: mirrors itertools.islice().

itertools.islice(iterable)

Path nustd.itertools.islice. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

args is 1-3 ints: stop | start, stop | start, stop, step.

Arguments

NameTypeDefaultMeaning
iterableArg[Iterable]

Undocumented: example.

pairwise

Yield overlapping consecutive pairs: mirrors itertools.pairwise().

itertools.pairwise(iterable)

Path nustd.itertools.pairwise. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
iterableArg[Iterable]

Undocumented: example.

permutations

r-length ordered arrangements: mirrors itertools.permutations().

itertools.permutations(iterable, r=None)

Path nustd.itertools.permutations. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
iterableArg[Iterable]
rIntArg | NoneNone

Undocumented: example.

product

The cartesian product of iterables: mirrors itertools.product().

itertools.product(repeat=1)

Path nustd.itertools.product. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
repeatIntArg1

Undocumented: example.

repeat

Yield elem times times, or forever: mirrors itertools.repeat().

itertools.repeat(elem, times=None)

Path nustd.itertools.repeat. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
elemobject
timesIntArg | NoneNone

Undocumented: example.

starmap

Apply function to unpacked items: mirrors itertools.starmap().

itertools.starmap(function, iterable)

Path nustd.itertools.starmap. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Each item is a tuple; function reads its parts via TupleAttrRef("item")[0], [1], ...

Arguments

NameTypeDefaultMeaning
functionNu
iterableArg[Iterable]

Undocumented: example.

takewhile

Yield while predicate holds, stop at the first falsy: itertools.takewhile().

itertools.takewhile(predicate, iterable)

Path nustd.itertools.takewhile. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

predicate reads the current item via AttrRef("item").

Arguments

NameTypeDefaultMeaning
predicateNu
iterableArg[Iterable]

Undocumented: example.

tee

Split iterable into n independent iterators: itertools.tee().

itertools.tee(iterable, n=2)

Path nustd.itertools.tee. Defined on nustd.itertools.functions, bound as a function. Builds Any.

Returns an Any holding a tuple of n iterators (not a stream), so it is the one member here backed by a ScalarQuery. Its source rides as a scalar child (a ScalarQuery may not hold a stream), and the atom materializes it with sync_iter before splitting.

Arguments

NameTypeDefaultMeaning
iterableArg[Iterable]
nIntArg2

Undocumented: example.

zip_longest

Zip to the longest, padding with fillvalue: itertools.zip_longest().

itertools.zip_longest(fillvalue=None)

Path nustd.itertools.zip_longest. Defined on nustd.itertools.functions, bound as a function. Builds Nu.

Arguments

NameTypeDefaultMeaning
fillvalueobjectNone

Undocumented: example.

On this page