iter
Module Reference
flatten(xs)
isiterable(x, str_iterable=False, bytes_iterable=False)
Is x
iterable?
- str_iterable
, bytes_iterable
: whether str
and bytes
are considered iterable
Source code in haskellian/src/haskellian/iter/basics.py
range(start=0, end=None, step=1)
Like range
, but possibly infinite (if end is None
)
batch(n, xs)
lazy_batch(n, xs)
Batches xs
into n
-tuples, but returns lazy iterators
lazy_shard(min_size, size, xs)
Shards xs
into groups of at least min_size
based on size
(last shard may have less)
Source code in haskellian/src/haskellian/iter/batching.py
shard(min_size, size, xs)
Shards xs
into groups of at least min_size
based on size
(last shard may have less)
Source code in haskellian/src/haskellian/iter/batching.py
split(n, xs)
Splits xs
into a list of the first n
and a generator of the rest
- e.g: split(3, [1,2,3,4,5]) == ([1, 2, 3], generator(4, 5))
Source code in haskellian/src/haskellian/iter/batching.py
at(i, xs)
pick(indices, xs)
Pick xs
values at indices indices
- E.g. pick([1, 2, 4], ['a', 'b', 'c', 'd', 'e', 'f', 'g']) = ['b', 'c', 'e']
lift(func)
Lift an iterable function func
to return an Iter
Source code in haskellian/src/haskellian/iter/lifting.py
oversample(data)
Balance lazy iterators by repeating the shorter ones
repeat(iter)
shuffle(xs, shuffle_size)
Reservoir sampling based shuffling
Source code in haskellian/src/haskellian/iter/misc.py
undersample(data)
Balance lazy iterators by truncating the longer ones
ndenumerate(xxs, depth=None)
ndenumerate(xxs: Iterable[Iterable[A]], depth: Literal[2] | None = None) -> I.Iter[tuple[tuple[int, int], A]]
Like enumerate
, but for nested iterables.
So, instead of
You can just do
Note:
- By default it unrolls all the way down until a none-iterable object is found.
- If you want to iterate over iterables (e.g. lists, numpy arrays, tensors), you'll have to pass in a fixed depth
Source code in haskellian/src/haskellian/iter/nested.py
ndflat(xxs, depth=None)
ndflat([[x1, x2], [x3, [x4]]]) = [x1, x2, x3, x4]
Source code in haskellian/src/haskellian/iter/nested.py
ndrange(*ranges)
Like range
, but returns an iterable of n
-tuples instead of ints.
So, instead of
You can just do:And it also supports normal (start, end)
or (start, end, step)
ranges:
Source code in haskellian/src/haskellian/iter/nested.py
every(n, xs)
Take every n
th element of xs
- every(3, range(10)) == Iter([0, 3, 6, 9])
fst(t)
head(xs)
last(xs)
pad(n, fill, xs)
Pads xs
to length n
, appending fill
s as needed
skip(n, xs)
snd(t)
tail(xs)
transpose(xs)
Transpose a 2d list, cropping to the shortest row. E.g:
butSource code in haskellian/src/haskellian/iter/transposing.py
transpose_ragged(xs)
Like transpose
, but resulting rows can be ragged (i.e. have different lengths). E.g:
Source code in haskellian/src/haskellian/iter/transposing.py
interleave(xs)
Interleave multiple iterators based on their weight.
- (weight, iter) = xs[i]
: iter
is yielded weight
times before moving to the next iterator.
Source code in haskellian/src/haskellian/iter/zipping.py
pairwise(xs)
pairwise([x1, x2, x3, x4, ...]) = [(x1, x2), (x2, x3), (x3, x4), ...]
- pairwise([]) = []
- pairwise([x]) = []