Welcome to streams’s documentation!¶
Contents:
- class streams.Stream(*iterables)¶
Create a new stream instance from iterables.
- all_match(predicate)¶
Returns True if all elements in the stream match the predicate, False otherwise.
This is a terminal operation.
- any_match(predicate)¶
Returns True if any element in the stream matches the predicate, False otherwise.
This is a terminal operation.
- apply_to(func)¶
Calls the given function with the stream as the parameter; that is apply_to(list) is the same as list(stream):
>>> Stream(range(3)).apply_to(list) [0, 1, 2]
This is a terminal operation.
- average()¶
Returns the numeric average of this stream.
This is a terminal operation.
- count()¶
Return the number of the elements in this stream.
This is a terminal operation.
- distinct()¶
Return a stream with distinct elements from this stream. The elements must be hashable.
- limit(n)¶
Returns a stream that will contain up to n elements of this stream.
- map(mapper, *others)¶
Returns a new stream that consists of the elements of this stream mapped through the given mapping function.
- max(key=None)¶
Returns the maximum value in this stream, optionally sorted by the given key function.
This is a terminal operation.
- min(key=None)¶
Returns the minimum value in this stream, optionally sorted by the given key function.
This is a terminal operation.
- next()¶
Yields the next element from this stream.
- none_match(predicate)¶
Returns true if the predicate(i) does not yield a true value for any item in the stream.
This is a terminal operator.
- classmethod of(*values)¶
Returns a new stream whose elements are the star arguments given to this function.
Stream.of(1, 2, 3) returns a stream that yields 1, 2 and 3:
>>> list(Stream.of(1, 2, 3)) [1, 2, 3]
- parallel()¶
Return a possibly parallelized version of this stream. A parallel stream is unordered; the order of elements is not specified at the terminal operation.
- partition(predicate)¶
Partition elements into False entries and True entries:
>>> sf, st = Stream(range(10)).partition(lambda x: x % 2 == 0) >>> list(sf) [1, 3, 5, 7, 9] >>> list(st) [0, 2, 4, 6, 8]
- peek(action)¶
Invoke action(e) for each element that passes through the stream at this point.
- sequential()¶
Convert the stream into a sequential stream.
- skip(n)¶
Skips n elements from this stream and return a stream of the rest.
- sorted(key=None, reverse=False)¶
Sort the elements, as if by builtin sorted; return a new sequential stream whose elements are in the given sorted order.
- starapply_to(func)¶
Calls the given function with the unpacked stream as parameters; that is starapply_to(func) is the same as func(*stream):
>>> Stream(range(5)).starapply_to(print) 0 1 2 3 4
This is a terminal operation.
- starmap(mapper)¶
Maps the iterable arguments from the stream through the func as:
new_e = func(*old_e)
- streammap(func)¶
Map each iterable element through the function, and return a stream of streams.
- sum()¶
Returns the sum of this stream. The elements must be summable together.
This is a terminal operation.
Examples¶
A basic (and rather uninteresting) stream:
>>> for x in Stream(range(3)):
... print(x)
0
1
2
Streams offer various operations as methods. For example getting first 10 even integers:
>>> from itertools import count
>>> Stream(count()).filter(lambda x: x % 2 == 0)[:10].to_list()
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Slicing endless iterators is supported by default.