API Reference

Lazy, immutable infinite sequences of numbers with element-wise arithmetic.

The primitives are constant() and count(); everything else is built from them via arithmetic, comparison, rounding, slicing and the element-wise maximum() and minimum() helpers.

class countably.NumberSequence(*args, **kwargs)[source]

A lazy, immutable sequence of numbers with element-wise arithmetic.

Instances are produced by countably.constant() and countably.count(); everything else is built from them via arithmetic (+, -, *, /, //, %, **), comparisons (<, <=, >, >=), unary ops (-x, abs(x)), rounding (math.floor(), math.ceil(), math.trunc(), round()), slicing, and the element-wise helpers countably.maximum() / countably.minimum().

Sequences are iterable, not iterators: iter(seq) starts a fresh walk each time. Infinite sequences report sys.maxsize from len(); finite slices report their true length. bool() deliberately raises TypeError – the truth value of a (possibly infinite) sequence is not defined.

countably.constant(value: float) NumberSequence[source]

Return an infinite sequence whose every element is value.

Args:

value: The number repeated at every index of the sequence.

Returns:

An infinite sequence of value.

>>> from countably import constant
>>> seq = constant(7)
>>> seq[0], seq[1_000]
(7, 7)
countably.count() NumberSequence[source]

Return the infinite sequence 0, 1, 2, 3, ....

The basic generator used to build everything else.

Returns:

The infinite sequence of the natural numbers.

>>> from countably import count
>>> list(count()[:5])
[0, 1, 2, 3, 4]
countably.maximum(left: NumberSequence | float, right: NumberSequence | float) NumberSequence[source]

Return the element-wise maximum of two sequences (or sequence + number).

Args:

left: The first sequence or number to compare. right: The second sequence or number to compare.

Returns:

A sequence of the larger element at each position.

>>> from countably import count, maximum
>>> list(maximum(count(), 3)[:6])
[3, 3, 3, 3, 4, 5]
countably.minimum(left: NumberSequence | float, right: NumberSequence | float) NumberSequence[source]

Return the element-wise minimum of two sequences (or sequence + number).

Args:

left: The first sequence or number to compare. right: The second sequence or number to compare.

Returns:

A sequence of the smaller element at each position.

>>> from countably import count, minimum
>>> list(minimum(count(), 3)[:6])
[0, 1, 2, 3, 3, 3]