Seq#

Sequence module.

Contains a collection of static methods (functions) for operating on sequences. A sequence is a thin wrapper around Iterable so all functions take (and return) Python iterables.

All functions takes the source as the last curried argument, i.e all functions returns a function that takes the source sequence as the only argument.

Example (functional style):
>>> from expression.collections import seq
>>> xs = seq.of_iterable([1, 2, 3])
>>> ys = xs.pipe(
    seq.map(lambda x: x + 1),
    seq.filter(lambda x: x < 3)
)
Example (fluent style):
>>> from expression.collections import Seq
>>> xs = Seq([1, 2, 3])
>>> ys = xs.map(lambda x: x + 1).filter(lambda x: x < 3)
class Seq(iterable: Iterable[_TSource] = ())#

Sequence type.

Contains instance methods for dot-chaining operators methods on sequences.

Example

>>> xs = Seq([1, 2, 3])
>>> ys = xs.map(lambda x: x + 1).filter(lambda x: x < 3)
append(*others: Iterable[_TSource]) Seq[_TSource]#

Append sequence to other sequences.

Wraps the given enumerations as a single concatenated enumeration.

choose(chooser: Callable[[_TSource], Option[_TResult]]) Seq[_TResult]#

Choose items from the sequence.

Applies the given function to each element of the list. Returns the list comprised of the results x for each element where the function returns Some(x).

Parameters:

chooser – The function to generate options from the elements.

Returns:

The list comprising the values selected from the chooser function.

static delay(generator: Callable[[], Iterable[_TSource]]) Iterable[_TSource]#

Delay sequence.

Returns a sequence that is built from the given delayed specification of a sequence.

The input function is evaluated each time an IEnumerator for the sequence is requested.

Parameters:

generator – The generating function for the sequence.

dict() Iterable[_TSource]#

Returns a json serializable representation of the list.

static empty() Seq[_TSource]#

Returns empty sequence.

fold(folder: Callable[[_TState, _TSource], _TState], state: _TState) _TState#

Fold sequence.

Applies a function to each element of the collection, threading an accumulator argument through the computation. If the input function is f and the elements are i0…iN then computes f (… (f s i0)…) iN.

Parameters:
  • folder – A function that updates the state with each element from the sequence.

  • state – The initial state.

Returns:

The state object after the folding function is applied to each element of the sequence.

head() _TSource#

Returns the first element of the sequence.

length() int#

Returns the length of the sequence.

map(mapper: Callable[[_TSource], _TResult]) Seq[_TResult]#

Map sequence.

Builds a new collection whose elements are the results of applying the given function to each of the elements of the collection.

Parameters:

mapper – A function to transform items from the input sequence.

Returns:

The result sequence.

mapi(mapping: Callable[[int, _TSource], _TResult]) Seq[_TResult]#

Map list with index.

Builds a new collection whose elements are the results of applying the given function to each of the elements of the collection. The integer index passed to the function indicates the index (from 0) of element being transformed.

Parameters:

mapping – The function to transform elements and their indices.

Returns:

The list of transformed elements.

scan(scanner: Callable[[_TState, _TSource], _TState], state: _TState) Iterable[_TState]#

Scan sequence.

Like fold, but computes on-demand and returns the sequence of intermediary and final results.

Parameters:
  • scanner – A function that updates the state with each element from the sequence.

  • state – The initial state.

Returns:

The resulting sequence of computed states.

skip(count: int) Seq[_TSource]#

Skip elements from sequence.

Returns a sequence that skips N elements of the underlying sequence and then yields the remaining elements of the sequence.

Parameters:

count – The number of items to skip.

starmap(mapping: Callable[[_T1, _T2], _TResult]) Seq[_TResult]#
starmap(mapping: Callable[[_T1, _T2, _T3], _TResult]) Seq[_TResult]
starmap(mapping: Callable[[_T1, _T2, _T3, _T4], _TResult]) Seq[_TResult]

Starmap source sequence.

Unpack arguments grouped as tuple elements. Builds a new collection whose elements are the results of applying the given function to the unpacked arguments to each of the elements of the collection.

Parameters:

mapping – A function to transform items from the input sequence.

Returns:

Partially applied map function.

sum() _TSupportsSum#

Returns the sum of the elements in the sequence.

sum_by(projection: Callable[[_TSource], _TSupportsSum]) _TSupportsSum#

Sum the sequence by projection.

Returns the sum of the results generated by applying the function to each element of the sequence.

tail() Seq[_TSource]#

Return the tail of the sequence.

Returns a sequence that skips 1 element of the underlying sequence and then yields the remaining elements of the sequence.

take(count: int) Seq[_TSource]#

Returns the first N elements of the sequence.

Parameters:

count – The number of items to take.

classmethod unfold(generator: Callable[[_TState], Option[tuple[_TSource, _TState]]], state: _TState) Iterable[_TSource]#

Unfold sequence.

Returns a list that contains the elements generated by the given computation. The given initial state argument is passed to the element generator.

Parameters:
  • generator – A function that takes in the current state and returns an option tuple of the next element of the list and the next state value.

  • state – The initial state.

Returns:

The result list.

zip(other: Iterable[_TResult]) Iterable[tuple[_TSource, _TResult]]#

Zip sequence.

Combines the two sequences into a list of pairs. The two sequences need not have equal lengths: when one sequence is exhausted any remaining elements in the other sequence are ignored.

Parameters:

other – The second input sequence.

Returns:

The result sequence.

append(*others: Iterable[_TSource]) Callable[[Iterable[_TSource]], Iterable[_TSource]]#

Append sequence to other sequences.

Wraps the given enumerations as a single concatenated enumeration.

concat(*iterables: Iterable[_TSource]) Iterable[_TSource]#

Concatenate sequences.

Combines the given variable number of enumerations and/or enumeration-of-enumerations as a single concatenated enumeration.

Parameters:

iterables – The input enumeration-of-enumerations.

Returns:

The result sequence.

delay(generator: Callable[[], Iterable[_TSource]]) Iterable[_TSource]#

Delay sequence.

Returns a sequence that is built from the given delayed specification of a sequence.

The input function is evaluated each time an Iterator for the sequence is requested.

Parameters:

generator – The generating function for the sequence.

empty: Seq[Any] = []#

The empty sequence.

fold_back(folder: Callable[[_TSource, _TState], _TState], source: Iterable[_TSource]) Callable[[_TState], _TState]#

Fold elements in sequence backwards.

Applies a function to each element of the collection, starting from the end, threading an accumulator argument through the computation. If the input function is f and the elements are i0…iN then computes f i0 (… (f iN s)…).

Parameters:
  • folder – A function that updates the state with each element from the sequence.

  • source – The input sequence to fold backwards.

Returns:

Partially applied fold_back function.

head(source: Iterable[_TSource]) _TSource#

Return the first element of the sequence.

Parameters:

source – The input sequence.

Returns:

The first element of the sequence.

Raises:

Raises ValueError if the source sequence is empty.

max(source: Iterable[_TSupportsGreaterThan]) _TSupportsGreaterThan#

Return maximum of all elements.

Returns the greatest of all elements of the sequence, compared via max().

min(source: Iterable[_TSupportsLessThan]) _TSupportsLessThan#

Return the minimum of all elements.

Returns the smallest of all elements of the sequence, compared via max().

of(*args: _TSource) Seq[_TSource]#

Create sequence from iterable.

Enables fluent dot chaining on the created sequence object.

of_iterable(source: Iterable[_TSource]) Seq[_TSource]#

Alias to Seq.of.

of_list(source: Iterable[_TSource]) Seq[_TSource]#

Alias to seq.of_iterable.

singleton(item: _TSource) Seq[_TSource]#

Returns a sequence that yields one item only.

Parameters:

item – The input item.

Returns:

The result sequence of one item.

sum(source: Iterable[_TSupportsSum]) _TSupportsSum#

Returns the sum of the elements in the sequence.

tail(source: Iterable[_TSource]) Iterable[_TSource]#

Return tail of sequence.

Returns a sequence that skips 1 element of the underlying sequence and then yields the remaining elements of the sequence.

zip(source1: Iterable[_TSource]) Callable[[Iterable[_TResult]], Iterable[tuple[_TSource, _TResult]]]#

Zip sequence with other.

Combines the two sequences into a list of pairs. The two sequences need not have equal lengths: when one sequence is exhausted any remaining elements in the other sequence are ignored.

Parameters:

source1 – The first input sequence.

Returns:

Partially applied zip function.