jivago.lang.stream module

class jivago.lang.stream.Stream(*iterables)[source]

Bases: typing.Generic

Stream class to perform functional-style operations in an aesthetically-pleasing manner.

Parameters:*iterables (Iterable) – Source iterables for the Stream object. When multiple iterables are given, they will be concatenated.
allMatch(fun: Callable[[T], bool]) → bool[source]

Returns True if all elements of the stream match the criteria.

anyMatch(fun: Callable[[T], bool]) → bool[source]

Returns True if any element of the stream matches the criteria.

count() → int[source]

Returns the number of elements in the Stream. Should never be used with an infinite stream!

filter(fun: Callable[[T], S]) → jivago.lang.stream.Stream[~T][T][source]

Filters elements using the supplied function. When iterating over tuples, the function can take multiple arguments.

first() → jivago.lang.nullable.Nullable[~T][T][source]

Returns a nullable containing the first element of the stream. If the stream is empty, returns an empty nullable.

firstMatch(fun: Callable[[T], bool]) → jivago.lang.nullable.Nullable[~T][T][source]

Returns a Nullable of the first element matching the criteria. If none exist, returns an empty Nullable.

flat() → jivago.lang.stream.Stream[~T][T][source]

When iterating over lists, flattens the stream by concatenating all lists.

forEach(fun: Callable[[T], Any]) → None[source]

Calls the function with each element. This is a terminal operation.

map(fun: Callable[[T], S]) → jivago.lang.stream.Stream[~S][S][source]

Maps elements using the supplied function. When iterating over tuples, the function can take multiple arguments.

max() → T[source]

Returns the max of all elements in the stream.

min() → T[source]

Returns the min of all elements in the stream.

noneMatch(fun: Callable[[T], bool]) → bool[source]

Returns True if no element of the stream matches the criteria.

static of(*args) → jivago.lang.stream.Stream[source]

Creates a stream with non iterable arguments.

Examples

>>> Stream.of(1,2,3,4).toList()
[1,2,3,4]
static range(*args) → jivago.lang.stream.Stream[int][int][source]

Creates an incrementing, integer stream. If arguments are supplied, they are passed as-is to the builtin range function. Otherwise, an infinite stream is created, starting at 0.

reduce(start_value: Any, reducer: Callable[[Any, T], Any])[source]

Reduce using the supplied function.

Parameters:
  • start_value – starting value for the accumulator.
  • reducer (Callable) – e.g. lambda accumulator, element: accumulator + element
sum()[source]

Returns the sum of all elements in the stream.

take(number: int) → jivago.lang.stream.Stream[~T][T][source]

Limit the stream to a specific number of items.

Examples

>>> Stream.range().take(5).toList()
[0,1,2,3,4]
toDict() → dict[source]

When iterating over tuples, collects all elements to a dictionary. The first element becomes the key, the second the value.

toList() → List[T][source]

Collects all elements to a list.

toSet() → Set[T][source]

Collects all elements to a set.

toTuple() → Tuple[T][source]

Collects all elements to a tuple.

unzip() → Tuple[tuple, ...][source]

When iterating over tuples, unwraps the stream back to separate lists.

static zip(*iterables) → jivago.lang.stream.Stream[typing.Tuple[~T]][Tuple[T]][source]

Creates a stream by zipping the iterables, instead of concatenating them.

Returns:Stream of tuples.