diff --git a/.gitignore b/.gitignore index 9d3cce4..c8a8160 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ build/ dist/ .vscode/ java_stream.egg-info/ -main.py \ No newline at end of file +main.py +.idea +*venv* diff --git a/CHANGELIST.md b/CHANGELIST.md new file mode 100644 index 0000000..9fd517e --- /dev/null +++ b/CHANGELIST.md @@ -0,0 +1,14 @@ +## CHANGELIST + +### 03-12-21 + +#### Summary + +- Added new orElse and orElseGet API to Optional class +- Adjusted Stream's findFirst to support predicates +- Update py docs with newly added API +- Added new ignorable items + +#### Authors + +sskorol (Sergey Korol, serhii.s.korol@gmail.com) diff --git a/doc/stream/booleans.html b/doc/stream/booleans.html index 1e4c7d2..e981800 100644 --- a/doc/stream/booleans.html +++ b/doc/stream/booleans.html @@ -1,328 +1,269 @@ - - - - stream.booleans API documentation - - - - - - + + + +stream.booleans API documentation + + + + + + + + + - -
-
-

-stream.booleans

- - -
- View Source -
from stream import Stream
-import random
-
-
-class BooleanStream(Stream):
-
-    @staticmethod
-    def random():
-        '''
-        Returns an infinite stream of random booleans
-
-        :return: a random boolean stream
-        '''
-        return BooleanStream(Stream.generate(lambda: random.randint(0, 1) == 1))
-
-    @staticmethod
-    def true():
-        '''
-        Returns an infinite stream of True
-
-        :return: a True boolean stream
-        '''
-        return BooleanStream(Stream.generate(lambda: True))
-
-    @staticmethod
-    def false():
-        '''
-        Returns an infinite stream of False
-
-        :return: a False boolean stream
-        '''
-        return BooleanStream(Stream.generate(lambda: False))
-
-    def __init__(self, _stream):
-        if isinstance(_stream, Stream):
-            self.iterable = _stream.iterable
-        else:
-            self.iterable = _stream
-
- -
- -
-
-
- #   - - - class - BooleanStream(stream.stream.Stream): -
- -
- View Source -
class BooleanStream(Stream):
-
-    @staticmethod
-    def random():
-        '''
-        Returns an infinite stream of random booleans
-
-        :return: a random boolean stream
-        '''
-        return BooleanStream(Stream.generate(lambda: random.randint(0, 1) == 1))
-
-    @staticmethod
-    def true():
-        '''
-        Returns an infinite stream of True
-
-        :return: a True boolean stream
-        '''
-        return BooleanStream(Stream.generate(lambda: True))
-
-    @staticmethod
-    def false():
-        '''
-        Returns an infinite stream of False
-
-        :return: a False boolean stream
-        '''
-        return BooleanStream(Stream.generate(lambda: False))
-
-    def __init__(self, _stream):
-        if isinstance(_stream, Stream):
-            self.iterable = _stream.iterable
-        else:
-            self.iterable = _stream
-
- -
- -

A sequence of elements supporting sequential operations.

- -

The following example illustrates an aggregate operation using -Stream:

- -
result = Stream(elements)
-                .filter(lambda w: w.getColor() == RED)
-                .map(lambda w: w.getWeight())
-                .sum()
-
- + +
+
+
+

Module stream.booleans

+
+
+
+ +Expand source code + +
from stream import Stream
+import random
+
+
+class BooleanStream(Stream):
+
+    @staticmethod
+    def random():
+        '''
+        Returns an infinite stream of random booleans
+
+        :return: a random boolean stream
+        '''
+        return BooleanStream(Stream.generate(lambda: random.randint(0, 1) == 1))
+
+    @staticmethod
+    def true():
+        '''
+        Returns an infinite stream of True
+
+        :return: a True boolean stream
+        '''
+        return BooleanStream(Stream.generate(lambda: True))
+
+    @staticmethod
+    def false():
+        '''
+        Returns an infinite stream of False
+
+        :return: a False boolean stream
+        '''
+        return BooleanStream(Stream.generate(lambda: False))
+
+    def __init__(self, _stream):
+        if isinstance(_stream, Stream):
+            self.iterable = _stream.iterable
+        else:
+            self.iterable = _stream
+
+
+
+
+
+
+
+
+
+

Classes

+
+
+class BooleanStream +(_stream) +
+
+

A sequence of elements supporting sequential operations.

+

The following example illustrates an aggregate operation using

+

Stream

+

result = Stream(elements) +.filter(lambda w: w.getColor() == RED) +.map(lambda w: w.getWeight()) +.sum()

A stream pipeline, like the elements example above, can be viewed as a query on the stream source.

- -

A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may raise Exception if it detects that the stream is being reused.

-
- - -
-
#   - - - BooleanStream(_stream)
- -
- View Source -
    def __init__(self, _stream):
-        if isinstance(_stream, Stream):
-            self.iterable = _stream.iterable
-        else:
-            self.iterable = _stream
-
- -
- - - -
-
-
#   - -
@staticmethod
- - def - random(): -
- -
- View Source -
    @staticmethod
-    def random():
-        '''
-        Returns an infinite stream of random booleans
-
-        :return: a random boolean stream
-        '''
-        return BooleanStream(Stream.generate(lambda: random.randint(0, 1) == 1))
-
- -
- -

Returns an infinite stream of random booleans

- -

:return: a random boolean stream

+

A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may raise Exception if it detects that the stream is being reused.

+
+ +Expand source code + +
class BooleanStream(Stream):
+
+    @staticmethod
+    def random():
+        '''
+        Returns an infinite stream of random booleans
+
+        :return: a random boolean stream
+        '''
+        return BooleanStream(Stream.generate(lambda: random.randint(0, 1) == 1))
+
+    @staticmethod
+    def true():
+        '''
+        Returns an infinite stream of True
+
+        :return: a True boolean stream
+        '''
+        return BooleanStream(Stream.generate(lambda: True))
+
+    @staticmethod
+    def false():
+        '''
+        Returns an infinite stream of False
+
+        :return: a False boolean stream
+        '''
+        return BooleanStream(Stream.generate(lambda: False))
+
+    def __init__(self, _stream):
+        if isinstance(_stream, Stream):
+            self.iterable = _stream.iterable
+        else:
+            self.iterable = _stream
+
+

Ancestors

+ +

Static methods

+
+
+def false() +
+
+

Returns an infinite stream of False

+

:return: a False boolean stream

+
+ +Expand source code + +
@staticmethod
+def false():
+    '''
+    Returns an infinite stream of False
+
+    :return: a False boolean stream
+    '''
+    return BooleanStream(Stream.generate(lambda: False))
+
+
+
+def random() +
+
+

Returns an infinite stream of random booleans

+

:return: a random boolean stream

+
+ +Expand source code + +
@staticmethod
+def random():
+    '''
+    Returns an infinite stream of random booleans
+
+    :return: a random boolean stream
+    '''
+    return BooleanStream(Stream.generate(lambda: random.randint(0, 1) == 1))
+
+
+
+def true() +
+
+

Returns an infinite stream of True

+

:return: a True boolean stream

+
+ +Expand source code + +
@staticmethod
+def true():
+    '''
+    Returns an infinite stream of True
+
+    :return: a True boolean stream
+    '''
+    return BooleanStream(Stream.generate(lambda: True))
+
+
+
+

Inherited members

+ +
+
+
+
+
-
-
#   - -
@staticmethod
- - def - true(): -
- -
- View Source -
    @staticmethod
-    def true():
-        '''
-        Returns an infinite stream of True
-
-        :return: a True boolean stream
-        '''
-        return BooleanStream(Stream.generate(lambda: True))
-
- -
- -

Returns an infinite stream of True

- -

:return: a True boolean stream

-
- - -
-
-
#   - -
@staticmethod
- - def - false(): -
- -
- View Source -
    @staticmethod
-    def false():
-        '''
-        Returns an infinite stream of False
-
-        :return: a False boolean stream
-        '''
-        return BooleanStream(Stream.generate(lambda: False))
-
- -
- -

Returns an infinite stream of False

- -

:return: a False boolean stream

-
- - -
- -
-
+ + + + \ No newline at end of file diff --git a/doc/stream/index.html b/doc/stream/index.html new file mode 100644 index 0000000..a090b9d --- /dev/null +++ b/doc/stream/index.html @@ -0,0 +1,83 @@ + + + + + + +stream API documentation + + + + + + + + + + + +
+
+
+

Package stream

+
+
+
+ +Expand source code + +
from .stream import Stream
+from .numbers import NumberStream
+from .booleans import BooleanStream
+
+
+
+

Sub-modules

+
+
stream.booleans
+
+
+
+
stream.numbers
+
+
+
+
stream.stream
+
+
+
+
stream.util
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+ + + \ No newline at end of file diff --git a/doc/stream/numbers.html b/doc/stream/numbers.html index 430ce5c..e6d18ed 100644 --- a/doc/stream/numbers.html +++ b/doc/stream/numbers.html @@ -1,1522 +1,1219 @@ - - - - stream.numbers API documentation - - - - - - + + + +stream.numbers API documentation + + + + + + + + + - -
-
-

-stream.numbers

- - -
- View Source -
import math
-import random
-
-from .stream import Stream
-
-
-class NumberStream(Stream):
-
-    @staticmethod
-    def integers():
-        '''
-        Returns an infinite stream of integer from 0 to infinity
-
-        :return: the infinite stream
-        '''
-        return NumberStream(Stream.iterate(0, lambda i: i + 1))
-
-    @staticmethod
-    def odds():
-        '''
-        Returns an infinite stream of odds number from 0 to infinity
-
-        :return: the infinite stream
-        '''
-        return NumberStream(Stream.iterate(1, lambda i: i + 2))
-
-    @staticmethod
-    def evens():
-        '''
-        Returns an infinite stream of evens number from 0 to infinity
-
-        :return: the infinite stream
-        '''
-        return NumberStream(Stream.iterate(0, lambda i: i + 2))
-
-    @staticmethod
-    def primes():
-        '''
-        Returns an infinite stream of primes number from 2 to infinity
-
-        :return: the infinite stream
-        '''
-        def prime_generator():
-            yield 2
-            primes = [2]
-            actual = 1
-            while True:
-                actual += 2
-                for prime in primes:
-                    if actual % prime == 0:
-                        break
-                else:
-                    primes.append(actual)
-                    yield actual
-
-        return NumberStream(prime_generator())
-
-    @staticmethod
-    def randint(lower, upper):
-        '''
-        Returns an infinite stream of random integer in range [a, b], including both end points.
-
-        :param int lower: min value for random numbers
-        :param int upper: max value for random numbers
-        :return: the infinite random stream
-        '''
-        return NumberStream(Stream.generate(lambda: random.randint(lower, upper)))
-
-    @staticmethod
-    def random():
-        '''
-        Returns an infinite stream of random numbers in range [0, 1], including both end points.
-
-        :param int lower: min value for random numbers
-        :param int upper: max value for random numbers
-        :return: the infinite random stream
-        '''
-        return NumberStream(Stream.generate(random.random))
-
-    @staticmethod
-    def range(start, end, step=1):
-        '''
-        Returns a stream of numbers from start to end with specified step.
-
-        :param int start: the start value
-        :param int end: the max value
-        :param int step: the step
-        :return: the new stream
-        '''
-        return NumberStream(Stream.iterate(start, lambda i: i + step).takeWhile(lambda x: x <= end))
-
-    @staticmethod
-    def pi():
-        '''
-        Returns a stream with the digits of PI.
-
-        :return: the PI's digits stream
-        '''
-        def pi_digits():
-            "Generate n digits of Pi."
-            k, a, b, a1, b1 = 2, 4, 1, 12, 4
-            while True:
-                p, q, k = k * k, 2 * k + 1, k + 1
-                a, b, a1, b1 = a1, b1, p * a + q * a1, p * b + q * b1
-                d, d1 = a / b, a1 / b1
-                while d == d1:
-                    yield int(d)
-                    a, a1 = 10 * (a % b), 10 * (a1 % b1)
-                    d, d1 = a / b, a1 / b1
-
-        return NumberStream(Stream(pi_digits()))
-
-    def __init__(self, _stream):
-        if isinstance(_stream, Stream):
-            self.iterable = _stream.iterable
-        else:
-            self.iterable = _stream
-
-    def average(self):
-        '''
-        Get the average value of the element of the stream.
-
-        :return: the average value
-        '''
-        _sum = 0
-        _count = 0
-        for elem in self:
-            _sum += elem
-            _count += 1
-        return _sum / _count
-
-    def takeWhileSmallerThan(self, maximum):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller than the specified value
-
-        :return: self
-        '''
-        return self.takeWhile(lambda x: x < maximum)
-
-    def takeWhileSmallerOrEqualThan(self, maximum):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller or equal than the specified value
-
-        :return: self
-        '''
-        return self.takeWhile(lambda x: x <= maximum)
-
-    def takeWhileGreaterThan(self, minimum):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that is greater than the specified value
-
-        :return: self
-        '''
-        return self.takeWhile(lambda x: x > minimum)
-
-    def takeWhileGreaterOrEqualThan(self, minimum):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that is greater or equal than the specified value
-
-        :return: self
-        '''
-        return self.takeWhile(lambda x: x >= minimum)
-
-    def smallerThan(self, maximum):
-        '''
-        Returns a stream consisting of the elements of this stream that are smaller than the specified value
-
-        :param int maximum: the maximum value [exclusive]
-        :return: self
-        '''
-        return self.filter(lambda x: x < maximum)
-
-    def smallerOrEqualThan(self, maximum):
-        '''
-        Returns a stream consisting of the elements of this stream that are smaller or equal than the specified value
-
-        :param int maximum: the maximum value [inclusive]
-        :return: self
-        '''
-        return self.filter(lambda x: x <= maximum)
-
-    def greaterThan(self, minimum):
-        '''
-        Returns a stream consisting of the elements of this stream that are greater than the specified value
-
-        :param int minimum: the minimum value [exclusive]
-        :return: self
-        '''
-        return self.filter(lambda x: x > minimum)
-
-    def greaterOrEqualThan(self, minimum):
-        '''
-        Returns a stream consisting of the elements of this stream that are greater than the specified value
-
-        :param int minimum: the minimum value [inclusive]
-        :return: self
-        '''
-        return self.filter(lambda x: x >= minimum)
-
-    def multipleOf(self, number):
-        '''
-        Returns a stream consisting of the elements of this stream that are multiple of the specified value
-
-        :param int number: the value
-        :return: self
-        '''
-        return self.filter(lambda x: x % number == 0)
-
-    def square(self):
-        '''
-        Returns a stream consisting of the square of the element of this stream
-
-        :return: self
-        '''
-        return self.pow(2)
-
-    def cube(self):
-        '''
-        Returns a stream consisting of the cube of the element of this stream
-
-        :return: self
-        '''
-        return self.pow(3)
-
-    def pow(self, power):
-        '''
-        Returns a stream consisting of the pow to the specified value of the element of this stream
-
-        :return: self
-        '''
-        return self.map(lambda x: x ** power)
-
-    def sqrt(self):
-        '''
-        Returns a stream consisting of the square root of the element of this stream
-
-        :return: self
-        '''
-        return self.map(math.sqrt)
-
-    def log(self):
-        '''
-        Returns a stream consisting of the log of the element of this stream
-
-        :return: self
-        '''
-        return self.map(math.log)
-
-    def sin(self):
-        '''
-        Returns a stream consisting of the sin of the element of this stream
-
-        :return: self
-        '''
-        return self.map(math.sin)
-
-    def cos(self):
-        '''
-        Returns a stream consisting of the cos of the element of this stream
-
-        :return: self
-        '''
-        return self.map(math.cos)
-
- -
- -
-
-
- #   - - - class - NumberStream(stream.stream.Stream): -
- -
- View Source -
class NumberStream(Stream):
-
-    @staticmethod
-    def integers():
-        '''
-        Returns an infinite stream of integer from 0 to infinity
-
-        :return: the infinite stream
-        '''
-        return NumberStream(Stream.iterate(0, lambda i: i + 1))
-
-    @staticmethod
-    def odds():
-        '''
-        Returns an infinite stream of odds number from 0 to infinity
-
-        :return: the infinite stream
-        '''
-        return NumberStream(Stream.iterate(1, lambda i: i + 2))
-
-    @staticmethod
-    def evens():
-        '''
-        Returns an infinite stream of evens number from 0 to infinity
-
-        :return: the infinite stream
-        '''
-        return NumberStream(Stream.iterate(0, lambda i: i + 2))
-
-    @staticmethod
-    def primes():
-        '''
-        Returns an infinite stream of primes number from 2 to infinity
-
-        :return: the infinite stream
-        '''
-        def prime_generator():
-            yield 2
-            primes = [2]
-            actual = 1
-            while True:
-                actual += 2
-                for prime in primes:
-                    if actual % prime == 0:
-                        break
-                else:
-                    primes.append(actual)
-                    yield actual
-
-        return NumberStream(prime_generator())
-
-    @staticmethod
-    def randint(lower, upper):
-        '''
-        Returns an infinite stream of random integer in range [a, b], including both end points.
-
-        :param int lower: min value for random numbers
-        :param int upper: max value for random numbers
-        :return: the infinite random stream
-        '''
-        return NumberStream(Stream.generate(lambda: random.randint(lower, upper)))
-
-    @staticmethod
-    def random():
-        '''
-        Returns an infinite stream of random numbers in range [0, 1], including both end points.
-
-        :param int lower: min value for random numbers
-        :param int upper: max value for random numbers
-        :return: the infinite random stream
-        '''
-        return NumberStream(Stream.generate(random.random))
-
-    @staticmethod
-    def range(start, end, step=1):
-        '''
-        Returns a stream of numbers from start to end with specified step.
-
-        :param int start: the start value
-        :param int end: the max value
-        :param int step: the step
-        :return: the new stream
-        '''
-        return NumberStream(Stream.iterate(start, lambda i: i + step).takeWhile(lambda x: x <= end))
-
-    @staticmethod
-    def pi():
-        '''
-        Returns a stream with the digits of PI.
-
-        :return: the PI's digits stream
-        '''
-        def pi_digits():
-            "Generate n digits of Pi."
-            k, a, b, a1, b1 = 2, 4, 1, 12, 4
-            while True:
-                p, q, k = k * k, 2 * k + 1, k + 1
-                a, b, a1, b1 = a1, b1, p * a + q * a1, p * b + q * b1
-                d, d1 = a / b, a1 / b1
-                while d == d1:
-                    yield int(d)
-                    a, a1 = 10 * (a % b), 10 * (a1 % b1)
-                    d, d1 = a / b, a1 / b1
-
-        return NumberStream(Stream(pi_digits()))
-
-    def __init__(self, _stream):
-        if isinstance(_stream, Stream):
-            self.iterable = _stream.iterable
-        else:
-            self.iterable = _stream
-
-    def average(self):
-        '''
-        Get the average value of the element of the stream.
-
-        :return: the average value
-        '''
-        _sum = 0
-        _count = 0
-        for elem in self:
-            _sum += elem
-            _count += 1
-        return _sum / _count
-
-    def takeWhileSmallerThan(self, maximum):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller than the specified value
-
-        :return: self
-        '''
-        return self.takeWhile(lambda x: x < maximum)
-
-    def takeWhileSmallerOrEqualThan(self, maximum):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller or equal than the specified value
-
-        :return: self
-        '''
-        return self.takeWhile(lambda x: x <= maximum)
-
-    def takeWhileGreaterThan(self, minimum):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that is greater than the specified value
-
-        :return: self
-        '''
-        return self.takeWhile(lambda x: x > minimum)
-
-    def takeWhileGreaterOrEqualThan(self, minimum):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that is greater or equal than the specified value
-
-        :return: self
-        '''
-        return self.takeWhile(lambda x: x >= minimum)
-
-    def smallerThan(self, maximum):
-        '''
-        Returns a stream consisting of the elements of this stream that are smaller than the specified value
-
-        :param int maximum: the maximum value [exclusive]
-        :return: self
-        '''
-        return self.filter(lambda x: x < maximum)
-
-    def smallerOrEqualThan(self, maximum):
-        '''
-        Returns a stream consisting of the elements of this stream that are smaller or equal than the specified value
-
-        :param int maximum: the maximum value [inclusive]
-        :return: self
-        '''
-        return self.filter(lambda x: x <= maximum)
-
-    def greaterThan(self, minimum):
-        '''
-        Returns a stream consisting of the elements of this stream that are greater than the specified value
-
-        :param int minimum: the minimum value [exclusive]
-        :return: self
-        '''
-        return self.filter(lambda x: x > minimum)
-
-    def greaterOrEqualThan(self, minimum):
-        '''
-        Returns a stream consisting of the elements of this stream that are greater than the specified value
-
-        :param int minimum: the minimum value [inclusive]
-        :return: self
-        '''
-        return self.filter(lambda x: x >= minimum)
-
-    def multipleOf(self, number):
-        '''
-        Returns a stream consisting of the elements of this stream that are multiple of the specified value
-
-        :param int number: the value
-        :return: self
-        '''
-        return self.filter(lambda x: x % number == 0)
-
-    def square(self):
-        '''
-        Returns a stream consisting of the square of the element of this stream
-
-        :return: self
-        '''
-        return self.pow(2)
-
-    def cube(self):
-        '''
-        Returns a stream consisting of the cube of the element of this stream
-
-        :return: self
-        '''
-        return self.pow(3)
-
-    def pow(self, power):
-        '''
-        Returns a stream consisting of the pow to the specified value of the element of this stream
-
-        :return: self
-        '''
-        return self.map(lambda x: x ** power)
-
-    def sqrt(self):
-        '''
-        Returns a stream consisting of the square root of the element of this stream
-
-        :return: self
-        '''
-        return self.map(math.sqrt)
-
-    def log(self):
-        '''
-        Returns a stream consisting of the log of the element of this stream
-
-        :return: self
-        '''
-        return self.map(math.log)
-
-    def sin(self):
-        '''
-        Returns a stream consisting of the sin of the element of this stream
-
-        :return: self
-        '''
-        return self.map(math.sin)
-
-    def cos(self):
-        '''
-        Returns a stream consisting of the cos of the element of this stream
-
-        :return: self
-        '''
-        return self.map(math.cos)
-
- -
- -

A sequence of elements supporting sequential operations.

- -

The following example illustrates an aggregate operation using -Stream:

- -
result = Stream(elements)
-                .filter(lambda w: w.getColor() == RED)
-                .map(lambda w: w.getWeight())
-                .sum()
-
- + +
+
+
+

Module stream.numbers

+
+
+
+ +Expand source code + +
import math
+import random
+
+from .stream import Stream
+
+
+class NumberStream(Stream):
+
+    @staticmethod
+    def integers():
+        '''
+        Returns an infinite stream of integer from 0 to infinity
+
+        :return: the infinite stream
+        '''
+        return NumberStream(Stream.iterate(0, lambda i: i + 1))
+
+    @staticmethod
+    def odds():
+        '''
+        Returns an infinite stream of odds number from 0 to infinity
+
+        :return: the infinite stream
+        '''
+        return NumberStream(Stream.iterate(1, lambda i: i + 2))
+
+    @staticmethod
+    def evens():
+        '''
+        Returns an infinite stream of evens number from 0 to infinity
+
+        :return: the infinite stream
+        '''
+        return NumberStream(Stream.iterate(0, lambda i: i + 2))
+
+    @staticmethod
+    def primes():
+        '''
+        Returns an infinite stream of primes number from 2 to infinity
+
+        :return: the infinite stream
+        '''
+        def prime_generator():
+            yield 2
+            primes = [2]
+            actual = 1
+            while True:
+                actual += 2
+                for prime in primes:
+                    if actual % prime == 0:
+                        break
+                else:
+                    primes.append(actual)
+                    yield actual
+
+        return NumberStream(prime_generator())
+
+    @staticmethod
+    def randint(lower, upper):
+        '''
+        Returns an infinite stream of random integer in range [a, b], including both end points.
+
+        :param int lower: min value for random numbers
+        :param int upper: max value for random numbers
+        :return: the infinite random stream
+        '''
+        return NumberStream(Stream.generate(lambda: random.randint(lower, upper)))
+
+    @staticmethod
+    def random():
+        '''
+        Returns an infinite stream of random numbers in range [0, 1], including both end points.
+
+        :param int lower: min value for random numbers
+        :param int upper: max value for random numbers
+        :return: the infinite random stream
+        '''
+        return NumberStream(Stream.generate(random.random))
+
+    @staticmethod
+    def range(start, end, step=1):
+        '''
+        Returns a stream of numbers from start to end with specified step.
+
+        :param int start: the start value
+        :param int end: the max value
+        :param int step: the step
+        :return: the new stream
+        '''
+        return NumberStream(Stream.iterate(start, lambda i: i + step).takeWhile(lambda x: x <= end))
+
+    @staticmethod
+    def pi():
+        '''
+        Returns a stream with the digits of PI.
+
+        :return: the PI's digits stream
+        '''
+        def pi_digits():
+            "Generate n digits of Pi."
+            k, a, b, a1, b1 = 2, 4, 1, 12, 4
+            while True:
+                p, q, k = k * k, 2 * k + 1, k + 1
+                a, b, a1, b1 = a1, b1, p * a + q * a1, p * b + q * b1
+                d, d1 = a / b, a1 / b1
+                while d == d1:
+                    yield int(d)
+                    a, a1 = 10 * (a % b), 10 * (a1 % b1)
+                    d, d1 = a / b, a1 / b1
+
+        return NumberStream(Stream(pi_digits()))
+
+    def __init__(self, _stream):
+        if isinstance(_stream, Stream):
+            self.iterable = _stream.iterable
+        else:
+            self.iterable = _stream
+
+    def average(self):
+        '''
+        Get the average value of the element of the stream.
+
+        :return: the average value
+        '''
+        _sum = 0
+        _count = 0
+        for elem in self:
+            _sum += elem
+            _count += 1
+        return _sum / _count
+
+    def takeWhileSmallerThan(self, maximum):
+        '''
+        Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller than the specified value
+
+        :return: self
+        '''
+        return self.takeWhile(lambda x: x < maximum)
+
+    def takeWhileSmallerOrEqualThan(self, maximum):
+        '''
+        Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller or equal than the specified value
+
+        :return: self
+        '''
+        return self.takeWhile(lambda x: x <= maximum)
+
+    def takeWhileGreaterThan(self, minimum):
+        '''
+        Returns a stream consisting of the longest prefix of elements taken from this stream that is greater than the specified value
+
+        :return: self
+        '''
+        return self.takeWhile(lambda x: x > minimum)
+
+    def takeWhileGreaterOrEqualThan(self, minimum):
+        '''
+        Returns a stream consisting of the longest prefix of elements taken from this stream that is greater or equal than the specified value
+
+        :return: self
+        '''
+        return self.takeWhile(lambda x: x >= minimum)
+
+    def smallerThan(self, maximum):
+        '''
+        Returns a stream consisting of the elements of this stream that are smaller than the specified value
+
+        :param int maximum: the maximum value [exclusive]
+        :return: self
+        '''
+        return self.filter(lambda x: x < maximum)
+
+    def smallerOrEqualThan(self, maximum):
+        '''
+        Returns a stream consisting of the elements of this stream that are smaller or equal than the specified value
+
+        :param int maximum: the maximum value [inclusive]
+        :return: self
+        '''
+        return self.filter(lambda x: x <= maximum)
+
+    def greaterThan(self, minimum):
+        '''
+        Returns a stream consisting of the elements of this stream that are greater than the specified value
+
+        :param int minimum: the minimum value [exclusive]
+        :return: self
+        '''
+        return self.filter(lambda x: x > minimum)
+
+    def greaterOrEqualThan(self, minimum):
+        '''
+        Returns a stream consisting of the elements of this stream that are greater than the specified value
+
+        :param int minimum: the minimum value [inclusive]
+        :return: self
+        '''
+        return self.filter(lambda x: x >= minimum)
+
+    def multipleOf(self, number):
+        '''
+        Returns a stream consisting of the elements of this stream that are multiple of the specified value
+
+        :param int number: the value
+        :return: self
+        '''
+        return self.filter(lambda x: x % number == 0)
+
+    def square(self):
+        '''
+        Returns a stream consisting of the square of the element of this stream
+
+        :return: self
+        '''
+        return self.pow(2)
+
+    def cube(self):
+        '''
+        Returns a stream consisting of the cube of the element of this stream
+
+        :return: self
+        '''
+        return self.pow(3)
+
+    def pow(self, power):
+        '''
+        Returns a stream consisting of the pow to the specified value of the element of this stream
+
+        :return: self
+        '''
+        return self.map(lambda x: x ** power)
+
+    def sqrt(self):
+        '''
+        Returns a stream consisting of the square root of the element of this stream
+
+        :return: self
+        '''
+        return self.map(math.sqrt)
+
+    def log(self):
+        '''
+        Returns a stream consisting of the log of the element of this stream
+
+        :return: self
+        '''
+        return self.map(math.log)
+
+    def sin(self):
+        '''
+        Returns a stream consisting of the sin of the element of this stream
+
+        :return: self
+        '''
+        return self.map(math.sin)
+
+    def cos(self):
+        '''
+        Returns a stream consisting of the cos of the element of this stream
+
+        :return: self
+        '''
+        return self.map(math.cos)
+
+
+
+
+
+
+
+
+
+

Classes

+
+
+class NumberStream +(_stream) +
+
+

A sequence of elements supporting sequential operations.

+

The following example illustrates an aggregate operation using

+

Stream

+

result = Stream(elements) +.filter(lambda w: w.getColor() == RED) +.map(lambda w: w.getWeight()) +.sum()

A stream pipeline, like the elements example above, can be viewed as a query on the stream source.

- -

A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may raise Exception if it detects that the stream is being reused.

-
- - -
-
#   - - - NumberStream(_stream)
- -
- View Source -
    def __init__(self, _stream):
-        if isinstance(_stream, Stream):
-            self.iterable = _stream.iterable
-        else:
-            self.iterable = _stream
-
- -
- - - -
-
-
#   - -
@staticmethod
- - def - integers(): -
- -
- View Source -
    @staticmethod
-    def integers():
-        '''
-        Returns an infinite stream of integer from 0 to infinity
-
-        :return: the infinite stream
-        '''
-        return NumberStream(Stream.iterate(0, lambda i: i + 1))
-
- -
- -

Returns an infinite stream of integer from 0 to infinity

- -

:return: the infinite stream

-
- - -
-
-
#   - -
@staticmethod
- - def - odds(): -
- -
- View Source -
    @staticmethod
-    def odds():
-        '''
-        Returns an infinite stream of odds number from 0 to infinity
-
-        :return: the infinite stream
-        '''
-        return NumberStream(Stream.iterate(1, lambda i: i + 2))
-
- -
- -

Returns an infinite stream of odds number from 0 to infinity

- -

:return: the infinite stream

-
- - -
-
-
#   - -
@staticmethod
- - def - evens(): -
- -
- View Source -
    @staticmethod
-    def evens():
-        '''
-        Returns an infinite stream of evens number from 0 to infinity
-
-        :return: the infinite stream
-        '''
-        return NumberStream(Stream.iterate(0, lambda i: i + 2))
-
- -
- -

Returns an infinite stream of evens number from 0 to infinity

- -

:return: the infinite stream

-
- - -
-
-
#   - -
@staticmethod
- - def - primes(): -
- -
- View Source -
    @staticmethod
-    def primes():
-        '''
-        Returns an infinite stream of primes number from 2 to infinity
-
-        :return: the infinite stream
-        '''
-        def prime_generator():
-            yield 2
-            primes = [2]
-            actual = 1
-            while True:
-                actual += 2
-                for prime in primes:
-                    if actual % prime == 0:
-                        break
-                else:
-                    primes.append(actual)
-                    yield actual
-
-        return NumberStream(prime_generator())
-
- -
- -

Returns an infinite stream of primes number from 2 to infinity

- -

:return: the infinite stream

-
- - -
-
-
#   - -
@staticmethod
- - def - randint(lower, upper): -
- -
- View Source -
    @staticmethod
-    def randint(lower, upper):
-        '''
-        Returns an infinite stream of random integer in range [a, b], including both end points.
-
-        :param int lower: min value for random numbers
-        :param int upper: max value for random numbers
-        :return: the infinite random stream
-        '''
-        return NumberStream(Stream.generate(lambda: random.randint(lower, upper)))
-
- -
- -

Returns an infinite stream of random integer in range [a, b], including both end points.

- +

A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may raise Exception if it detects that the stream is being reused.

+
+ +Expand source code + +
class NumberStream(Stream):
+
+    @staticmethod
+    def integers():
+        '''
+        Returns an infinite stream of integer from 0 to infinity
+
+        :return: the infinite stream
+        '''
+        return NumberStream(Stream.iterate(0, lambda i: i + 1))
+
+    @staticmethod
+    def odds():
+        '''
+        Returns an infinite stream of odds number from 0 to infinity
+
+        :return: the infinite stream
+        '''
+        return NumberStream(Stream.iterate(1, lambda i: i + 2))
+
+    @staticmethod
+    def evens():
+        '''
+        Returns an infinite stream of evens number from 0 to infinity
+
+        :return: the infinite stream
+        '''
+        return NumberStream(Stream.iterate(0, lambda i: i + 2))
+
+    @staticmethod
+    def primes():
+        '''
+        Returns an infinite stream of primes number from 2 to infinity
+
+        :return: the infinite stream
+        '''
+        def prime_generator():
+            yield 2
+            primes = [2]
+            actual = 1
+            while True:
+                actual += 2
+                for prime in primes:
+                    if actual % prime == 0:
+                        break
+                else:
+                    primes.append(actual)
+                    yield actual
+
+        return NumberStream(prime_generator())
+
+    @staticmethod
+    def randint(lower, upper):
+        '''
+        Returns an infinite stream of random integer in range [a, b], including both end points.
+
+        :param int lower: min value for random numbers
+        :param int upper: max value for random numbers
+        :return: the infinite random stream
+        '''
+        return NumberStream(Stream.generate(lambda: random.randint(lower, upper)))
+
+    @staticmethod
+    def random():
+        '''
+        Returns an infinite stream of random numbers in range [0, 1], including both end points.
+
+        :param int lower: min value for random numbers
+        :param int upper: max value for random numbers
+        :return: the infinite random stream
+        '''
+        return NumberStream(Stream.generate(random.random))
+
+    @staticmethod
+    def range(start, end, step=1):
+        '''
+        Returns a stream of numbers from start to end with specified step.
+
+        :param int start: the start value
+        :param int end: the max value
+        :param int step: the step
+        :return: the new stream
+        '''
+        return NumberStream(Stream.iterate(start, lambda i: i + step).takeWhile(lambda x: x <= end))
+
+    @staticmethod
+    def pi():
+        '''
+        Returns a stream with the digits of PI.
+
+        :return: the PI's digits stream
+        '''
+        def pi_digits():
+            "Generate n digits of Pi."
+            k, a, b, a1, b1 = 2, 4, 1, 12, 4
+            while True:
+                p, q, k = k * k, 2 * k + 1, k + 1
+                a, b, a1, b1 = a1, b1, p * a + q * a1, p * b + q * b1
+                d, d1 = a / b, a1 / b1
+                while d == d1:
+                    yield int(d)
+                    a, a1 = 10 * (a % b), 10 * (a1 % b1)
+                    d, d1 = a / b, a1 / b1
+
+        return NumberStream(Stream(pi_digits()))
+
+    def __init__(self, _stream):
+        if isinstance(_stream, Stream):
+            self.iterable = _stream.iterable
+        else:
+            self.iterable = _stream
+
+    def average(self):
+        '''
+        Get the average value of the element of the stream.
+
+        :return: the average value
+        '''
+        _sum = 0
+        _count = 0
+        for elem in self:
+            _sum += elem
+            _count += 1
+        return _sum / _count
+
+    def takeWhileSmallerThan(self, maximum):
+        '''
+        Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller than the specified value
+
+        :return: self
+        '''
+        return self.takeWhile(lambda x: x < maximum)
+
+    def takeWhileSmallerOrEqualThan(self, maximum):
+        '''
+        Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller or equal than the specified value
+
+        :return: self
+        '''
+        return self.takeWhile(lambda x: x <= maximum)
+
+    def takeWhileGreaterThan(self, minimum):
+        '''
+        Returns a stream consisting of the longest prefix of elements taken from this stream that is greater than the specified value
+
+        :return: self
+        '''
+        return self.takeWhile(lambda x: x > minimum)
+
+    def takeWhileGreaterOrEqualThan(self, minimum):
+        '''
+        Returns a stream consisting of the longest prefix of elements taken from this stream that is greater or equal than the specified value
+
+        :return: self
+        '''
+        return self.takeWhile(lambda x: x >= minimum)
+
+    def smallerThan(self, maximum):
+        '''
+        Returns a stream consisting of the elements of this stream that are smaller than the specified value
+
+        :param int maximum: the maximum value [exclusive]
+        :return: self
+        '''
+        return self.filter(lambda x: x < maximum)
+
+    def smallerOrEqualThan(self, maximum):
+        '''
+        Returns a stream consisting of the elements of this stream that are smaller or equal than the specified value
+
+        :param int maximum: the maximum value [inclusive]
+        :return: self
+        '''
+        return self.filter(lambda x: x <= maximum)
+
+    def greaterThan(self, minimum):
+        '''
+        Returns a stream consisting of the elements of this stream that are greater than the specified value
+
+        :param int minimum: the minimum value [exclusive]
+        :return: self
+        '''
+        return self.filter(lambda x: x > minimum)
+
+    def greaterOrEqualThan(self, minimum):
+        '''
+        Returns a stream consisting of the elements of this stream that are greater than the specified value
+
+        :param int minimum: the minimum value [inclusive]
+        :return: self
+        '''
+        return self.filter(lambda x: x >= minimum)
+
+    def multipleOf(self, number):
+        '''
+        Returns a stream consisting of the elements of this stream that are multiple of the specified value
+
+        :param int number: the value
+        :return: self
+        '''
+        return self.filter(lambda x: x % number == 0)
+
+    def square(self):
+        '''
+        Returns a stream consisting of the square of the element of this stream
+
+        :return: self
+        '''
+        return self.pow(2)
+
+    def cube(self):
+        '''
+        Returns a stream consisting of the cube of the element of this stream
+
+        :return: self
+        '''
+        return self.pow(3)
+
+    def pow(self, power):
+        '''
+        Returns a stream consisting of the pow to the specified value of the element of this stream
+
+        :return: self
+        '''
+        return self.map(lambda x: x ** power)
+
+    def sqrt(self):
+        '''
+        Returns a stream consisting of the square root of the element of this stream
+
+        :return: self
+        '''
+        return self.map(math.sqrt)
+
+    def log(self):
+        '''
+        Returns a stream consisting of the log of the element of this stream
+
+        :return: self
+        '''
+        return self.map(math.log)
+
+    def sin(self):
+        '''
+        Returns a stream consisting of the sin of the element of this stream
+
+        :return: self
+        '''
+        return self.map(math.sin)
+
+    def cos(self):
+        '''
+        Returns a stream consisting of the cos of the element of this stream
+
+        :return: self
+        '''
+        return self.map(math.cos)
+
+

Ancestors

+ +

Static methods

+
+
+def evens() +
+
+

Returns an infinite stream of evens number from 0 to infinity

+

:return: the infinite stream

+
+ +Expand source code + +
@staticmethod
+def evens():
+    '''
+    Returns an infinite stream of evens number from 0 to infinity
+
+    :return: the infinite stream
+    '''
+    return NumberStream(Stream.iterate(0, lambda i: i + 2))
+
+
+
+def integers() +
+
+

Returns an infinite stream of integer from 0 to infinity

+

:return: the infinite stream

+
+ +Expand source code + +
@staticmethod
+def integers():
+    '''
+    Returns an infinite stream of integer from 0 to infinity
+
+    :return: the infinite stream
+    '''
+    return NumberStream(Stream.iterate(0, lambda i: i + 1))
+
+
+
+def odds() +
+
+

Returns an infinite stream of odds number from 0 to infinity

+

:return: the infinite stream

+
+ +Expand source code + +
@staticmethod
+def odds():
+    '''
+    Returns an infinite stream of odds number from 0 to infinity
+
+    :return: the infinite stream
+    '''
+    return NumberStream(Stream.iterate(1, lambda i: i + 2))
+
+
+
+def pi() +
+
+

Returns a stream with the digits of PI.

+

:return: the PI's digits stream

+
+ +Expand source code + +
@staticmethod
+def pi():
+    '''
+    Returns a stream with the digits of PI.
+
+    :return: the PI's digits stream
+    '''
+    def pi_digits():
+        "Generate n digits of Pi."
+        k, a, b, a1, b1 = 2, 4, 1, 12, 4
+        while True:
+            p, q, k = k * k, 2 * k + 1, k + 1
+            a, b, a1, b1 = a1, b1, p * a + q * a1, p * b + q * b1
+            d, d1 = a / b, a1 / b1
+            while d == d1:
+                yield int(d)
+                a, a1 = 10 * (a % b), 10 * (a1 % b1)
+                d, d1 = a / b, a1 / b1
+
+    return NumberStream(Stream(pi_digits()))
+
+
+
+def primes() +
+
+

Returns an infinite stream of primes number from 2 to infinity

+

:return: the infinite stream

+
+ +Expand source code + +
@staticmethod
+def primes():
+    '''
+    Returns an infinite stream of primes number from 2 to infinity
+
+    :return: the infinite stream
+    '''
+    def prime_generator():
+        yield 2
+        primes = [2]
+        actual = 1
+        while True:
+            actual += 2
+            for prime in primes:
+                if actual % prime == 0:
+                    break
+            else:
+                primes.append(actual)
+                yield actual
+
+    return NumberStream(prime_generator())
+
+
+
+def randint(lower, upper) +
+
+

Returns an infinite stream of random integer in range [a, b], including both end points.

:param int lower: min value for random numbers :param int upper: max value for random numbers -:return: the infinite random stream

-
- - -
-
-
#   - -
@staticmethod
- - def - random(): -
- -
- View Source -
    @staticmethod
-    def random():
-        '''
-        Returns an infinite stream of random numbers in range [0, 1], including both end points.
-
-        :param int lower: min value for random numbers
-        :param int upper: max value for random numbers
-        :return: the infinite random stream
-        '''
-        return NumberStream(Stream.generate(random.random))
-
- -
- -

Returns an infinite stream of random numbers in range [0, 1], including both end points.

- +:return: the infinite random stream

+
+ +Expand source code + +
@staticmethod
+def randint(lower, upper):
+    '''
+    Returns an infinite stream of random integer in range [a, b], including both end points.
+
+    :param int lower: min value for random numbers
+    :param int upper: max value for random numbers
+    :return: the infinite random stream
+    '''
+    return NumberStream(Stream.generate(lambda: random.randint(lower, upper)))
+
+
+
+def random() +
+
+

Returns an infinite stream of random numbers in range [0, 1], including both end points.

:param int lower: min value for random numbers :param int upper: max value for random numbers -:return: the infinite random stream

-
- - -
-
-
#   - -
@staticmethod
- - def - range(start, end, step=1): -
- -
- View Source -
    @staticmethod
-    def range(start, end, step=1):
-        '''
-        Returns a stream of numbers from start to end with specified step.
-
-        :param int start: the start value
-        :param int end: the max value
-        :param int step: the step
-        :return: the new stream
-        '''
-        return NumberStream(Stream.iterate(start, lambda i: i + step).takeWhile(lambda x: x <= end))
-
- -
- -

Returns a stream of numbers from start to end with specified step.

- +:return: the infinite random stream

+
+ +Expand source code + +
@staticmethod
+def random():
+    '''
+    Returns an infinite stream of random numbers in range [0, 1], including both end points.
+
+    :param int lower: min value for random numbers
+    :param int upper: max value for random numbers
+    :return: the infinite random stream
+    '''
+    return NumberStream(Stream.generate(random.random))
+
+ +
+def range(start, end, step=1) +
+
+

Returns a stream of numbers from start to end with specified step.

:param int start: the start value :param int end: the max value :param int step: the step -:return: the new stream

-
- - -
-
-
#   - -
@staticmethod
- - def - pi(): -
- -
- View Source -
    @staticmethod
-    def pi():
-        '''
-        Returns a stream with the digits of PI.
-
-        :return: the PI's digits stream
-        '''
-        def pi_digits():
-            "Generate n digits of Pi."
-            k, a, b, a1, b1 = 2, 4, 1, 12, 4
-            while True:
-                p, q, k = k * k, 2 * k + 1, k + 1
-                a, b, a1, b1 = a1, b1, p * a + q * a1, p * b + q * b1
-                d, d1 = a / b, a1 / b1
-                while d == d1:
-                    yield int(d)
-                    a, a1 = 10 * (a % b), 10 * (a1 % b1)
-                    d, d1 = a / b, a1 / b1
-
-        return NumberStream(Stream(pi_digits()))
-
- -
- -

Returns a stream with the digits of PI.

- -

:return: the PI's digits stream

-
- - -
-
-
#   - - - def - average(self): -
- -
- View Source -
    def average(self):
-        '''
-        Get the average value of the element of the stream.
-
-        :return: the average value
-        '''
-        _sum = 0
-        _count = 0
-        for elem in self:
-            _sum += elem
-            _count += 1
-        return _sum / _count
-
- -
- -

Get the average value of the element of the stream.

- -

:return: the average value

-
- - -
-
-
#   - - - def - takeWhileSmallerThan(self, maximum): -
- -
- View Source -
    def takeWhileSmallerThan(self, maximum):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller than the specified value
-
-        :return: self
-        '''
-        return self.takeWhile(lambda x: x < maximum)
-
- -
- -

Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller than the specified value

- -

:return: self

-
- - -
-
-
#   - - - def - takeWhileSmallerOrEqualThan(self, maximum): -
- -
- View Source -
    def takeWhileSmallerOrEqualThan(self, maximum):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller or equal than the specified value
-
-        :return: self
-        '''
-        return self.takeWhile(lambda x: x <= maximum)
-
- -
- -

Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller or equal than the specified value

- -

:return: self

-
- - -
-
-
#   - - - def - takeWhileGreaterThan(self, minimum): -
- -
- View Source -
    def takeWhileGreaterThan(self, minimum):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that is greater than the specified value
-
-        :return: self
-        '''
-        return self.takeWhile(lambda x: x > minimum)
-
- -
- -

Returns a stream consisting of the longest prefix of elements taken from this stream that is greater than the specified value

- -

:return: self

-
- - -
-
-
#   - - - def - takeWhileGreaterOrEqualThan(self, minimum): -
- -
- View Source -
    def takeWhileGreaterOrEqualThan(self, minimum):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that is greater or equal than the specified value
-
-        :return: self
-        '''
-        return self.takeWhile(lambda x: x >= minimum)
-
- -
- -

Returns a stream consisting of the longest prefix of elements taken from this stream that is greater or equal than the specified value

- -

:return: self

-
- - -
-
-
#   - - - def - smallerThan(self, maximum): -
- -
- View Source -
    def smallerThan(self, maximum):
-        '''
-        Returns a stream consisting of the elements of this stream that are smaller than the specified value
-
-        :param int maximum: the maximum value [exclusive]
-        :return: self
-        '''
-        return self.filter(lambda x: x < maximum)
-
- -
- -

Returns a stream consisting of the elements of this stream that are smaller than the specified value

- -

:param int maximum: the maximum value [exclusive] -:return: self

-
- - -
-
-
#   - - - def - smallerOrEqualThan(self, maximum): -
- -
- View Source -
    def smallerOrEqualThan(self, maximum):
-        '''
-        Returns a stream consisting of the elements of this stream that are smaller or equal than the specified value
-
-        :param int maximum: the maximum value [inclusive]
-        :return: self
-        '''
-        return self.filter(lambda x: x <= maximum)
-
- -
- -

Returns a stream consisting of the elements of this stream that are smaller or equal than the specified value

- -

:param int maximum: the maximum value [inclusive] -:return: self

-
- - -
-
-
#   - - - def - greaterThan(self, minimum): -
- -
- View Source -
    def greaterThan(self, minimum):
-        '''
-        Returns a stream consisting of the elements of this stream that are greater than the specified value
-
-        :param int minimum: the minimum value [exclusive]
-        :return: self
-        '''
-        return self.filter(lambda x: x > minimum)
-
- -
- -

Returns a stream consisting of the elements of this stream that are greater than the specified value

- -

:param int minimum: the minimum value [exclusive] -:return: self

-
- - -
-
-
#   - - - def - greaterOrEqualThan(self, minimum): -
- -
- View Source -
    def greaterOrEqualThan(self, minimum):
-        '''
-        Returns a stream consisting of the elements of this stream that are greater than the specified value
-
-        :param int minimum: the minimum value [inclusive]
-        :return: self
-        '''
-        return self.filter(lambda x: x >= minimum)
-
- -
- -

Returns a stream consisting of the elements of this stream that are greater than the specified value

- +:return: the new stream

+
+ +Expand source code + +
@staticmethod
+def range(start, end, step=1):
+    '''
+    Returns a stream of numbers from start to end with specified step.
+
+    :param int start: the start value
+    :param int end: the max value
+    :param int step: the step
+    :return: the new stream
+    '''
+    return NumberStream(Stream.iterate(start, lambda i: i + step).takeWhile(lambda x: x <= end))
+
+ + +

Methods

+
+
+def average(self) +
+
+

Get the average value of the element of the stream.

+

:return: the average value

+
+ +Expand source code + +
def average(self):
+    '''
+    Get the average value of the element of the stream.
+
+    :return: the average value
+    '''
+    _sum = 0
+    _count = 0
+    for elem in self:
+        _sum += elem
+        _count += 1
+    return _sum / _count
+
+
+
+def cos(self) +
+
+

Returns a stream consisting of the cos of the element of this stream

+

:return: self

+
+ +Expand source code + +
def cos(self):
+    '''
+    Returns a stream consisting of the cos of the element of this stream
+
+    :return: self
+    '''
+    return self.map(math.cos)
+
+
+
+def cube(self) +
+
+

Returns a stream consisting of the cube of the element of this stream

+

:return: self

+
+ +Expand source code + +
def cube(self):
+    '''
+    Returns a stream consisting of the cube of the element of this stream
+
+    :return: self
+    '''
+    return self.pow(3)
+
+
+
+def greaterOrEqualThan(self, minimum) +
+
+

Returns a stream consisting of the elements of this stream that are greater than the specified value

:param int minimum: the minimum value [inclusive] -:return: self

-
- - -
-
-
#   - - - def - multipleOf(self, number): -
- -
- View Source -
    def multipleOf(self, number):
-        '''
-        Returns a stream consisting of the elements of this stream that are multiple of the specified value
-
-        :param int number: the value
-        :return: self
-        '''
-        return self.filter(lambda x: x % number == 0)
-
- -
- -

Returns a stream consisting of the elements of this stream that are multiple of the specified value

- +:return: self

+
+ +Expand source code + +
def greaterOrEqualThan(self, minimum):
+    '''
+    Returns a stream consisting of the elements of this stream that are greater than the specified value
+
+    :param int minimum: the minimum value [inclusive]
+    :return: self
+    '''
+    return self.filter(lambda x: x >= minimum)
+
+ +
+def greaterThan(self, minimum) +
+
+

Returns a stream consisting of the elements of this stream that are greater than the specified value

+

:param int minimum: the minimum value [exclusive] +:return: self

+
+ +Expand source code + +
def greaterThan(self, minimum):
+    '''
+    Returns a stream consisting of the elements of this stream that are greater than the specified value
+
+    :param int minimum: the minimum value [exclusive]
+    :return: self
+    '''
+    return self.filter(lambda x: x > minimum)
+
+
+
+def log(self) +
+
+

Returns a stream consisting of the log of the element of this stream

+

:return: self

+
+ +Expand source code + +
def log(self):
+    '''
+    Returns a stream consisting of the log of the element of this stream
+
+    :return: self
+    '''
+    return self.map(math.log)
+
+
+
+def multipleOf(self, number) +
+
+

Returns a stream consisting of the elements of this stream that are multiple of the specified value

:param int number: the value -:return: self

-
- - -
-
-
#   - - - def - square(self): -
- -
- View Source -
    def square(self):
-        '''
-        Returns a stream consisting of the square of the element of this stream
-
-        :return: self
-        '''
-        return self.pow(2)
-
- -
- -

Returns a stream consisting of the square of the element of this stream

- -

:return: self

-
- - -
-
-
#   - - - def - cube(self): -
- -
- View Source -
    def cube(self):
-        '''
-        Returns a stream consisting of the cube of the element of this stream
-
-        :return: self
-        '''
-        return self.pow(3)
-
- -
- -

Returns a stream consisting of the cube of the element of this stream

- -

:return: self

-
- - -
-
-
#   - - - def - pow(self, power): -
- -
- View Source -
    def pow(self, power):
-        '''
-        Returns a stream consisting of the pow to the specified value of the element of this stream
-
-        :return: self
-        '''
-        return self.map(lambda x: x ** power)
-
- -
- -

Returns a stream consisting of the pow to the specified value of the element of this stream

- -

:return: self

-
- - -
-
-
#   - - - def - sqrt(self): -
- -
- View Source -
    def sqrt(self):
-        '''
-        Returns a stream consisting of the square root of the element of this stream
-
-        :return: self
-        '''
-        return self.map(math.sqrt)
-
- -
- -

Returns a stream consisting of the square root of the element of this stream

- -

:return: self

-
- - -
-
-
#   - - - def - log(self): -
- -
- View Source -
    def log(self):
-        '''
-        Returns a stream consisting of the log of the element of this stream
-
-        :return: self
-        '''
-        return self.map(math.log)
-
- -
- -

Returns a stream consisting of the log of the element of this stream

- -

:return: self

-
- - -
-
-
#   - - - def - sin(self): -
- -
- View Source -
    def sin(self):
-        '''
-        Returns a stream consisting of the sin of the element of this stream
-
-        :return: self
-        '''
-        return self.map(math.sin)
-
- -
- -

Returns a stream consisting of the sin of the element of this stream

- -

:return: self

-
- - -
-
-
#   - - - def - cos(self): -
- -
- View Source -
    def cos(self):
-        '''
-        Returns a stream consisting of the cos of the element of this stream
-
-        :return: self
-        '''
-        return self.map(math.cos)
-
- -
- -

Returns a stream consisting of the cos of the element of this stream

- -

:return: self

+:return: self

+
+ +Expand source code + +
def multipleOf(self, number):
+    '''
+    Returns a stream consisting of the elements of this stream that are multiple of the specified value
+
+    :param int number: the value
+    :return: self
+    '''
+    return self.filter(lambda x: x % number == 0)
+
+ +
+def pow(self, power) +
+
+

Returns a stream consisting of the pow to the specified value of the element of this stream

+

:return: self

+
+ +Expand source code + +
def pow(self, power):
+    '''
+    Returns a stream consisting of the pow to the specified value of the element of this stream
+
+    :return: self
+    '''
+    return self.map(lambda x: x ** power)
+
+
+
+def sin(self) +
+
+

Returns a stream consisting of the sin of the element of this stream

+

:return: self

+
+ +Expand source code + +
def sin(self):
+    '''
+    Returns a stream consisting of the sin of the element of this stream
+
+    :return: self
+    '''
+    return self.map(math.sin)
+
+
+
+def smallerOrEqualThan(self, maximum) +
+
+

Returns a stream consisting of the elements of this stream that are smaller or equal than the specified value

+

:param int maximum: the maximum value [inclusive] +:return: self

+
+ +Expand source code + +
def smallerOrEqualThan(self, maximum):
+    '''
+    Returns a stream consisting of the elements of this stream that are smaller or equal than the specified value
+
+    :param int maximum: the maximum value [inclusive]
+    :return: self
+    '''
+    return self.filter(lambda x: x <= maximum)
+
+
+
+def smallerThan(self, maximum) +
+
+

Returns a stream consisting of the elements of this stream that are smaller than the specified value

+

:param int maximum: the maximum value [exclusive] +:return: self

+
+ +Expand source code + +
def smallerThan(self, maximum):
+    '''
+    Returns a stream consisting of the elements of this stream that are smaller than the specified value
+
+    :param int maximum: the maximum value [exclusive]
+    :return: self
+    '''
+    return self.filter(lambda x: x < maximum)
+
+
+
+def sqrt(self) +
+
+

Returns a stream consisting of the square root of the element of this stream

+

:return: self

+
+ +Expand source code + +
def sqrt(self):
+    '''
+    Returns a stream consisting of the square root of the element of this stream
+
+    :return: self
+    '''
+    return self.map(math.sqrt)
+
+
+
+def square(self) +
+
+

Returns a stream consisting of the square of the element of this stream

+

:return: self

+
+ +Expand source code + +
def square(self):
+    '''
+    Returns a stream consisting of the square of the element of this stream
+
+    :return: self
+    '''
+    return self.pow(2)
+
+
+
+def takeWhileGreaterOrEqualThan(self, minimum) +
+
+

Returns a stream consisting of the longest prefix of elements taken from this stream that is greater or equal than the specified value

+

:return: self

+
+ +Expand source code + +
def takeWhileGreaterOrEqualThan(self, minimum):
+    '''
+    Returns a stream consisting of the longest prefix of elements taken from this stream that is greater or equal than the specified value
+
+    :return: self
+    '''
+    return self.takeWhile(lambda x: x >= minimum)
+
+
+
+def takeWhileGreaterThan(self, minimum) +
+
+

Returns a stream consisting of the longest prefix of elements taken from this stream that is greater than the specified value

+

:return: self

+
+ +Expand source code + +
def takeWhileGreaterThan(self, minimum):
+    '''
+    Returns a stream consisting of the longest prefix of elements taken from this stream that is greater than the specified value
+
+    :return: self
+    '''
+    return self.takeWhile(lambda x: x > minimum)
+
+
+
+def takeWhileSmallerOrEqualThan(self, maximum) +
+
+

Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller or equal than the specified value

+

:return: self

+
+ +Expand source code + +
def takeWhileSmallerOrEqualThan(self, maximum):
+    '''
+    Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller or equal than the specified value
+
+    :return: self
+    '''
+    return self.takeWhile(lambda x: x <= maximum)
+
+
+
+def takeWhileSmallerThan(self, maximum) +
+
+

Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller than the specified value

+

:return: self

+
+ +Expand source code + +
def takeWhileSmallerThan(self, maximum):
+    '''
+    Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller than the specified value
+
+    :return: self
+    '''
+    return self.takeWhile(lambda x: x < maximum)
+
+
+ +

Inherited members

+ + + +
+ +
+ + + + \ No newline at end of file diff --git a/doc/stream/stream.html b/doc/stream/stream.html index 470e954..f69dbaf 100644 --- a/doc/stream/stream.html +++ b/doc/stream/stream.html @@ -1,1929 +1,1580 @@ - - - - stream.stream API documentation - - - - - - + + + +stream.stream API documentation + + + + + + + + + - -
-
-

-stream.stream

- - -
- View Source -
from functools import cmp_to_key
-import random
-
-from .util.iterators import IteratorUtils
-from .util.optional import Optional
-
-
-class Stream():
-
-    """
-    A sequence of elements supporting sequential operations.
-
-    The following example illustrates an aggregate operation using
-    Stream:
-
-        result = Stream(elements)
-                        .filter(lambda w: w.getColor() == RED)
-                        .map(lambda w: w.getWeight())
-                        .sum()
-
-    A stream pipeline, like the elements example above, can be viewed as a query on the stream source.
-
-    A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may raise Exception if it detects that the stream is being reused.
-    """
-
-    """
-    Static Methods
-    """
-
-    @staticmethod
-    def empty():
-        '''
-        Returns an empty sequential Stream.
-
-        :return: an empty sequential stream
-        '''
-        return Stream([])
-
-    @staticmethod
-    def of(elem):
-        '''
-        Returns a sequential Stream containing a single element.
-
-        :param T elem: the single element
-        :return: a singleton sequential stream
-        '''
-        return Stream(iter([elem]))
-
-    @staticmethod
-    def of(*elements):
-        '''
-        Returns a sequential ordered stream whose elements are the specified values.
-
-        :param *T elements: the elements of the new stream
-        :return: the new stream
-        '''
-        return Stream(iter(list(elements)))
-
-    @staticmethod
-    def ofNullable(elem):
-        '''
-        Returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.
-
-        :param T elem: the single element
-        :return: a stream with a single element if the specified element is non-null, otherwise an empty stream
-        '''
-        return Stream.of(elem) if elem is not None else Stream.empty()
-
-    @staticmethod
-    def iterate(seed, operator):
-        '''
-        Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.
-
-        :param T seed: the initial element
-        :param UnaryOperator operator: a function to be applied to the previous element to produce a new element
-        :return: a new sequential Stream
-        '''
-        return Stream(IteratorUtils.iterate(seed, operator))
-
-    @staticmethod
-    def generate(supplier):
-        '''
-        Returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc.
-
-        :param Supplier supplier: the Supplier of generated elements
-        :return: a new infinite sequential unordered Stream
-        '''
-        return Stream(IteratorUtils.generate(supplier))
-
-    @staticmethod
-    def concat(*streams):
-        '''
-        Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream and so on.
-
-        :param *Stream streams: the streams to concat
-        :return: the concatenation of the input streams
-        '''
-        return Stream(IteratorUtils.concat(*streams))
-
-    @staticmethod
-    def constant(element):
-        '''
-        Return an infinite sequential stream where each element is the passed element
-
-        :param T elem: the element
-        :return: the new stream made of @element
-        '''
-        return Stream.generate(lambda: element)
-
-    """
-    Normal Methods
-    """
-
-    def __init__(self, iterable):
-        self.iterable = iterable
-
-    def filter(self, predicate):
-        '''
-        Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
-
-        :param function predicate: predicate to apply to each element to determine if it should be included
-        :return: self
-        '''
-        self.iterable = IteratorUtils.filter(self.iterable, predicate)
-        return self
-
-    def map(self, mapper):
-        '''
-        Returns a stream consisting of the results of applying the given function to the elements of this stream.
-
-        :param function mapper: function to apply to each element
-        :return: self
-        '''
-        self.iterable = IteratorUtils.map(self.iterable, mapper)
-        return self
-
-    def flatMap(self, flatMapper):
-        '''
-        Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)
-
-        :param function flatMapper: function to apply to each element which produces a stream of new values
-        :return: self
-        '''
-        self.iterable = IteratorUtils.flatMap(self.iterable, flatMapper)
-        return self
-
-    def distinct(self):
-        '''
-        Returns a stream consisting of the distinct elements of this stream.
-
-        :return: self
-        '''
-        self.iterable = IteratorUtils.distinct(self.iterable)
-        return self
-
-    def limit(self, count):
-        '''
-        Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
-
-        :param int count:  the number of elements the stream should be limited to
-        :return: self
-        '''
-        self.iterable = IteratorUtils.limit(self.iterable, count)
-        return self
-
-    def skip(self, count):
-        '''
-        Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.
-
-        :param int count:  the number of leading elements to skip
-        :return: self
-        '''
-        self.iterable = IteratorUtils.skip(self.iterable, count)
-        return self
-
-    def takeWhile(self, predicate):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.
-
-        :param Predicate predicate:  predicate to apply to elements to determine the longest prefix of elements.
-        :return: self
-        '''
-        self.iterable = IteratorUtils.takeWhile(self.iterable, predicate)
-        return self
-
-    def dropWhile(self, predicate):
-        '''
-        Returns a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.
-
-        :param Predicate predicate:  predicate to apply to elements to determine the longest prefix of elements.
-        :return: self
-        '''
-        self.iterable = IteratorUtils.dropWhile(self.iterable, predicate)
-        return self
-
-    """
-    From here this method mustn't be called on infinite stream
-    """
-
-    def sorted(self, comparator=None):
-        '''
-        Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.
-
-        :param Comparator comparator: Comparator to be used to compare stream elements - if null default comparator is used
-        :return: self
-        '''
-        self.iterable = iter(sorted(
-            self.iterable, key=cmp_to_key(comparator))) if comparator is not None else iter(sorted(
-                self.iterable))
-        return self
-
-    def peek(self, consumer):
-        '''
-        Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
-
-        :param Consumer consumer: action to perform on the elements as they are consumed from the stream
-        :return: self
-        '''
-        self.iterable = IteratorUtils.peek(self.iterable, consumer)
-        return self
-
-    def forEach(self, function):
-        '''
-        Performs an action for each element of this stream.
-
-        :param Function function: action to perform on the elements
-        :return: None
-        '''
-        for elem in self.iterable:
-            function(elem)
-
-    def anyMatch(self, predicate):
-        '''
-        Returns whether any elements of this stream match the provided predicate.
-
-        :param Predicate predicate: predicate to apply to elements of this stream
-        :return: True if any elements of the stream match the provided predicate, otherwise False
-        '''
-        return any([predicate(elem) for elem in self.iterable])
-
-    def allMatch(self, predicate):
-        '''
-        Returns whether all elements of this stream match the provided predicate.
-
-        :param Predicate predicate: predicate to apply to elements of this stream
-        :return: True if either all elements of the stream match the provided predicate or the stream is empty, otherwise False
-        '''
-        return all([predicate(elem) for elem in self.iterable])
-
-    def noneMatch(self, predicate):
-        '''
-        Returns whether no elements of this stream match the provided predicate.
-
-        :param Predicate predicate: predicate to apply to elements of this stream
-        :return: True if either no elements of the stream match the provided predicate or the stream is empty, otherwise False
-        '''
-        return not self.anyMatch(predicate)
-
-    def findFirst(self):
-        '''
-        Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned.
-
-        :return: an Optional describing the first element of this stream, or an empty Optional if the stream is empty
-        '''
-        for elem in self.iterable:
-            return Optional.of(elem)
-        return Optional.ofNullable(None)
-
-    def findAny(self):
-        '''
-        Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.
-
-        :return: an Optional describing some element of this stream, or an empty Optional if the stream is empty
-        '''
-        return self.findFirst()
-
-    def reduce(self, accumulator, identity=None):
-        '''
-        Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.
-
-        :param T identity: the identity value for the accumulating function - if not specified it will be the first element of the stream
-        :param Accumulator accumulator: function for combining two values
-        :return: the result of reduction
-        '''
-        result = identity
-        for elem in self.iterable:
-            if(result is None):
-                result = elem
-            else:
-                result = accumulator(result, elem)
-        return Optional.ofNullable(result)
-
-    def min(self, comparator=None):
-        '''
-        Returns the minimum element of this stream according to the provided Comparator. This is a special case of a reduction.
-
-        :param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used
-        :return: an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty
-        '''
-        elements = list(self.iterable)
-        if len(elements) == 0:
-            return Optional.empty()
-
-        return Optional.ofNullable(min(elements, key=cmp_to_key(comparator), default=None)) if comparator is not None else Optional.ofNullable(min(elements, default=None))
-
-    def max(self, comparator=None):
-        '''
-        Returns the maximum element of this stream according to the provided Comparator. This is a special case of a reduction.
-
-        :param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used
-        :return: an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty
-        '''
-        elements = list(self.iterable)
-        if len(elements) == 0:
-            return Optional.empty()
-        return Optional.ofNullable(max(elements, key=cmp_to_key(comparator))) if comparator is not None else Optional.ofNullable(max(elements))
-
-    def sum(self):
-        '''
-        Returns the sum of all elements of this stream. This is a special case of a reduction.
-
-        :return: an Optional describing the sum of all the elements of this stream, or an empty Optional if the stream is empty
-        '''
-        return self.reduce(lambda x, y: x + y)
-
-    def count(self):
-        '''
-        Returns the count of elements in this stream. This is a special case of a reduction.
-
-        :return: the count of elements in this stream
-        '''
-        count = 0
-        for elem in self.iterable:
-            count += 1
-
-        return count
-
-    def toList(self):
-        '''
-        Returns a list with the elements in this stream.
-
-        :return: the list of elements in this stream
-        '''
-        return list(self.iterable)
-
-    def toSet(self):
-        '''
-        Returns a set with the elements in this stream.
-
-        :return: the set of elements in this stream
-        '''
-        return set(self.iterable)
-
-    def toNumberStream(self):
-        from .numbers import NumberStream
-        return NumberStream(self)
-
-    def toBooleanStream(self):
-        from .booleans import BooleanStream
-        return BooleanStream(self)
-
-    def __iter__(self):
-        '''
-        Returns an iterator over the elements in this stream.
-
-        :return: the iterator over the elements in this stream
-        '''
-        return iter(self.iterable)
-
-    def __eq__(self, value):
-        '''
-        Check if this stream is equal to the specified stream
-
-        :return: True if the streams match, False otherwise
-        '''
-        return self.toSet() == value.toSet()
-
- -
- -
-
-
- #   - - - class - Stream: -
- -
- View Source -
class Stream():
-
-    """
-    A sequence of elements supporting sequential operations.
-
-    The following example illustrates an aggregate operation using
-    Stream:
-
-        result = Stream(elements)
-                        .filter(lambda w: w.getColor() == RED)
-                        .map(lambda w: w.getWeight())
-                        .sum()
-
-    A stream pipeline, like the elements example above, can be viewed as a query on the stream source.
-
-    A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may raise Exception if it detects that the stream is being reused.
-    """
-
-    """
-    Static Methods
-    """
-
-    @staticmethod
-    def empty():
-        '''
-        Returns an empty sequential Stream.
-
-        :return: an empty sequential stream
-        '''
-        return Stream([])
-
-    @staticmethod
-    def of(elem):
-        '''
-        Returns a sequential Stream containing a single element.
-
-        :param T elem: the single element
-        :return: a singleton sequential stream
-        '''
-        return Stream(iter([elem]))
-
-    @staticmethod
-    def of(*elements):
-        '''
-        Returns a sequential ordered stream whose elements are the specified values.
-
-        :param *T elements: the elements of the new stream
-        :return: the new stream
-        '''
-        return Stream(iter(list(elements)))
-
-    @staticmethod
-    def ofNullable(elem):
-        '''
-        Returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.
-
-        :param T elem: the single element
-        :return: a stream with a single element if the specified element is non-null, otherwise an empty stream
-        '''
-        return Stream.of(elem) if elem is not None else Stream.empty()
-
-    @staticmethod
-    def iterate(seed, operator):
-        '''
-        Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.
-
-        :param T seed: the initial element
-        :param UnaryOperator operator: a function to be applied to the previous element to produce a new element
-        :return: a new sequential Stream
-        '''
-        return Stream(IteratorUtils.iterate(seed, operator))
-
-    @staticmethod
-    def generate(supplier):
-        '''
-        Returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc.
-
-        :param Supplier supplier: the Supplier of generated elements
-        :return: a new infinite sequential unordered Stream
-        '''
-        return Stream(IteratorUtils.generate(supplier))
-
-    @staticmethod
-    def concat(*streams):
-        '''
-        Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream and so on.
-
-        :param *Stream streams: the streams to concat
-        :return: the concatenation of the input streams
-        '''
-        return Stream(IteratorUtils.concat(*streams))
-
-    @staticmethod
-    def constant(element):
-        '''
-        Return an infinite sequential stream where each element is the passed element
-
-        :param T elem: the element
-        :return: the new stream made of @element
-        '''
-        return Stream.generate(lambda: element)
-
-    """
-    Normal Methods
-    """
-
-    def __init__(self, iterable):
-        self.iterable = iterable
-
-    def filter(self, predicate):
-        '''
-        Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
-
-        :param function predicate: predicate to apply to each element to determine if it should be included
-        :return: self
-        '''
-        self.iterable = IteratorUtils.filter(self.iterable, predicate)
-        return self
-
-    def map(self, mapper):
-        '''
-        Returns a stream consisting of the results of applying the given function to the elements of this stream.
-
-        :param function mapper: function to apply to each element
-        :return: self
-        '''
-        self.iterable = IteratorUtils.map(self.iterable, mapper)
-        return self
-
-    def flatMap(self, flatMapper):
-        '''
-        Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)
-
-        :param function flatMapper: function to apply to each element which produces a stream of new values
-        :return: self
-        '''
-        self.iterable = IteratorUtils.flatMap(self.iterable, flatMapper)
-        return self
-
-    def distinct(self):
-        '''
-        Returns a stream consisting of the distinct elements of this stream.
-
-        :return: self
-        '''
-        self.iterable = IteratorUtils.distinct(self.iterable)
-        return self
-
-    def limit(self, count):
-        '''
-        Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
-
-        :param int count:  the number of elements the stream should be limited to
-        :return: self
-        '''
-        self.iterable = IteratorUtils.limit(self.iterable, count)
-        return self
-
-    def skip(self, count):
-        '''
-        Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.
-
-        :param int count:  the number of leading elements to skip
-        :return: self
-        '''
-        self.iterable = IteratorUtils.skip(self.iterable, count)
-        return self
-
-    def takeWhile(self, predicate):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.
-
-        :param Predicate predicate:  predicate to apply to elements to determine the longest prefix of elements.
-        :return: self
-        '''
-        self.iterable = IteratorUtils.takeWhile(self.iterable, predicate)
-        return self
-
-    def dropWhile(self, predicate):
-        '''
-        Returns a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.
-
-        :param Predicate predicate:  predicate to apply to elements to determine the longest prefix of elements.
-        :return: self
-        '''
-        self.iterable = IteratorUtils.dropWhile(self.iterable, predicate)
-        return self
-
-    """
-    From here this method mustn't be called on infinite stream
-    """
-
-    def sorted(self, comparator=None):
-        '''
-        Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.
-
-        :param Comparator comparator: Comparator to be used to compare stream elements - if null default comparator is used
-        :return: self
-        '''
-        self.iterable = iter(sorted(
-            self.iterable, key=cmp_to_key(comparator))) if comparator is not None else iter(sorted(
-                self.iterable))
-        return self
-
-    def peek(self, consumer):
-        '''
-        Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
-
-        :param Consumer consumer: action to perform on the elements as they are consumed from the stream
-        :return: self
-        '''
-        self.iterable = IteratorUtils.peek(self.iterable, consumer)
-        return self
-
-    def forEach(self, function):
-        '''
-        Performs an action for each element of this stream.
-
-        :param Function function: action to perform on the elements
-        :return: None
-        '''
-        for elem in self.iterable:
-            function(elem)
-
-    def anyMatch(self, predicate):
-        '''
-        Returns whether any elements of this stream match the provided predicate.
-
-        :param Predicate predicate: predicate to apply to elements of this stream
-        :return: True if any elements of the stream match the provided predicate, otherwise False
-        '''
-        return any([predicate(elem) for elem in self.iterable])
-
-    def allMatch(self, predicate):
-        '''
-        Returns whether all elements of this stream match the provided predicate.
-
-        :param Predicate predicate: predicate to apply to elements of this stream
-        :return: True if either all elements of the stream match the provided predicate or the stream is empty, otherwise False
-        '''
-        return all([predicate(elem) for elem in self.iterable])
-
-    def noneMatch(self, predicate):
-        '''
-        Returns whether no elements of this stream match the provided predicate.
-
-        :param Predicate predicate: predicate to apply to elements of this stream
-        :return: True if either no elements of the stream match the provided predicate or the stream is empty, otherwise False
-        '''
-        return not self.anyMatch(predicate)
-
-    def findFirst(self):
-        '''
-        Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned.
-
-        :return: an Optional describing the first element of this stream, or an empty Optional if the stream is empty
-        '''
-        for elem in self.iterable:
-            return Optional.of(elem)
-        return Optional.ofNullable(None)
-
-    def findAny(self):
-        '''
-        Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.
-
-        :return: an Optional describing some element of this stream, or an empty Optional if the stream is empty
-        '''
-        return self.findFirst()
-
-    def reduce(self, accumulator, identity=None):
-        '''
-        Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.
-
-        :param T identity: the identity value for the accumulating function - if not specified it will be the first element of the stream
-        :param Accumulator accumulator: function for combining two values
-        :return: the result of reduction
-        '''
-        result = identity
-        for elem in self.iterable:
-            if(result is None):
-                result = elem
-            else:
-                result = accumulator(result, elem)
-        return Optional.ofNullable(result)
-
-    def min(self, comparator=None):
-        '''
-        Returns the minimum element of this stream according to the provided Comparator. This is a special case of a reduction.
-
-        :param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used
-        :return: an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty
-        '''
-        elements = list(self.iterable)
-        if len(elements) == 0:
-            return Optional.empty()
-
-        return Optional.ofNullable(min(elements, key=cmp_to_key(comparator), default=None)) if comparator is not None else Optional.ofNullable(min(elements, default=None))
-
-    def max(self, comparator=None):
-        '''
-        Returns the maximum element of this stream according to the provided Comparator. This is a special case of a reduction.
-
-        :param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used
-        :return: an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty
-        '''
-        elements = list(self.iterable)
-        if len(elements) == 0:
-            return Optional.empty()
-        return Optional.ofNullable(max(elements, key=cmp_to_key(comparator))) if comparator is not None else Optional.ofNullable(max(elements))
-
-    def sum(self):
-        '''
-        Returns the sum of all elements of this stream. This is a special case of a reduction.
-
-        :return: an Optional describing the sum of all the elements of this stream, or an empty Optional if the stream is empty
-        '''
-        return self.reduce(lambda x, y: x + y)
-
-    def count(self):
-        '''
-        Returns the count of elements in this stream. This is a special case of a reduction.
-
-        :return: the count of elements in this stream
-        '''
-        count = 0
-        for elem in self.iterable:
-            count += 1
-
-        return count
-
-    def toList(self):
-        '''
-        Returns a list with the elements in this stream.
-
-        :return: the list of elements in this stream
-        '''
-        return list(self.iterable)
-
-    def toSet(self):
-        '''
-        Returns a set with the elements in this stream.
-
-        :return: the set of elements in this stream
-        '''
-        return set(self.iterable)
-
-    def toNumberStream(self):
-        from .numbers import NumberStream
-        return NumberStream(self)
-
-    def toBooleanStream(self):
-        from .booleans import BooleanStream
-        return BooleanStream(self)
-
-    def __iter__(self):
-        '''
-        Returns an iterator over the elements in this stream.
-
-        :return: the iterator over the elements in this stream
-        '''
-        return iter(self.iterable)
-
-    def __eq__(self, value):
-        '''
-        Check if this stream is equal to the specified stream
-
-        :return: True if the streams match, False otherwise
-        '''
-        return self.toSet() == value.toSet()
-
- -
- -

A sequence of elements supporting sequential operations.

- -

The following example illustrates an aggregate operation using -Stream:

- -
result = Stream(elements)
-                .filter(lambda w: w.getColor() == RED)
-                .map(lambda w: w.getWeight())
-                .sum()
-
- + +
+
+
+

Module stream.stream

+
+
+
+ +Expand source code + +
from functools import cmp_to_key
+import random
+
+from .util.iterators import IteratorUtils
+from .util.optional import Optional
+
+
+class Stream():
+
+    """
+    A sequence of elements supporting sequential operations.
+
+    The following example illustrates an aggregate operation using
+    Stream:
+
+        result = Stream(elements)
+                        .filter(lambda w: w.getColor() == RED)
+                        .map(lambda w: w.getWeight())
+                        .sum()
+
+    A stream pipeline, like the elements example above, can be viewed as a query on the stream source.
+
+    A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may raise Exception if it detects that the stream is being reused.
+    """
+
+    """
+    Static Methods
+    """
+
+    @staticmethod
+    def empty():
+        '''
+        Returns an empty sequential Stream.
+
+        :return: an empty sequential stream
+        '''
+        return Stream([])
+
+    @staticmethod
+    def of(elem):
+        '''
+        Returns a sequential Stream containing a single element.
+
+        :param T elem: the single element
+        :return: a singleton sequential stream
+        '''
+        return Stream(iter([elem]))
+
+    @staticmethod
+    def of(*elements):
+        '''
+        Returns a sequential ordered stream whose elements are the specified values.
+
+        :param *T elements: the elements of the new stream
+        :return: the new stream
+        '''
+        return Stream(iter(list(elements)))
+
+    @staticmethod
+    def ofNullable(elem):
+        '''
+        Returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.
+
+        :param T elem: the single element
+        :return: a stream with a single element if the specified element is non-null, otherwise an empty stream
+        '''
+        return Stream.of(elem) if elem is not None else Stream.empty()
+
+    @staticmethod
+    def iterate(seed, operator):
+        '''
+        Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.
+
+        :param T seed: the initial element
+        :param UnaryOperator operator: a function to be applied to the previous element to produce a new element
+        :return: a new sequential Stream
+        '''
+        return Stream(IteratorUtils.iterate(seed, operator))
+
+    @staticmethod
+    def generate(supplier):
+        '''
+        Returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc.
+
+        :param Supplier supplier: the Supplier of generated elements
+        :return: a new infinite sequential unordered Stream
+        '''
+        return Stream(IteratorUtils.generate(supplier))
+
+    @staticmethod
+    def concat(*streams):
+        '''
+        Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream and so on.
+
+        :param *Stream streams: the streams to concat
+        :return: the concatenation of the input streams
+        '''
+        return Stream(IteratorUtils.concat(*streams))
+
+    @staticmethod
+    def constant(element):
+        '''
+        Return an infinite sequential stream where each element is the passed element
+
+        :param T elem: the element
+        :return: the new stream made of @element
+        '''
+        return Stream.generate(lambda: element)
+
+    """
+    Normal Methods
+    """
+
+    def __init__(self, iterable):
+        self.iterable = iterable
+
+    def filter(self, predicate):
+        '''
+        Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
+
+        :param function predicate: predicate to apply to each element to determine if it should be included
+        :return: self
+        '''
+        self.iterable = IteratorUtils.filter(self.iterable, predicate)
+        return self
+
+    def map(self, mapper):
+        '''
+        Returns a stream consisting of the results of applying the given function to the elements of this stream.
+
+        :param function mapper: function to apply to each element
+        :return: self
+        '''
+        self.iterable = IteratorUtils.map(self.iterable, mapper)
+        return self
+
+    def flatMap(self, flatMapper):
+        '''
+        Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)
+
+        :param function flatMapper: function to apply to each element which produces a stream of new values
+        :return: self
+        '''
+        self.iterable = IteratorUtils.flatMap(self.iterable, flatMapper)
+        return self
+
+    def distinct(self):
+        '''
+        Returns a stream consisting of the distinct elements of this stream.
+
+        :return: self
+        '''
+        self.iterable = IteratorUtils.distinct(self.iterable)
+        return self
+
+    def limit(self, count):
+        '''
+        Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
+
+        :param int count:  the number of elements the stream should be limited to
+        :return: self
+        '''
+        self.iterable = IteratorUtils.limit(self.iterable, count)
+        return self
+
+    def skip(self, count):
+        '''
+        Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.
+
+        :param int count:  the number of leading elements to skip
+        :return: self
+        '''
+        self.iterable = IteratorUtils.skip(self.iterable, count)
+        return self
+
+    def takeWhile(self, predicate):
+        '''
+        Returns a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.
+
+        :param Predicate predicate:  predicate to apply to elements to determine the longest prefix of elements.
+        :return: self
+        '''
+        self.iterable = IteratorUtils.takeWhile(self.iterable, predicate)
+        return self
+
+    def dropWhile(self, predicate):
+        '''
+        Returns a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.
+
+        :param Predicate predicate:  predicate to apply to elements to determine the longest prefix of elements.
+        :return: self
+        '''
+        self.iterable = IteratorUtils.dropWhile(self.iterable, predicate)
+        return self
+
+    """
+    From here this method mustn't be called on infinite stream
+    """
+
+    def sorted(self, comparator=None):
+        '''
+        Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.
+
+        :param Comparator comparator: Comparator to be used to compare stream elements - if null default comparator is used
+        :return: self
+        '''
+        self.iterable = iter(sorted(
+            self.iterable, key=cmp_to_key(comparator))) if comparator is not None else iter(sorted(
+                self.iterable))
+        return self
+
+    def peek(self, consumer):
+        '''
+        Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
+
+        :param Consumer consumer: action to perform on the elements as they are consumed from the stream
+        :return: self
+        '''
+        self.iterable = IteratorUtils.peek(self.iterable, consumer)
+        return self
+
+    def forEach(self, function):
+        '''
+        Performs an action for each element of this stream.
+
+        :param Function function: action to perform on the elements
+        :return: None
+        '''
+        for elem in self.iterable:
+            function(elem)
+
+    def anyMatch(self, predicate):
+        '''
+        Returns whether any elements of this stream match the provided predicate.
+
+        :param Predicate predicate: predicate to apply to elements of this stream
+        :return: True if any elements of the stream match the provided predicate, otherwise False
+        '''
+        return any([predicate(elem) for elem in self.iterable])
+
+    def allMatch(self, predicate):
+        '''
+        Returns whether all elements of this stream match the provided predicate.
+
+        :param Predicate predicate: predicate to apply to elements of this stream
+        :return: True if either all elements of the stream match the provided predicate or the stream is empty, otherwise False
+        '''
+        return all([predicate(elem) for elem in self.iterable])
+
+    def noneMatch(self, predicate):
+        '''
+        Returns whether no elements of this stream match the provided predicate.
+
+        :param Predicate predicate: predicate to apply to elements of this stream
+        :return: True if either no elements of the stream match the provided predicate or the stream is empty, otherwise False
+        '''
+        return not self.anyMatch(predicate)
+
+    def findFirst(self, predicate=None):
+        '''
+        Returns an Optional describing the first element (with optional filtering by a given predicate) of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned.
+
+        :param Predicate predicate: optional predicate to apply to elements of this stream
+        :return: an Optional describing the first element of this stream, or an empty Optional if the stream is empty
+        '''
+        if predicate:
+            self.filter(predicate)
+
+        for elem in self.iterable:
+            return Optional.of(elem)
+        return Optional.ofNullable(None)
+
+    def findAny(self):
+        '''
+        Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.
+
+        :return: an Optional describing some element of this stream, or an empty Optional if the stream is empty
+        '''
+        return self.findFirst()
+
+    def reduce(self, accumulator, identity=None):
+        '''
+        Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.
+
+        :param T identity: the identity value for the accumulating function - if not specified it will be the first element of the stream
+        :param Accumulator accumulator: function for combining two values
+        :return: the result of reduction
+        '''
+        result = identity
+        for elem in self.iterable:
+            if(result is None):
+                result = elem
+            else:
+                result = accumulator(result, elem)
+        return Optional.ofNullable(result)
+
+    def min(self, comparator=None):
+        '''
+        Returns the minimum element of this stream according to the provided Comparator. This is a special case of a reduction.
+
+        :param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used
+        :return: an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty
+        '''
+        elements = list(self.iterable)
+        if len(elements) == 0:
+            return Optional.empty()
+
+        return Optional.ofNullable(min(elements, key=cmp_to_key(comparator), default=None)) if comparator is not None else Optional.ofNullable(min(elements, default=None))
+
+    def max(self, comparator=None):
+        '''
+        Returns the maximum element of this stream according to the provided Comparator. This is a special case of a reduction.
+
+        :param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used
+        :return: an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty
+        '''
+        elements = list(self.iterable)
+        if len(elements) == 0:
+            return Optional.empty()
+        return Optional.ofNullable(max(elements, key=cmp_to_key(comparator))) if comparator is not None else Optional.ofNullable(max(elements))
+
+    def sum(self):
+        '''
+        Returns the sum of all elements of this stream. This is a special case of a reduction.
+
+        :return: an Optional describing the sum of all the elements of this stream, or an empty Optional if the stream is empty
+        '''
+        return self.reduce(lambda x, y: x + y)
+
+    def count(self):
+        '''
+        Returns the count of elements in this stream. This is a special case of a reduction.
+
+        :return: the count of elements in this stream
+        '''
+        count = 0
+        for elem in self.iterable:
+            count += 1
+
+        return count
+
+    def toList(self):
+        '''
+        Returns a list with the elements in this stream.
+
+        :return: the list of elements in this stream
+        '''
+        return list(self.iterable)
+
+    def toSet(self):
+        '''
+        Returns a set with the elements in this stream.
+
+        :return: the set of elements in this stream
+        '''
+        return set(self.iterable)
+
+    def toNumberStream(self):
+        from .numbers import NumberStream
+        return NumberStream(self)
+
+    def toBooleanStream(self):
+        from .booleans import BooleanStream
+        return BooleanStream(self)
+
+    def __iter__(self):
+        '''
+        Returns an iterator over the elements in this stream.
+
+        :return: the iterator over the elements in this stream
+        '''
+        return iter(self.iterable)
+
+    def __eq__(self, value):
+        '''
+        Check if this stream is equal to the specified stream
+
+        :return: True if the streams match, False otherwise
+        '''
+        return self.toSet() == value.toSet()
+
+
+
+
+
+
+
+
+
+

Classes

+
+
+class Stream +(iterable) +
+
+

A sequence of elements supporting sequential operations.

+

The following example illustrates an aggregate operation using

+

Stream

+

result = Stream(elements) +.filter(lambda w: w.getColor() == RED) +.map(lambda w: w.getWeight()) +.sum()

A stream pipeline, like the elements example above, can be viewed as a query on the stream source.

- -

A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may raise Exception if it detects that the stream is being reused.

-
- - -
-
#   - - - Stream(iterable)
- -
- View Source -
    def __init__(self, iterable):
-        self.iterable = iterable
-
- -
- - - -
-
-
#   - -
@staticmethod
- - def - empty(): -
- -
- View Source -
    @staticmethod
-    def empty():
-        '''
-        Returns an empty sequential Stream.
-
-        :return: an empty sequential stream
-        '''
-        return Stream([])
-
- -
- -

Returns an empty sequential Stream.

- -

:return: an empty sequential stream

-
- - -
-
-
#   - -
@staticmethod
- - def - of(*elements): -
- -
- View Source -
    @staticmethod
-    def of(*elements):
-        '''
-        Returns a sequential ordered stream whose elements are the specified values.
-
-        :param *T elements: the elements of the new stream
-        :return: the new stream
-        '''
-        return Stream(iter(list(elements)))
-
- -
- -

Returns a sequential ordered stream whose elements are the specified values.

- -

:param *T elements: the elements of the new stream -:return: the new stream

-
- - -
-
-
#   - -
@staticmethod
- - def - ofNullable(elem): -
- -
- View Source -
    @staticmethod
-    def ofNullable(elem):
-        '''
-        Returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.
-
-        :param T elem: the single element
-        :return: a stream with a single element if the specified element is non-null, otherwise an empty stream
-        '''
-        return Stream.of(elem) if elem is not None else Stream.empty()
-
- -
- -

Returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.

- -

:param T elem: the single element -:return: a stream with a single element if the specified element is non-null, otherwise an empty stream

-
- - -
-
-
#   - -
@staticmethod
- - def - iterate(seed, operator): -
- -
- View Source -
    @staticmethod
-    def iterate(seed, operator):
-        '''
-        Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.
-
-        :param T seed: the initial element
-        :param UnaryOperator operator: a function to be applied to the previous element to produce a new element
-        :return: a new sequential Stream
-        '''
-        return Stream(IteratorUtils.iterate(seed, operator))
-
- -
- -

Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.

- -

:param T seed: the initial element -:param UnaryOperator operator: a function to be applied to the previous element to produce a new element -:return: a new sequential Stream

-
- - -
-
-
#   - -
@staticmethod
- - def - generate(supplier): -
- -
- View Source -
    @staticmethod
-    def generate(supplier):
-        '''
-        Returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc.
-
-        :param Supplier supplier: the Supplier of generated elements
-        :return: a new infinite sequential unordered Stream
-        '''
-        return Stream(IteratorUtils.generate(supplier))
-
- -
- -

Returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc.

- -

:param Supplier supplier: the Supplier of generated elements -:return: a new infinite sequential unordered Stream

-
- - -
-
-
#   - -
@staticmethod
- - def - concat(*streams): -
- -
- View Source -
    @staticmethod
-    def concat(*streams):
-        '''
-        Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream and so on.
-
-        :param *Stream streams: the streams to concat
-        :return: the concatenation of the input streams
-        '''
-        return Stream(IteratorUtils.concat(*streams))
-
- -
- -

Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream and so on.

- +

A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may raise Exception if it detects that the stream is being reused.

+
+ +Expand source code + +
class Stream():
+
+    """
+    A sequence of elements supporting sequential operations.
+
+    The following example illustrates an aggregate operation using
+    Stream:
+
+        result = Stream(elements)
+                        .filter(lambda w: w.getColor() == RED)
+                        .map(lambda w: w.getWeight())
+                        .sum()
+
+    A stream pipeline, like the elements example above, can be viewed as a query on the stream source.
+
+    A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for example, "forked" streams, where the same source feeds two or more pipelines, or multiple traversals of the same stream. A stream implementation may raise Exception if it detects that the stream is being reused.
+    """
+
+    """
+    Static Methods
+    """
+
+    @staticmethod
+    def empty():
+        '''
+        Returns an empty sequential Stream.
+
+        :return: an empty sequential stream
+        '''
+        return Stream([])
+
+    @staticmethod
+    def of(elem):
+        '''
+        Returns a sequential Stream containing a single element.
+
+        :param T elem: the single element
+        :return: a singleton sequential stream
+        '''
+        return Stream(iter([elem]))
+
+    @staticmethod
+    def of(*elements):
+        '''
+        Returns a sequential ordered stream whose elements are the specified values.
+
+        :param *T elements: the elements of the new stream
+        :return: the new stream
+        '''
+        return Stream(iter(list(elements)))
+
+    @staticmethod
+    def ofNullable(elem):
+        '''
+        Returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.
+
+        :param T elem: the single element
+        :return: a stream with a single element if the specified element is non-null, otherwise an empty stream
+        '''
+        return Stream.of(elem) if elem is not None else Stream.empty()
+
+    @staticmethod
+    def iterate(seed, operator):
+        '''
+        Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.
+
+        :param T seed: the initial element
+        :param UnaryOperator operator: a function to be applied to the previous element to produce a new element
+        :return: a new sequential Stream
+        '''
+        return Stream(IteratorUtils.iterate(seed, operator))
+
+    @staticmethod
+    def generate(supplier):
+        '''
+        Returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc.
+
+        :param Supplier supplier: the Supplier of generated elements
+        :return: a new infinite sequential unordered Stream
+        '''
+        return Stream(IteratorUtils.generate(supplier))
+
+    @staticmethod
+    def concat(*streams):
+        '''
+        Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream and so on.
+
+        :param *Stream streams: the streams to concat
+        :return: the concatenation of the input streams
+        '''
+        return Stream(IteratorUtils.concat(*streams))
+
+    @staticmethod
+    def constant(element):
+        '''
+        Return an infinite sequential stream where each element is the passed element
+
+        :param T elem: the element
+        :return: the new stream made of @element
+        '''
+        return Stream.generate(lambda: element)
+
+    """
+    Normal Methods
+    """
+
+    def __init__(self, iterable):
+        self.iterable = iterable
+
+    def filter(self, predicate):
+        '''
+        Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
+
+        :param function predicate: predicate to apply to each element to determine if it should be included
+        :return: self
+        '''
+        self.iterable = IteratorUtils.filter(self.iterable, predicate)
+        return self
+
+    def map(self, mapper):
+        '''
+        Returns a stream consisting of the results of applying the given function to the elements of this stream.
+
+        :param function mapper: function to apply to each element
+        :return: self
+        '''
+        self.iterable = IteratorUtils.map(self.iterable, mapper)
+        return self
+
+    def flatMap(self, flatMapper):
+        '''
+        Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)
+
+        :param function flatMapper: function to apply to each element which produces a stream of new values
+        :return: self
+        '''
+        self.iterable = IteratorUtils.flatMap(self.iterable, flatMapper)
+        return self
+
+    def distinct(self):
+        '''
+        Returns a stream consisting of the distinct elements of this stream.
+
+        :return: self
+        '''
+        self.iterable = IteratorUtils.distinct(self.iterable)
+        return self
+
+    def limit(self, count):
+        '''
+        Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
+
+        :param int count:  the number of elements the stream should be limited to
+        :return: self
+        '''
+        self.iterable = IteratorUtils.limit(self.iterable, count)
+        return self
+
+    def skip(self, count):
+        '''
+        Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.
+
+        :param int count:  the number of leading elements to skip
+        :return: self
+        '''
+        self.iterable = IteratorUtils.skip(self.iterable, count)
+        return self
+
+    def takeWhile(self, predicate):
+        '''
+        Returns a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.
+
+        :param Predicate predicate:  predicate to apply to elements to determine the longest prefix of elements.
+        :return: self
+        '''
+        self.iterable = IteratorUtils.takeWhile(self.iterable, predicate)
+        return self
+
+    def dropWhile(self, predicate):
+        '''
+        Returns a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.
+
+        :param Predicate predicate:  predicate to apply to elements to determine the longest prefix of elements.
+        :return: self
+        '''
+        self.iterable = IteratorUtils.dropWhile(self.iterable, predicate)
+        return self
+
+    """
+    From here this method mustn't be called on infinite stream
+    """
+
+    def sorted(self, comparator=None):
+        '''
+        Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.
+
+        :param Comparator comparator: Comparator to be used to compare stream elements - if null default comparator is used
+        :return: self
+        '''
+        self.iterable = iter(sorted(
+            self.iterable, key=cmp_to_key(comparator))) if comparator is not None else iter(sorted(
+                self.iterable))
+        return self
+
+    def peek(self, consumer):
+        '''
+        Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
+
+        :param Consumer consumer: action to perform on the elements as they are consumed from the stream
+        :return: self
+        '''
+        self.iterable = IteratorUtils.peek(self.iterable, consumer)
+        return self
+
+    def forEach(self, function):
+        '''
+        Performs an action for each element of this stream.
+
+        :param Function function: action to perform on the elements
+        :return: None
+        '''
+        for elem in self.iterable:
+            function(elem)
+
+    def anyMatch(self, predicate):
+        '''
+        Returns whether any elements of this stream match the provided predicate.
+
+        :param Predicate predicate: predicate to apply to elements of this stream
+        :return: True if any elements of the stream match the provided predicate, otherwise False
+        '''
+        return any([predicate(elem) for elem in self.iterable])
+
+    def allMatch(self, predicate):
+        '''
+        Returns whether all elements of this stream match the provided predicate.
+
+        :param Predicate predicate: predicate to apply to elements of this stream
+        :return: True if either all elements of the stream match the provided predicate or the stream is empty, otherwise False
+        '''
+        return all([predicate(elem) for elem in self.iterable])
+
+    def noneMatch(self, predicate):
+        '''
+        Returns whether no elements of this stream match the provided predicate.
+
+        :param Predicate predicate: predicate to apply to elements of this stream
+        :return: True if either no elements of the stream match the provided predicate or the stream is empty, otherwise False
+        '''
+        return not self.anyMatch(predicate)
+
+    def findFirst(self, predicate=None):
+        '''
+        Returns an Optional describing the first element (with optional filtering by a given predicate) of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned.
+
+        :param Predicate predicate: optional predicate to apply to elements of this stream
+        :return: an Optional describing the first element of this stream, or an empty Optional if the stream is empty
+        '''
+        if predicate:
+            self.filter(predicate)
+
+        for elem in self.iterable:
+            return Optional.of(elem)
+        return Optional.ofNullable(None)
+
+    def findAny(self):
+        '''
+        Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.
+
+        :return: an Optional describing some element of this stream, or an empty Optional if the stream is empty
+        '''
+        return self.findFirst()
+
+    def reduce(self, accumulator, identity=None):
+        '''
+        Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.
+
+        :param T identity: the identity value for the accumulating function - if not specified it will be the first element of the stream
+        :param Accumulator accumulator: function for combining two values
+        :return: the result of reduction
+        '''
+        result = identity
+        for elem in self.iterable:
+            if(result is None):
+                result = elem
+            else:
+                result = accumulator(result, elem)
+        return Optional.ofNullable(result)
+
+    def min(self, comparator=None):
+        '''
+        Returns the minimum element of this stream according to the provided Comparator. This is a special case of a reduction.
+
+        :param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used
+        :return: an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty
+        '''
+        elements = list(self.iterable)
+        if len(elements) == 0:
+            return Optional.empty()
+
+        return Optional.ofNullable(min(elements, key=cmp_to_key(comparator), default=None)) if comparator is not None else Optional.ofNullable(min(elements, default=None))
+
+    def max(self, comparator=None):
+        '''
+        Returns the maximum element of this stream according to the provided Comparator. This is a special case of a reduction.
+
+        :param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used
+        :return: an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty
+        '''
+        elements = list(self.iterable)
+        if len(elements) == 0:
+            return Optional.empty()
+        return Optional.ofNullable(max(elements, key=cmp_to_key(comparator))) if comparator is not None else Optional.ofNullable(max(elements))
+
+    def sum(self):
+        '''
+        Returns the sum of all elements of this stream. This is a special case of a reduction.
+
+        :return: an Optional describing the sum of all the elements of this stream, or an empty Optional if the stream is empty
+        '''
+        return self.reduce(lambda x, y: x + y)
+
+    def count(self):
+        '''
+        Returns the count of elements in this stream. This is a special case of a reduction.
+
+        :return: the count of elements in this stream
+        '''
+        count = 0
+        for elem in self.iterable:
+            count += 1
+
+        return count
+
+    def toList(self):
+        '''
+        Returns a list with the elements in this stream.
+
+        :return: the list of elements in this stream
+        '''
+        return list(self.iterable)
+
+    def toSet(self):
+        '''
+        Returns a set with the elements in this stream.
+
+        :return: the set of elements in this stream
+        '''
+        return set(self.iterable)
+
+    def toNumberStream(self):
+        from .numbers import NumberStream
+        return NumberStream(self)
+
+    def toBooleanStream(self):
+        from .booleans import BooleanStream
+        return BooleanStream(self)
+
+    def __iter__(self):
+        '''
+        Returns an iterator over the elements in this stream.
+
+        :return: the iterator over the elements in this stream
+        '''
+        return iter(self.iterable)
+
+    def __eq__(self, value):
+        '''
+        Check if this stream is equal to the specified stream
+
+        :return: True if the streams match, False otherwise
+        '''
+        return self.toSet() == value.toSet()
+
+

Subclasses

+ +

Static methods

+
+
+def concat(*streams) +
+
+

Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream and so on.

:param *Stream streams: the streams to concat -:return: the concatenation of the input streams

-
- - -
-
-
#   - -
@staticmethod
- - def - constant(element): -
- -
- View Source -
    @staticmethod
-    def constant(element):
-        '''
-        Return an infinite sequential stream where each element is the passed element
-
-        :param T elem: the element
-        :return: the new stream made of @element
-        '''
-        return Stream.generate(lambda: element)
-
- -
- -

Return an infinite sequential stream where each element is the passed element

- +:return: the concatenation of the input streams

+
+ +Expand source code + +
@staticmethod
+def concat(*streams):
+    '''
+    Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream and so on.
+
+    :param *Stream streams: the streams to concat
+    :return: the concatenation of the input streams
+    '''
+    return Stream(IteratorUtils.concat(*streams))
+
+
+
+def constant(element) +
+
+

Return an infinite sequential stream where each element is the passed element

:param T elem: the element -:return: the new stream made of @element

-
- - -
-
-
#   - - - def - filter(self, predicate): -
- -
- View Source -
    def filter(self, predicate):
-        '''
-        Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
-
-        :param function predicate: predicate to apply to each element to determine if it should be included
-        :return: self
-        '''
-        self.iterable = IteratorUtils.filter(self.iterable, predicate)
-        return self
-
- -
- -

Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.

- +:return: the new stream made of @element

+
+ +Expand source code + +
@staticmethod
+def constant(element):
+    '''
+    Return an infinite sequential stream where each element is the passed element
+
+    :param T elem: the element
+    :return: the new stream made of @element
+    '''
+    return Stream.generate(lambda: element)
+
+ +
+def empty() +
+
+

Returns an empty sequential Stream.

+

:return: an empty sequential stream

+
+ +Expand source code + +
@staticmethod
+def empty():
+    '''
+    Returns an empty sequential Stream.
+
+    :return: an empty sequential stream
+    '''
+    return Stream([])
+
+
+
+def generate(supplier) +
+
+

Returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc.

+

:param Supplier supplier: the Supplier of generated elements +:return: a new infinite sequential unordered Stream

+
+ +Expand source code + +
@staticmethod
+def generate(supplier):
+    '''
+    Returns an infinite sequential unordered stream where each element is generated by the provided Supplier. This is suitable for generating constant streams, streams of random elements, etc.
+
+    :param Supplier supplier: the Supplier of generated elements
+    :return: a new infinite sequential unordered Stream
+    '''
+    return Stream(IteratorUtils.generate(supplier))
+
+
+
+def iterate(seed, operator) +
+
+

Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.

+

:param T seed: the initial element +:param UnaryOperator operator: a function to be applied to the previous element to produce a new element +:return: a new sequential Stream

+
+ +Expand source code + +
@staticmethod
+def iterate(seed, operator):
+    '''
+    Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.
+
+    :param T seed: the initial element
+    :param UnaryOperator operator: a function to be applied to the previous element to produce a new element
+    :return: a new sequential Stream
+    '''
+    return Stream(IteratorUtils.iterate(seed, operator))
+
+
+
+def of(*elements) +
+
+

Returns a sequential ordered stream whose elements are the specified values.

+

:param *T elements: the elements of the new stream +:return: the new stream

+
+ +Expand source code + +
@staticmethod
+def of(*elements):
+    '''
+    Returns a sequential ordered stream whose elements are the specified values.
+
+    :param *T elements: the elements of the new stream
+    :return: the new stream
+    '''
+    return Stream(iter(list(elements)))
+
+
+
+def ofNullable(elem) +
+
+

Returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.

+

:param T elem: the single element +:return: a stream with a single element if the specified element is non-null, otherwise an empty stream

+
+ +Expand source code + +
@staticmethod
+def ofNullable(elem):
+    '''
+    Returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.
+
+    :param T elem: the single element
+    :return: a stream with a single element if the specified element is non-null, otherwise an empty stream
+    '''
+    return Stream.of(elem) if elem is not None else Stream.empty()
+
+
+ +

Methods

+
+
+def allMatch(self, predicate) +
+
+

Returns whether all elements of this stream match the provided predicate.

+

:param Predicate predicate: predicate to apply to elements of this stream +:return: True if either all elements of the stream match the provided predicate or the stream is empty, otherwise False

+
+ +Expand source code + +
def allMatch(self, predicate):
+    '''
+    Returns whether all elements of this stream match the provided predicate.
+
+    :param Predicate predicate: predicate to apply to elements of this stream
+    :return: True if either all elements of the stream match the provided predicate or the stream is empty, otherwise False
+    '''
+    return all([predicate(elem) for elem in self.iterable])
+
+
+
+def anyMatch(self, predicate) +
+
+

Returns whether any elements of this stream match the provided predicate.

+

:param Predicate predicate: predicate to apply to elements of this stream +:return: True if any elements of the stream match the provided predicate, otherwise False

+
+ +Expand source code + +
def anyMatch(self, predicate):
+    '''
+    Returns whether any elements of this stream match the provided predicate.
+
+    :param Predicate predicate: predicate to apply to elements of this stream
+    :return: True if any elements of the stream match the provided predicate, otherwise False
+    '''
+    return any([predicate(elem) for elem in self.iterable])
+
+
+
+def count(self) +
+
+

Returns the count of elements in this stream. This is a special case of a reduction.

+

:return: the count of elements in this stream

+
+ +Expand source code + +
def count(self):
+    '''
+    Returns the count of elements in this stream. This is a special case of a reduction.
+
+    :return: the count of elements in this stream
+    '''
+    count = 0
+    for elem in self.iterable:
+        count += 1
+
+    return count
+
+
+
+def distinct(self) +
+
+

Returns a stream consisting of the distinct elements of this stream.

+

:return: self

+
+ +Expand source code + +
def distinct(self):
+    '''
+    Returns a stream consisting of the distinct elements of this stream.
+
+    :return: self
+    '''
+    self.iterable = IteratorUtils.distinct(self.iterable)
+    return self
+
+
+
+def dropWhile(self, predicate) +
+
+

Returns a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.

+

:param Predicate predicate: +predicate to apply to elements to determine the longest prefix of elements. +:return: self

+
+ +Expand source code + +
def dropWhile(self, predicate):
+    '''
+    Returns a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.
+
+    :param Predicate predicate:  predicate to apply to elements to determine the longest prefix of elements.
+    :return: self
+    '''
+    self.iterable = IteratorUtils.dropWhile(self.iterable, predicate)
+    return self
+
+
+
+def filter(self, predicate) +
+
+

Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.

:param function predicate: predicate to apply to each element to determine if it should be included -:return: self

-
- - -
-
-
#   - - - def - map(self, mapper): -
- -
- View Source -
    def map(self, mapper):
-        '''
-        Returns a stream consisting of the results of applying the given function to the elements of this stream.
-
-        :param function mapper: function to apply to each element
-        :return: self
-        '''
-        self.iterable = IteratorUtils.map(self.iterable, mapper)
-        return self
-
- -
- -

Returns a stream consisting of the results of applying the given function to the elements of this stream.

- -

:param function mapper: function to apply to each element -:return: self

-
- - -
-
-
#   - - - def - flatMap(self, flatMapper): -
- -
- View Source -
    def flatMap(self, flatMapper):
-        '''
-        Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)
-
-        :param function flatMapper: function to apply to each element which produces a stream of new values
-        :return: self
-        '''
-        self.iterable = IteratorUtils.flatMap(self.iterable, flatMapper)
-        return self
-
- -
- -

Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)

- +:return: self

+
+ +Expand source code + +
def filter(self, predicate):
+    '''
+    Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
+
+    :param function predicate: predicate to apply to each element to determine if it should be included
+    :return: self
+    '''
+    self.iterable = IteratorUtils.filter(self.iterable, predicate)
+    return self
+
+ +
+def findAny(self) +
+
+

Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.

+

:return: an Optional describing some element of this stream, or an empty Optional if the stream is empty

+
+ +Expand source code + +
def findAny(self):
+    '''
+    Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.
+
+    :return: an Optional describing some element of this stream, or an empty Optional if the stream is empty
+    '''
+    return self.findFirst()
+
+
+
+def findFirst(self, predicate=None) +
+
+

Returns an Optional describing the first element (with optional filtering by a given predicate) of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned.

+

:param Predicate predicate: optional predicate to apply to elements of this stream +:return: an Optional describing the first element of this stream, or an empty Optional if the stream is empty

+
+ +Expand source code + +
def findFirst(self, predicate=None):
+    '''
+    Returns an Optional describing the first element (with optional filtering by a given predicate) of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned.
+
+    :param Predicate predicate: optional predicate to apply to elements of this stream
+    :return: an Optional describing the first element of this stream, or an empty Optional if the stream is empty
+    '''
+    if predicate:
+        self.filter(predicate)
+
+    for elem in self.iterable:
+        return Optional.of(elem)
+    return Optional.ofNullable(None)
+
+
+
+def flatMap(self, flatMapper) +
+
+

Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)

:param function flatMapper: function to apply to each element which produces a stream of new values -:return: self

-
- - -
-
-
#   - - - def - distinct(self): -
- -
- View Source -
    def distinct(self):
-        '''
-        Returns a stream consisting of the distinct elements of this stream.
-
-        :return: self
-        '''
-        self.iterable = IteratorUtils.distinct(self.iterable)
-        return self
-
- -
- -

Returns a stream consisting of the distinct elements of this stream.

- -

:return: self

-
- - -
-
-
#   - - - def - limit(self, count): -
- -
- View Source -
    def limit(self, count):
-        '''
-        Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
-
-        :param int count:  the number of elements the stream should be limited to
-        :return: self
-        '''
-        self.iterable = IteratorUtils.limit(self.iterable, count)
-        return self
-
- -
- -

Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.

- -

:param int count: the number of elements the stream should be limited to -:return: self

-
- - -
-
-
#   - - - def - skip(self, count): -
- -
- View Source -
    def skip(self, count):
-        '''
-        Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.
-
-        :param int count:  the number of leading elements to skip
-        :return: self
-        '''
-        self.iterable = IteratorUtils.skip(self.iterable, count)
-        return self
-
- -
- -

Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.

- -

:param int count: the number of leading elements to skip -:return: self

-
- - -
-
-
#   - - - def - takeWhile(self, predicate): -
- -
- View Source -
    def takeWhile(self, predicate):
-        '''
-        Returns a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.
-
-        :param Predicate predicate:  predicate to apply to elements to determine the longest prefix of elements.
-        :return: self
-        '''
-        self.iterable = IteratorUtils.takeWhile(self.iterable, predicate)
-        return self
-
- -
- -

Returns a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.

- -

:param Predicate predicate: predicate to apply to elements to determine the longest prefix of elements. -:return: self

-
- - -
-
-
#   - - - def - dropWhile(self, predicate): -
- -
- View Source -
    def dropWhile(self, predicate):
-        '''
-        Returns a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.
-
-        :param Predicate predicate:  predicate to apply to elements to determine the longest prefix of elements.
-        :return: self
-        '''
-        self.iterable = IteratorUtils.dropWhile(self.iterable, predicate)
-        return self
-
- -
- -

Returns a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.

- -

:param Predicate predicate: predicate to apply to elements to determine the longest prefix of elements. -:return: self

-
- - -
-
-
#   - - - def - sorted(self, comparator=None): -
- -
- View Source -
    def sorted(self, comparator=None):
-        '''
-        Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.
-
-        :param Comparator comparator: Comparator to be used to compare stream elements - if null default comparator is used
-        :return: self
-        '''
-        self.iterable = iter(sorted(
-            self.iterable, key=cmp_to_key(comparator))) if comparator is not None else iter(sorted(
-                self.iterable))
-        return self
-
- -
- -

Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.

- -

:param Comparator comparator: Comparator to be used to compare stream elements - if null default comparator is used -:return: self

-
- - -
-
-
#   - - - def - peek(self, consumer): -
- -
- View Source -
    def peek(self, consumer):
-        '''
-        Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
-
-        :param Consumer consumer: action to perform on the elements as they are consumed from the stream
-        :return: self
-        '''
-        self.iterable = IteratorUtils.peek(self.iterable, consumer)
-        return self
-
- -
- -

Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.

- -

:param Consumer consumer: action to perform on the elements as they are consumed from the stream -:return: self

-
- - -
-
-
#   - - - def - forEach(self, function): -
- -
- View Source -
    def forEach(self, function):
-        '''
-        Performs an action for each element of this stream.
-
-        :param Function function: action to perform on the elements
-        :return: None
-        '''
-        for elem in self.iterable:
-            function(elem)
-
- -
- -

Performs an action for each element of this stream.

- +:return: self

+
+ +Expand source code + +
def flatMap(self, flatMapper):
+    '''
+    Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)
+
+    :param function flatMapper: function to apply to each element which produces a stream of new values
+    :return: self
+    '''
+    self.iterable = IteratorUtils.flatMap(self.iterable, flatMapper)
+    return self
+
+ +
+def forEach(self, function) +
+
+

Performs an action for each element of this stream.

:param Function function: action to perform on the elements -:return: None

-
- - -
-
-
#   - - - def - anyMatch(self, predicate): -
- -
- View Source -
    def anyMatch(self, predicate):
-        '''
-        Returns whether any elements of this stream match the provided predicate.
-
-        :param Predicate predicate: predicate to apply to elements of this stream
-        :return: True if any elements of the stream match the provided predicate, otherwise False
-        '''
-        return any([predicate(elem) for elem in self.iterable])
-
- -
- -

Returns whether any elements of this stream match the provided predicate.

- -

:param Predicate predicate: predicate to apply to elements of this stream -:return: True if any elements of the stream match the provided predicate, otherwise False

-
- - -
-
-
#   - - - def - allMatch(self, predicate): -
- -
- View Source -
    def allMatch(self, predicate):
-        '''
-        Returns whether all elements of this stream match the provided predicate.
-
-        :param Predicate predicate: predicate to apply to elements of this stream
-        :return: True if either all elements of the stream match the provided predicate or the stream is empty, otherwise False
-        '''
-        return all([predicate(elem) for elem in self.iterable])
-
- -
- -

Returns whether all elements of this stream match the provided predicate.

- -

:param Predicate predicate: predicate to apply to elements of this stream -:return: True if either all elements of the stream match the provided predicate or the stream is empty, otherwise False

-
- - -
-
-
#   - - - def - noneMatch(self, predicate): -
- -
- View Source -
    def noneMatch(self, predicate):
-        '''
-        Returns whether no elements of this stream match the provided predicate.
-
-        :param Predicate predicate: predicate to apply to elements of this stream
-        :return: True if either no elements of the stream match the provided predicate or the stream is empty, otherwise False
-        '''
-        return not self.anyMatch(predicate)
-
- -
- -

Returns whether no elements of this stream match the provided predicate.

- +:return: None

+
+ +Expand source code + +
def forEach(self, function):
+    '''
+    Performs an action for each element of this stream.
+
+    :param Function function: action to perform on the elements
+    :return: None
+    '''
+    for elem in self.iterable:
+        function(elem)
+
+ +
+def limit(self, count) +
+
+

Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.

+

:param int count: +the number of elements the stream should be limited to +:return: self

+
+ +Expand source code + +
def limit(self, count):
+    '''
+    Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
+
+    :param int count:  the number of elements the stream should be limited to
+    :return: self
+    '''
+    self.iterable = IteratorUtils.limit(self.iterable, count)
+    return self
+
+
+
+def map(self, mapper) +
+
+

Returns a stream consisting of the results of applying the given function to the elements of this stream.

+

:param function mapper: function to apply to each element +:return: self

+
+ +Expand source code + +
def map(self, mapper):
+    '''
+    Returns a stream consisting of the results of applying the given function to the elements of this stream.
+
+    :param function mapper: function to apply to each element
+    :return: self
+    '''
+    self.iterable = IteratorUtils.map(self.iterable, mapper)
+    return self
+
+
+
+def max(self, comparator=None) +
+
+

Returns the maximum element of this stream according to the provided Comparator. This is a special case of a reduction.

+

:param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used +:return: an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty

+
+ +Expand source code + +
def max(self, comparator=None):
+    '''
+    Returns the maximum element of this stream according to the provided Comparator. This is a special case of a reduction.
+
+    :param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used
+    :return: an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty
+    '''
+    elements = list(self.iterable)
+    if len(elements) == 0:
+        return Optional.empty()
+    return Optional.ofNullable(max(elements, key=cmp_to_key(comparator))) if comparator is not None else Optional.ofNullable(max(elements))
+
+
+
+def min(self, comparator=None) +
+
+

Returns the minimum element of this stream according to the provided Comparator. This is a special case of a reduction.

+

:param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used +:return: an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty

+
+ +Expand source code + +
def min(self, comparator=None):
+    '''
+    Returns the minimum element of this stream according to the provided Comparator. This is a special case of a reduction.
+
+    :param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used
+    :return: an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty
+    '''
+    elements = list(self.iterable)
+    if len(elements) == 0:
+        return Optional.empty()
+
+    return Optional.ofNullable(min(elements, key=cmp_to_key(comparator), default=None)) if comparator is not None else Optional.ofNullable(min(elements, default=None))
+
+
+
+def noneMatch(self, predicate) +
+
+

Returns whether no elements of this stream match the provided predicate.

:param Predicate predicate: predicate to apply to elements of this stream -:return: True if either no elements of the stream match the provided predicate or the stream is empty, otherwise False

-
- - -
-
-
#   - - - def - findFirst(self): -
- -
- View Source -
    def findFirst(self):
-        '''
-        Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned.
-
-        :return: an Optional describing the first element of this stream, or an empty Optional if the stream is empty
-        '''
-        for elem in self.iterable:
-            return Optional.of(elem)
-        return Optional.ofNullable(None)
-
- -
- -

Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned.

- -

:return: an Optional describing the first element of this stream, or an empty Optional if the stream is empty

-
- - -
-
-
#   - - - def - findAny(self): -
- -
- View Source -
    def findAny(self):
-        '''
-        Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.
-
-        :return: an Optional describing some element of this stream, or an empty Optional if the stream is empty
-        '''
-        return self.findFirst()
-
- -
- -

Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.

- -

:return: an Optional describing some element of this stream, or an empty Optional if the stream is empty

-
- - -
-
-
#   - - - def - reduce(self, accumulator, identity=None): -
- -
- View Source -
    def reduce(self, accumulator, identity=None):
-        '''
-        Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.
-
-        :param T identity: the identity value for the accumulating function - if not specified it will be the first element of the stream
-        :param Accumulator accumulator: function for combining two values
-        :return: the result of reduction
-        '''
-        result = identity
-        for elem in self.iterable:
-            if(result is None):
-                result = elem
-            else:
-                result = accumulator(result, elem)
-        return Optional.ofNullable(result)
-
- -
- -

Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.

- +:return: True if either no elements of the stream match the provided predicate or the stream is empty, otherwise False

+
+ +Expand source code + +
def noneMatch(self, predicate):
+    '''
+    Returns whether no elements of this stream match the provided predicate.
+
+    :param Predicate predicate: predicate to apply to elements of this stream
+    :return: True if either no elements of the stream match the provided predicate or the stream is empty, otherwise False
+    '''
+    return not self.anyMatch(predicate)
+
+ +
+def peek(self, consumer) +
+
+

Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.

+

:param Consumer consumer: action to perform on the elements as they are consumed from the stream +:return: self

+
+ +Expand source code + +
def peek(self, consumer):
+    '''
+    Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
+
+    :param Consumer consumer: action to perform on the elements as they are consumed from the stream
+    :return: self
+    '''
+    self.iterable = IteratorUtils.peek(self.iterable, consumer)
+    return self
+
+
+
+def reduce(self, accumulator, identity=None) +
+
+

Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.

:param T identity: the identity value for the accumulating function - if not specified it will be the first element of the stream :param Accumulator accumulator: function for combining two values -:return: the result of reduction

-
- - -
-
-
#   - - - def - min(self, comparator=None): -
- -
- View Source -
    def min(self, comparator=None):
-        '''
-        Returns the minimum element of this stream according to the provided Comparator. This is a special case of a reduction.
-
-        :param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used
-        :return: an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty
-        '''
-        elements = list(self.iterable)
-        if len(elements) == 0:
-            return Optional.empty()
-
-        return Optional.ofNullable(min(elements, key=cmp_to_key(comparator), default=None)) if comparator is not None else Optional.ofNullable(min(elements, default=None))
-
- -
- -

Returns the minimum element of this stream according to the provided Comparator. This is a special case of a reduction.

- -

:param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used -:return: an Optional describing the minimum element of this stream, or an empty Optional if the stream is empty

-
- - -
-
-
#   - - - def - max(self, comparator=None): -
- -
- View Source -
    def max(self, comparator=None):
-        '''
-        Returns the maximum element of this stream according to the provided Comparator. This is a special case of a reduction.
-
-        :param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used
-        :return: an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty
-        '''
-        elements = list(self.iterable)
-        if len(elements) == 0:
-            return Optional.empty()
-        return Optional.ofNullable(max(elements, key=cmp_to_key(comparator))) if comparator is not None else Optional.ofNullable(max(elements))
-
- -
- -

Returns the maximum element of this stream according to the provided Comparator. This is a special case of a reduction.

- -

:param Comparator comparator: Comparator to compare elements of this stream - if null default comparator is used -:return: an Optional describing the maximum element of this stream, or an empty Optional if the stream is empty

-
- - -
-
-
#   - - - def - sum(self): -
- -
- View Source -
    def sum(self):
-        '''
-        Returns the sum of all elements of this stream. This is a special case of a reduction.
-
-        :return: an Optional describing the sum of all the elements of this stream, or an empty Optional if the stream is empty
-        '''
-        return self.reduce(lambda x, y: x + y)
-
- -
- -

Returns the sum of all elements of this stream. This is a special case of a reduction.

- -

:return: an Optional describing the sum of all the elements of this stream, or an empty Optional if the stream is empty

-
- - -
-
-
#   - - - def - count(self): -
- -
- View Source -
    def count(self):
-        '''
-        Returns the count of elements in this stream. This is a special case of a reduction.
-
-        :return: the count of elements in this stream
-        '''
-        count = 0
-        for elem in self.iterable:
-            count += 1
-
-        return count
-
- -
- -

Returns the count of elements in this stream. This is a special case of a reduction.

- -

:return: the count of elements in this stream

-
- - -
-
-
#   - - - def - toList(self): -
- -
- View Source -
    def toList(self):
-        '''
-        Returns a list with the elements in this stream.
-
-        :return: the list of elements in this stream
-        '''
-        return list(self.iterable)
-
- -
- -

Returns a list with the elements in this stream.

- -

:return: the list of elements in this stream

-
- - -
-
-
#   - - - def - toSet(self): -
- -
- View Source -
    def toSet(self):
-        '''
-        Returns a set with the elements in this stream.
-
-        :return: the set of elements in this stream
-        '''
-        return set(self.iterable)
-
- -
- -

Returns a set with the elements in this stream.

- -

:return: the set of elements in this stream

+:return: the result of reduction

+
+ +Expand source code + +
def reduce(self, accumulator, identity=None):
+    '''
+    Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.
+
+    :param T identity: the identity value for the accumulating function - if not specified it will be the first element of the stream
+    :param Accumulator accumulator: function for combining two values
+    :return: the result of reduction
+    '''
+    result = identity
+    for elem in self.iterable:
+        if(result is None):
+            result = elem
+        else:
+            result = accumulator(result, elem)
+    return Optional.ofNullable(result)
+
+ +
+def skip(self, count) +
+
+

Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.

+

:param int count: +the number of leading elements to skip +:return: self

+
+ +Expand source code + +
def skip(self, count):
+    '''
+    Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.
+
+    :param int count:  the number of leading elements to skip
+    :return: self
+    '''
+    self.iterable = IteratorUtils.skip(self.iterable, count)
+    return self
+
+
+
+def sorted(self, comparator=None) +
+
+

Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.

+

:param Comparator comparator: Comparator to be used to compare stream elements - if null default comparator is used +:return: self

+
+ +Expand source code + +
def sorted(self, comparator=None):
+    '''
+    Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.
+
+    :param Comparator comparator: Comparator to be used to compare stream elements - if null default comparator is used
+    :return: self
+    '''
+    self.iterable = iter(sorted(
+        self.iterable, key=cmp_to_key(comparator))) if comparator is not None else iter(sorted(
+            self.iterable))
+    return self
+
+
+
+def sum(self) +
+
+

Returns the sum of all elements of this stream. This is a special case of a reduction.

+

:return: an Optional describing the sum of all the elements of this stream, or an empty Optional if the stream is empty

+
+ +Expand source code + +
def sum(self):
+    '''
+    Returns the sum of all elements of this stream. This is a special case of a reduction.
+
+    :return: an Optional describing the sum of all the elements of this stream, or an empty Optional if the stream is empty
+    '''
+    return self.reduce(lambda x, y: x + y)
+
+
+
+def takeWhile(self, predicate) +
+
+

Returns a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.

+

:param Predicate predicate: +predicate to apply to elements to determine the longest prefix of elements. +:return: self

+
+ +Expand source code + +
def takeWhile(self, predicate):
+    '''
+    Returns a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.
+
+    :param Predicate predicate:  predicate to apply to elements to determine the longest prefix of elements.
+    :return: self
+    '''
+    self.iterable = IteratorUtils.takeWhile(self.iterable, predicate)
+    return self
+
+
+
+def toBooleanStream(self) +
+
+
+
+ +Expand source code + +
def toBooleanStream(self):
+    from .booleans import BooleanStream
+    return BooleanStream(self)
+
+
+
+def toList(self) +
+
+

Returns a list with the elements in this stream.

+

:return: the list of elements in this stream

+
+ +Expand source code + +
def toList(self):
+    '''
+    Returns a list with the elements in this stream.
+
+    :return: the list of elements in this stream
+    '''
+    return list(self.iterable)
+
+
+
+def toNumberStream(self) +
+
+
+
+ +Expand source code + +
def toNumberStream(self):
+    from .numbers import NumberStream
+    return NumberStream(self)
+
+
+
+def toSet(self) +
+
+

Returns a set with the elements in this stream.

+

:return: the set of elements in this stream

+
+ +Expand source code + +
def toSet(self):
+    '''
+    Returns a set with the elements in this stream.
+
+    :return: the set of elements in this stream
+    '''
+    return set(self.iterable)
+
+
+ + + +
+ +
+ + + + \ No newline at end of file diff --git a/doc/stream/util/index.html b/doc/stream/util/index.html new file mode 100644 index 0000000..516b8bc --- /dev/null +++ b/doc/stream/util/index.html @@ -0,0 +1,70 @@ + + + + + + +stream.util API documentation + + + + + + + + + + + +
+
+
+

Module stream.util

+
+
+
+
+

Sub-modules

+
+
stream.util.iterators
+
+
+
+
stream.util.optional
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+ + + \ No newline at end of file diff --git a/doc/stream/util/iterators.html b/doc/stream/util/iterators.html index c96d259..b7bb611 100644 --- a/doc/stream/util/iterators.html +++ b/doc/stream/util/iterators.html @@ -1,605 +1,487 @@ - - - - stream.util.iterators API documentation - - - - - - + + + +stream.util.iterators API documentation + + + + + + + + + - -
-
-

-stream.util.iterators

- - -
- View Source -
class IteratorUtils:
-
-    """
-    Iterator Utils
-    """
-
-    """
-    Methods for Static Method of Stream
-    """
-    @staticmethod
-    def iterate(seed, operator):
-        while(True):
-            yield seed
-            seed = operator(seed)
-
-    @staticmethod
-    def generate(generator):
-        while(True):
-            yield generator()
-
-    @staticmethod
-    def concat(*iterables):
-        for iterable in iterables:
-            for elem in iterable:
-                yield elem
-
-    """
-    Methods for Normal Method of Stream
-    """
-    @staticmethod
-    def filter(iterable, predicate):
-        for elem in iterable:
-            if(predicate(elem)):
-                yield elem
-
-    @staticmethod
-    def map(iterable, mapper):
-        for elem in iterable:
-            yield mapper(elem)
-
-    @staticmethod
-    def flatMap(iterable, mapper):
-        for elem in iterable:
-            for internal in mapper(elem):
-                yield internal
-
-    @staticmethod
-    def distinct(iterable):
-        elements = set()
-        for elem in iterable:
-            if elem not in elements:
-                elements.add(elem)
-                yield elem
-
-    @staticmethod
-    def peek(iterable, function):
-        for elem in iterable:
-            function(elem)
-            yield elem
-
-    @staticmethod
-    def takeWhile(iterable, predicate):
-        for elem in iterable:
-            if(predicate(elem)):
-                yield elem
-            else:
-                break
-
-    @staticmethod
-    def dropWhile(iterable, predicate):
-        toDrop = True
-        for elem in iterable:
-            if(not predicate(elem) and toDrop):
-                toDrop = False
-            if(not toDrop):
-                yield elem
-
-    @staticmethod
-    def skip(iterable, count):
-        index = 0
-        for elem in iterable:
-            index += 1
-            if(index > count):
-                yield elem
-
-    @staticmethod
-    def limit(iterable, count):
-        index = 0
-        for elem in iterable:
-            index += 1
-            if(index <= count):
-                yield elem
-            else:
-                break
-
- -
- -
-
-
- #   - - - class - IteratorUtils: -
- -
- View Source -
class IteratorUtils:
-
-    """
-    Iterator Utils
-    """
-
-    """
-    Methods for Static Method of Stream
-    """
-    @staticmethod
-    def iterate(seed, operator):
-        while(True):
-            yield seed
-            seed = operator(seed)
-
-    @staticmethod
-    def generate(generator):
-        while(True):
-            yield generator()
-
-    @staticmethod
-    def concat(*iterables):
-        for iterable in iterables:
-            for elem in iterable:
-                yield elem
-
-    """
-    Methods for Normal Method of Stream
-    """
-    @staticmethod
-    def filter(iterable, predicate):
-        for elem in iterable:
-            if(predicate(elem)):
-                yield elem
-
-    @staticmethod
-    def map(iterable, mapper):
-        for elem in iterable:
-            yield mapper(elem)
-
-    @staticmethod
-    def flatMap(iterable, mapper):
-        for elem in iterable:
-            for internal in mapper(elem):
-                yield internal
-
-    @staticmethod
-    def distinct(iterable):
-        elements = set()
-        for elem in iterable:
-            if elem not in elements:
-                elements.add(elem)
-                yield elem
-
-    @staticmethod
-    def peek(iterable, function):
-        for elem in iterable:
-            function(elem)
-            yield elem
-
-    @staticmethod
-    def takeWhile(iterable, predicate):
-        for elem in iterable:
-            if(predicate(elem)):
-                yield elem
-            else:
-                break
-
-    @staticmethod
-    def dropWhile(iterable, predicate):
-        toDrop = True
-        for elem in iterable:
-            if(not predicate(elem) and toDrop):
-                toDrop = False
-            if(not toDrop):
-                yield elem
-
-    @staticmethod
-    def skip(iterable, count):
-        index = 0
-        for elem in iterable:
-            index += 1
-            if(index > count):
-                yield elem
-
-    @staticmethod
-    def limit(iterable, count):
-        index = 0
-        for elem in iterable:
-            index += 1
-            if(index <= count):
-                yield elem
-            else:
-                break
-
- -
- -

Iterator Utils

+ +
+
+
+

Module stream.util.iterators

+
+
+
+ +Expand source code + +
class IteratorUtils:
+
+    """
+    Iterator Utils
+    """
+
+    """
+    Methods for Static Method of Stream
+    """
+    @staticmethod
+    def iterate(seed, operator):
+        while(True):
+            yield seed
+            seed = operator(seed)
+
+    @staticmethod
+    def generate(generator):
+        while(True):
+            yield generator()
+
+    @staticmethod
+    def concat(*iterables):
+        for iterable in iterables:
+            for elem in iterable:
+                yield elem
+
+    """
+    Methods for Normal Method of Stream
+    """
+    @staticmethod
+    def filter(iterable, predicate):
+        for elem in iterable:
+            if(predicate(elem)):
+                yield elem
+
+    @staticmethod
+    def map(iterable, mapper):
+        for elem in iterable:
+            yield mapper(elem)
+
+    @staticmethod
+    def flatMap(iterable, mapper):
+        for elem in iterable:
+            for internal in mapper(elem):
+                yield internal
+
+    @staticmethod
+    def distinct(iterable):
+        elements = set()
+        for elem in iterable:
+            if elem not in elements:
+                elements.add(elem)
+                yield elem
+
+    @staticmethod
+    def peek(iterable, function):
+        for elem in iterable:
+            function(elem)
+            yield elem
+
+    @staticmethod
+    def takeWhile(iterable, predicate):
+        for elem in iterable:
+            if(predicate(elem)):
+                yield elem
+            else:
+                break
+
+    @staticmethod
+    def dropWhile(iterable, predicate):
+        toDrop = True
+        for elem in iterable:
+            if(not predicate(elem) and toDrop):
+                toDrop = False
+            if(not toDrop):
+                yield elem
+
+    @staticmethod
+    def skip(iterable, count):
+        index = 0
+        for elem in iterable:
+            index += 1
+            if(index > count):
+                yield elem
+
+    @staticmethod
+    def limit(iterable, count):
+        index = 0
+        for elem in iterable:
+            index += 1
+            if(index <= count):
+                yield elem
+            else:
+                break
+
+
+
+
+
+
+
+
+
+

Classes

+
+
+class IteratorUtils +
+
+

Iterator Utils

+
+ +Expand source code + +
class IteratorUtils:
+
+    """
+    Iterator Utils
+    """
+
+    """
+    Methods for Static Method of Stream
+    """
+    @staticmethod
+    def iterate(seed, operator):
+        while(True):
+            yield seed
+            seed = operator(seed)
+
+    @staticmethod
+    def generate(generator):
+        while(True):
+            yield generator()
+
+    @staticmethod
+    def concat(*iterables):
+        for iterable in iterables:
+            for elem in iterable:
+                yield elem
+
+    """
+    Methods for Normal Method of Stream
+    """
+    @staticmethod
+    def filter(iterable, predicate):
+        for elem in iterable:
+            if(predicate(elem)):
+                yield elem
+
+    @staticmethod
+    def map(iterable, mapper):
+        for elem in iterable:
+            yield mapper(elem)
+
+    @staticmethod
+    def flatMap(iterable, mapper):
+        for elem in iterable:
+            for internal in mapper(elem):
+                yield internal
+
+    @staticmethod
+    def distinct(iterable):
+        elements = set()
+        for elem in iterable:
+            if elem not in elements:
+                elements.add(elem)
+                yield elem
+
+    @staticmethod
+    def peek(iterable, function):
+        for elem in iterable:
+            function(elem)
+            yield elem
+
+    @staticmethod
+    def takeWhile(iterable, predicate):
+        for elem in iterable:
+            if(predicate(elem)):
+                yield elem
+            else:
+                break
+
+    @staticmethod
+    def dropWhile(iterable, predicate):
+        toDrop = True
+        for elem in iterable:
+            if(not predicate(elem) and toDrop):
+                toDrop = False
+            if(not toDrop):
+                yield elem
+
+    @staticmethod
+    def skip(iterable, count):
+        index = 0
+        for elem in iterable:
+            index += 1
+            if(index > count):
+                yield elem
+
+    @staticmethod
+    def limit(iterable, count):
+        index = 0
+        for elem in iterable:
+            index += 1
+            if(index <= count):
+                yield elem
+            else:
+                break
+
+

Static methods

+
+
+def concat(*iterables) +
+
+
+
+ +Expand source code + +
@staticmethod
+def concat(*iterables):
+    for iterable in iterables:
+        for elem in iterable:
+            yield elem
+
+
+
+def distinct(iterable) +
+
+
+
+ +Expand source code + +
@staticmethod
+def distinct(iterable):
+    elements = set()
+    for elem in iterable:
+        if elem not in elements:
+            elements.add(elem)
+            yield elem
+
+
+
+def dropWhile(iterable, predicate) +
+
+
+
+ +Expand source code + +
@staticmethod
+def dropWhile(iterable, predicate):
+    toDrop = True
+    for elem in iterable:
+        if(not predicate(elem) and toDrop):
+            toDrop = False
+        if(not toDrop):
+            yield elem
+
+
+
+def filter(iterable, predicate) +
+
+
+
+ +Expand source code + +
@staticmethod
+def filter(iterable, predicate):
+    for elem in iterable:
+        if(predicate(elem)):
+            yield elem
+
+
+
+def flatMap(iterable, mapper) +
+
+
+
+ +Expand source code + +
@staticmethod
+def flatMap(iterable, mapper):
+    for elem in iterable:
+        for internal in mapper(elem):
+            yield internal
+
+
+
+def generate(generator) +
+
+
+
+ +Expand source code + +
@staticmethod
+def generate(generator):
+    while(True):
+        yield generator()
+
+
+
+def iterate(seed, operator) +
+
+
+
+ +Expand source code + +
@staticmethod
+def iterate(seed, operator):
+    while(True):
+        yield seed
+        seed = operator(seed)
+
+
+
+def limit(iterable, count) +
+
+
+
+ +Expand source code + +
@staticmethod
+def limit(iterable, count):
+    index = 0
+    for elem in iterable:
+        index += 1
+        if(index <= count):
+            yield elem
+        else:
+            break
+
+
+
+def map(iterable, mapper) +
+
+
+
+ +Expand source code + +
@staticmethod
+def map(iterable, mapper):
+    for elem in iterable:
+        yield mapper(elem)
+
+
+
+def peek(iterable, function) +
+
+
+
+ +Expand source code + +
@staticmethod
+def peek(iterable, function):
+    for elem in iterable:
+        function(elem)
+        yield elem
+
+
+
+def skip(iterable, count) +
+
+
+
+ +Expand source code + +
@staticmethod
+def skip(iterable, count):
+    index = 0
+    for elem in iterable:
+        index += 1
+        if(index > count):
+            yield elem
+
+
+
+def takeWhile(iterable, predicate) +
+
+
+
+ +Expand source code + +
@staticmethod
+def takeWhile(iterable, predicate):
+    for elem in iterable:
+        if(predicate(elem)):
+            yield elem
+        else:
+            break
+
+
+
+
+
+
+
+
-
+ + + + \ No newline at end of file diff --git a/doc/stream/util/optional.html b/doc/stream/util/optional.html index c226a73..73ee428 100644 --- a/doc/stream/util/optional.html +++ b/doc/stream/util/optional.html @@ -1,703 +1,648 @@ - - - - stream.util.optional API documentation - - - - - - + + + +stream.util.optional API documentation + + + + + + + + + - -
-
-

-stream.util.optional

- - -
- View Source -
class Optional:
-
-    '''
-    A container object which may or may not contain a non-null value. If a value is present, isPresent() returns true. If no value is present, the object is considered empty and isPresent() returns false.
-
-    Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (returns a default value if no value is present) and ifPresent() (performs an action if a value is present). 
-    '''
-
-    '''
-    STATIC METHODS
-    '''
-
-    @staticmethod
-    def empty():
-        '''
-        Returns an empty Optional instance. No value is present for this Optional.
-
-        :return: an empty Optional
-        '''
-        return Optional(None)
-
-    @staticmethod
-    def of(elem):
-        '''
-        Returns an Optional describing the given non-null value.
-
-        :param T elem: the value to describe, which must be non-null
-        :return: an Optional with the value present
-        :raise: Exception if the value is null
-        '''
-        if elem is None:
-            raise Exception("Optional Empty")
-        return Optional(elem)
-
-    @staticmethod
-    def ofNullable(elem):
-        '''
-        Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional.
-
-        :param T elem: the possibly-null value to describe
-        :return: an Optional with a present value if the specified value is non-null, otherwise an empty Optional
-        '''
-        return Optional(elem)
-
-    '''
-    NON STATIC METHODS
-    '''
-
-    def __init__(self, elem):
-        self.__elem = elem
-
-    def get(self):
-        '''
-        If a value is present, returns the value, otherwise raise an Exception.
-
-        :return: the non-null value described by this Optional
-        :raise: Exception if no value is present
-        '''
-        if self.isEmpty():
-            raise Exception("Optional Empty")
-        return self.__elem
-
-    def isPresent(self):
-        '''
-        If a value is present, returns true, otherwise false.
-
-        :return: true if a value is present, otherwise false
-        '''
-        return not self.isEmpty()
-
-    def isEmpty(self):
-        '''
-        If a value is not present, returns true, otherwise false.
-
-        :return: true if a value is not present, otherwise false
-        '''
-        return self.__elem is None
-
-    def ifPresent(self, action):
-        '''
-        If a value is present, performs the given action with the value, otherwise does nothing.
-
-        :param Function action: the action to be performed, if a value is present
-        :raise Exception if value is present and the given action is null
-        '''
-        if self.isPresent():
-            action(self.get())
-
-    def ifPresentOrElse(self, action, emptyAction):
-        '''
-        If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
-
-        :param Function action: the action to be performed, if a value is present
-        :param Function emptyAction: the empty-based action to be performed, if no value is present
-        :raise Exception if a value is present and the given action is null, or no value is present and the given empty-based action is null.
-        '''
-        if self.isPresent():
-            action(self.get())
-        else:
-            emptyAction()
-
-    def filter(self, predicate):
-        '''
-        If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.
-
-        :param Predicate predicate: the predicate to apply to a value, if present
-        :return: an Optional describing the value of this Optional, if a value is present and the value matches the given predicate, otherwise an empty Optional
-        '''
-        if self.isPresent() and predicate(self.get()):
-            return self
-        else:
-            return Optional.empty()
-
-    def map(self, mapper):
-        '''
-        If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional. 
-        If the mapping function returns a null result then this method returns an empty Optional.
-
-        :param Mapper mapper: the mapping function to apply to a value, if present
-        :return: an Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise an empty Optional
-        '''
-        if self.isPresent():
-            return Optional.ofNullable(mapper(self.get()))
-        else:
-            return Optional.empty()
-
- -
- -
-
-
- #   - - - class - Optional: -
- -
- View Source -
class Optional:
-
-    '''
-    A container object which may or may not contain a non-null value. If a value is present, isPresent() returns true. If no value is present, the object is considered empty and isPresent() returns false.
-
-    Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (returns a default value if no value is present) and ifPresent() (performs an action if a value is present). 
-    '''
-
-    '''
-    STATIC METHODS
-    '''
-
-    @staticmethod
-    def empty():
-        '''
-        Returns an empty Optional instance. No value is present for this Optional.
-
-        :return: an empty Optional
-        '''
-        return Optional(None)
-
-    @staticmethod
-    def of(elem):
-        '''
-        Returns an Optional describing the given non-null value.
-
-        :param T elem: the value to describe, which must be non-null
-        :return: an Optional with the value present
-        :raise: Exception if the value is null
-        '''
-        if elem is None:
-            raise Exception("Optional Empty")
-        return Optional(elem)
-
-    @staticmethod
-    def ofNullable(elem):
-        '''
-        Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional.
-
-        :param T elem: the possibly-null value to describe
-        :return: an Optional with a present value if the specified value is non-null, otherwise an empty Optional
-        '''
-        return Optional(elem)
-
-    '''
-    NON STATIC METHODS
-    '''
-
-    def __init__(self, elem):
-        self.__elem = elem
-
-    def get(self):
-        '''
-        If a value is present, returns the value, otherwise raise an Exception.
-
-        :return: the non-null value described by this Optional
-        :raise: Exception if no value is present
-        '''
-        if self.isEmpty():
-            raise Exception("Optional Empty")
-        return self.__elem
-
-    def isPresent(self):
-        '''
-        If a value is present, returns true, otherwise false.
-
-        :return: true if a value is present, otherwise false
-        '''
-        return not self.isEmpty()
-
-    def isEmpty(self):
-        '''
-        If a value is not present, returns true, otherwise false.
-
-        :return: true if a value is not present, otherwise false
-        '''
-        return self.__elem is None
-
-    def ifPresent(self, action):
-        '''
-        If a value is present, performs the given action with the value, otherwise does nothing.
-
-        :param Function action: the action to be performed, if a value is present
-        :raise Exception if value is present and the given action is null
-        '''
-        if self.isPresent():
-            action(self.get())
-
-    def ifPresentOrElse(self, action, emptyAction):
-        '''
-        If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
-
-        :param Function action: the action to be performed, if a value is present
-        :param Function emptyAction: the empty-based action to be performed, if no value is present
-        :raise Exception if a value is present and the given action is null, or no value is present and the given empty-based action is null.
-        '''
-        if self.isPresent():
-            action(self.get())
-        else:
-            emptyAction()
-
-    def filter(self, predicate):
-        '''
-        If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.
-
-        :param Predicate predicate: the predicate to apply to a value, if present
-        :return: an Optional describing the value of this Optional, if a value is present and the value matches the given predicate, otherwise an empty Optional
-        '''
-        if self.isPresent() and predicate(self.get()):
-            return self
-        else:
-            return Optional.empty()
-
-    def map(self, mapper):
-        '''
-        If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional. 
-        If the mapping function returns a null result then this method returns an empty Optional.
-
-        :param Mapper mapper: the mapping function to apply to a value, if present
-        :return: an Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise an empty Optional
-        '''
-        if self.isPresent():
-            return Optional.ofNullable(mapper(self.get()))
-        else:
-            return Optional.empty()
-
- -
- -

A container object which may or may not contain a non-null value. If a value is present, isPresent() returns true. If no value is present, the object is considered empty and isPresent() returns false.

- -

Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (returns a default value if no value is present) and ifPresent() (performs an action if a value is present).

-
- - -
-
#   - - - Optional(elem)
- -
- View Source -
    def __init__(self, elem):
-        self.__elem = elem
-
- -
- - - -
-
-
#   - -
@staticmethod
- - def - empty(): -
- -
- View Source -
    @staticmethod
-    def empty():
-        '''
-        Returns an empty Optional instance. No value is present for this Optional.
-
-        :return: an empty Optional
-        '''
-        return Optional(None)
-
- -
- -

Returns an empty Optional instance. No value is present for this Optional.

- -

:return: an empty Optional

-
- - -
-
-
#   - -
@staticmethod
- - def - of(elem): -
- -
- View Source -
    @staticmethod
-    def of(elem):
-        '''
-        Returns an Optional describing the given non-null value.
-
-        :param T elem: the value to describe, which must be non-null
-        :return: an Optional with the value present
-        :raise: Exception if the value is null
-        '''
-        if elem is None:
-            raise Exception("Optional Empty")
-        return Optional(elem)
-
- -
- -

Returns an Optional describing the given non-null value.

- + +
+
+
+

Module stream.util.optional

+
+
+
+ +Expand source code + +
class Optional:
+
+    '''
+    A container object which may or may not contain a non-null value. If a value is present, isPresent() returns true. If no value is present, the object is considered empty and isPresent() returns false.
+
+    Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (returns a default value if no value is present) and ifPresent() (performs an action if a value is present). 
+    '''
+
+    '''
+    STATIC METHODS
+    '''
+
+    @staticmethod
+    def empty():
+        '''
+        Returns an empty Optional instance. No value is present for this Optional.
+
+        :return: an empty Optional
+        '''
+        return Optional(None)
+
+    @staticmethod
+    def of(elem):
+        '''
+        Returns an Optional describing the given non-null value.
+
+        :param T elem: the value to describe, which must be non-null
+        :return: an Optional with the value present
+        :raise: Exception if the value is null
+        '''
+        if elem is None:
+            raise Exception("Optional Empty")
+        return Optional(elem)
+
+    @staticmethod
+    def ofNullable(elem):
+        '''
+        Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional.
+
+        :param T elem: the possibly-null value to describe
+        :return: an Optional with a present value if the specified value is non-null, otherwise an empty Optional
+        '''
+        return Optional(elem)
+
+    '''
+    NON STATIC METHODS
+    '''
+
+    def __init__(self, elem):
+        self.__elem = elem
+
+    def get(self):
+        '''
+        If a value is present, returns the value, otherwise raise an Exception.
+
+        :return: the non-null value described by this Optional
+        :raise: Exception if no value is present
+        '''
+        if self.isEmpty():
+            raise Exception("Optional Empty")
+        return self.__elem
+
+    def isPresent(self):
+        '''
+        If a value is present, returns true, otherwise false.
+
+        :return: true if a value is present, otherwise false
+        '''
+        return not self.isEmpty()
+
+    def isEmpty(self):
+        '''
+        If a value is not present, returns true, otherwise false.
+
+        :return: true if a value is not present, otherwise false
+        '''
+        return self.__elem is None
+
+    def ifPresent(self, action):
+        '''
+        If a value is present, performs the given action with the value, otherwise does nothing.
+
+        :param Function action: the action to be performed, if a value is present
+        :raise Exception if value is present and the given action is null
+        '''
+        if self.isPresent():
+            action(self.get())
+
+    def ifPresentOrElse(self, action, emptyAction):
+        '''
+        If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
+
+        :param Function action: the action to be performed, if a value is present
+        :param Function emptyAction: the empty-based action to be performed, if no value is present
+        :raise Exception if a value is present and the given action is null, or no value is present and the given empty-based action is null.
+        '''
+        if self.isPresent():
+            action(self.get())
+        else:
+            emptyAction()
+
+    def orElse(self, value):
+        '''
+        Returns the value if present, or a provided argument otherwise. It's a safe alternative to get() method.
+
+        :return: the non-null value described by this Optional or the value passed as an argument
+        '''
+        return self.__elem if self.isPresent() else value
+
+    def orElseGet(self, supplier):
+        '''
+        Returns either a stored value, or calls a supplier otherwise. It's a safe alternative to get() method.
+
+        :return: the non-null value described by this Optional or the result of supplier argument
+        '''
+        return self.__elem if self.isPresent() else supplier()
+
+    def filter(self, predicate):
+        '''
+        If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.
+
+        :param Predicate predicate: the predicate to apply to a value, if present
+        :return: an Optional describing the value of this Optional, if a value is present and the value matches the given predicate, otherwise an empty Optional
+        '''
+        if self.isPresent() and predicate(self.get()):
+            return self
+        else:
+            return Optional.empty()
+
+    def map(self, mapper):
+        '''
+        If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional. 
+        If the mapping function returns a null result then this method returns an empty Optional.
+
+        :param Mapper mapper: the mapping function to apply to a value, if present
+        :return: an Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise an empty Optional
+        '''
+        if self.isPresent():
+            return Optional.ofNullable(mapper(self.get()))
+        else:
+            return Optional.empty()
+
+
+
+
+
+
+
+
+
+

Classes

+
+
+class Optional +(elem) +
+
+

A container object which may or may not contain a non-null value. If a value is present, isPresent() returns true. If no value is present, the object is considered empty and isPresent() returns false.

+

Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (returns a default value if no value is present) and ifPresent() (performs an action if a value is present).

+
+ +Expand source code + +
class Optional:
+
+    '''
+    A container object which may or may not contain a non-null value. If a value is present, isPresent() returns true. If no value is present, the object is considered empty and isPresent() returns false.
+
+    Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (returns a default value if no value is present) and ifPresent() (performs an action if a value is present). 
+    '''
+
+    '''
+    STATIC METHODS
+    '''
+
+    @staticmethod
+    def empty():
+        '''
+        Returns an empty Optional instance. No value is present for this Optional.
+
+        :return: an empty Optional
+        '''
+        return Optional(None)
+
+    @staticmethod
+    def of(elem):
+        '''
+        Returns an Optional describing the given non-null value.
+
+        :param T elem: the value to describe, which must be non-null
+        :return: an Optional with the value present
+        :raise: Exception if the value is null
+        '''
+        if elem is None:
+            raise Exception("Optional Empty")
+        return Optional(elem)
+
+    @staticmethod
+    def ofNullable(elem):
+        '''
+        Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional.
+
+        :param T elem: the possibly-null value to describe
+        :return: an Optional with a present value if the specified value is non-null, otherwise an empty Optional
+        '''
+        return Optional(elem)
+
+    '''
+    NON STATIC METHODS
+    '''
+
+    def __init__(self, elem):
+        self.__elem = elem
+
+    def get(self):
+        '''
+        If a value is present, returns the value, otherwise raise an Exception.
+
+        :return: the non-null value described by this Optional
+        :raise: Exception if no value is present
+        '''
+        if self.isEmpty():
+            raise Exception("Optional Empty")
+        return self.__elem
+
+    def isPresent(self):
+        '''
+        If a value is present, returns true, otherwise false.
+
+        :return: true if a value is present, otherwise false
+        '''
+        return not self.isEmpty()
+
+    def isEmpty(self):
+        '''
+        If a value is not present, returns true, otherwise false.
+
+        :return: true if a value is not present, otherwise false
+        '''
+        return self.__elem is None
+
+    def ifPresent(self, action):
+        '''
+        If a value is present, performs the given action with the value, otherwise does nothing.
+
+        :param Function action: the action to be performed, if a value is present
+        :raise Exception if value is present and the given action is null
+        '''
+        if self.isPresent():
+            action(self.get())
+
+    def ifPresentOrElse(self, action, emptyAction):
+        '''
+        If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
+
+        :param Function action: the action to be performed, if a value is present
+        :param Function emptyAction: the empty-based action to be performed, if no value is present
+        :raise Exception if a value is present and the given action is null, or no value is present and the given empty-based action is null.
+        '''
+        if self.isPresent():
+            action(self.get())
+        else:
+            emptyAction()
+
+    def orElse(self, value):
+        '''
+        Returns the value if present, or a provided argument otherwise. It's a safe alternative to get() method.
+
+        :return: the non-null value described by this Optional or the value passed as an argument
+        '''
+        return self.__elem if self.isPresent() else value
+
+    def orElseGet(self, supplier):
+        '''
+        Returns either a stored value, or calls a supplier otherwise. It's a safe alternative to get() method.
+
+        :return: the non-null value described by this Optional or the result of supplier argument
+        '''
+        return self.__elem if self.isPresent() else supplier()
+
+    def filter(self, predicate):
+        '''
+        If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.
+
+        :param Predicate predicate: the predicate to apply to a value, if present
+        :return: an Optional describing the value of this Optional, if a value is present and the value matches the given predicate, otherwise an empty Optional
+        '''
+        if self.isPresent() and predicate(self.get()):
+            return self
+        else:
+            return Optional.empty()
+
+    def map(self, mapper):
+        '''
+        If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional. 
+        If the mapping function returns a null result then this method returns an empty Optional.
+
+        :param Mapper mapper: the mapping function to apply to a value, if present
+        :return: an Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise an empty Optional
+        '''
+        if self.isPresent():
+            return Optional.ofNullable(mapper(self.get()))
+        else:
+            return Optional.empty()
+
+

Static methods

+
+
+def empty() +
+
+

Returns an empty Optional instance. No value is present for this Optional.

+

:return: an empty Optional

+
+ +Expand source code + +
@staticmethod
+def empty():
+    '''
+    Returns an empty Optional instance. No value is present for this Optional.
+
+    :return: an empty Optional
+    '''
+    return Optional(None)
+
+
+
+def of(elem) +
+
+

Returns an Optional describing the given non-null value.

:param T elem: the value to describe, which must be non-null :return: an Optional with the value present -:raise: Exception if the value is null

-
- - -
-
-
#   - -
@staticmethod
- - def - ofNullable(elem): -
- -
- View Source -
    @staticmethod
-    def ofNullable(elem):
-        '''
-        Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional.
-
-        :param T elem: the possibly-null value to describe
-        :return: an Optional with a present value if the specified value is non-null, otherwise an empty Optional
-        '''
-        return Optional(elem)
-
- -
- -

Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional.

- +:raise: Exception if the value is null

+
+ +Expand source code + +
@staticmethod
+def of(elem):
+    '''
+    Returns an Optional describing the given non-null value.
+
+    :param T elem: the value to describe, which must be non-null
+    :return: an Optional with the value present
+    :raise: Exception if the value is null
+    '''
+    if elem is None:
+        raise Exception("Optional Empty")
+    return Optional(elem)
+
+ +
+def ofNullable(elem) +
+
+

Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional.

:param T elem: the possibly-null value to describe -:return: an Optional with a present value if the specified value is non-null, otherwise an empty Optional

-
- - -
-
-
#   - - - def - get(self): -
- -
- View Source -
    def get(self):
-        '''
-        If a value is present, returns the value, otherwise raise an Exception.
-
-        :return: the non-null value described by this Optional
-        :raise: Exception if no value is present
-        '''
-        if self.isEmpty():
-            raise Exception("Optional Empty")
-        return self.__elem
-
- -
- -

If a value is present, returns the value, otherwise raise an Exception.

- +:return: an Optional with a present value if the specified value is non-null, otherwise an empty Optional

+
+ +Expand source code + +
@staticmethod
+def ofNullable(elem):
+    '''
+    Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional.
+
+    :param T elem: the possibly-null value to describe
+    :return: an Optional with a present value if the specified value is non-null, otherwise an empty Optional
+    '''
+    return Optional(elem)
+
+ + +

Methods

+
+
+def filter(self, predicate) +
+
+

If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.

+

:param Predicate predicate: the predicate to apply to a value, if present +:return: an Optional describing the value of this Optional, if a value is present and the value matches the given predicate, otherwise an empty Optional

+
+ +Expand source code + +
def filter(self, predicate):
+    '''
+    If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.
+
+    :param Predicate predicate: the predicate to apply to a value, if present
+    :return: an Optional describing the value of this Optional, if a value is present and the value matches the given predicate, otherwise an empty Optional
+    '''
+    if self.isPresent() and predicate(self.get()):
+        return self
+    else:
+        return Optional.empty()
+
+
+
+def get(self) +
+
+

If a value is present, returns the value, otherwise raise an Exception.

:return: the non-null value described by this Optional -:raise: Exception if no value is present

-
- - -
-
-
#   - - - def - isPresent(self): -
- -
- View Source -
    def isPresent(self):
-        '''
-        If a value is present, returns true, otherwise false.
-
-        :return: true if a value is present, otherwise false
-        '''
-        return not self.isEmpty()
-
- -
- -

If a value is present, returns true, otherwise false.

- -

:return: true if a value is present, otherwise false

-
- - -
-
-
#   - - - def - isEmpty(self): -
- -
- View Source -
    def isEmpty(self):
-        '''
-        If a value is not present, returns true, otherwise false.
-
-        :return: true if a value is not present, otherwise false
-        '''
-        return self.__elem is None
-
- -
- -

If a value is not present, returns true, otherwise false.

- -

:return: true if a value is not present, otherwise false

-
- - -
-
-
#   - - - def - ifPresent(self, action): -
- -
- View Source -
    def ifPresent(self, action):
-        '''
-        If a value is present, performs the given action with the value, otherwise does nothing.
-
-        :param Function action: the action to be performed, if a value is present
-        :raise Exception if value is present and the given action is null
-        '''
-        if self.isPresent():
-            action(self.get())
-
- -
- -

If a value is present, performs the given action with the value, otherwise does nothing.

- +:raise: Exception if no value is present

+
+ +Expand source code + +
def get(self):
+    '''
+    If a value is present, returns the value, otherwise raise an Exception.
+
+    :return: the non-null value described by this Optional
+    :raise: Exception if no value is present
+    '''
+    if self.isEmpty():
+        raise Exception("Optional Empty")
+    return self.__elem
+
+ +
+def ifPresent(self, action) +
+
+

If a value is present, performs the given action with the value, otherwise does nothing.

:param Function action: the action to be performed, if a value is present -:raise Exception if value is present and the given action is null

-
- - -
-
-
#   - - - def - ifPresentOrElse(self, action, emptyAction): -
- -
- View Source -
    def ifPresentOrElse(self, action, emptyAction):
-        '''
-        If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
-
-        :param Function action: the action to be performed, if a value is present
-        :param Function emptyAction: the empty-based action to be performed, if no value is present
-        :raise Exception if a value is present and the given action is null, or no value is present and the given empty-based action is null.
-        '''
-        if self.isPresent():
-            action(self.get())
-        else:
-            emptyAction()
-
- -
- -

If a value is present, performs the given action with the value, otherwise performs the given empty-based action.

- +:raise Exception if value is present and the given action is null

+
+ +Expand source code + +
def ifPresent(self, action):
+    '''
+    If a value is present, performs the given action with the value, otherwise does nothing.
+
+    :param Function action: the action to be performed, if a value is present
+    :raise Exception if value is present and the given action is null
+    '''
+    if self.isPresent():
+        action(self.get())
+
+ +
+def ifPresentOrElse(self, action, emptyAction) +
+
+

If a value is present, performs the given action with the value, otherwise performs the given empty-based action.

:param Function action: the action to be performed, if a value is present :param Function emptyAction: the empty-based action to be performed, if no value is present -:raise Exception if a value is present and the given action is null, or no value is present and the given empty-based action is null.

-
- - -
-
-
#   - - - def - filter(self, predicate): -
- -
- View Source -
    def filter(self, predicate):
-        '''
-        If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.
-
-        :param Predicate predicate: the predicate to apply to a value, if present
-        :return: an Optional describing the value of this Optional, if a value is present and the value matches the given predicate, otherwise an empty Optional
-        '''
-        if self.isPresent() and predicate(self.get()):
-            return self
-        else:
-            return Optional.empty()
-
- -
- -

If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.

- -

:param Predicate predicate: the predicate to apply to a value, if present -:return: an Optional describing the value of this Optional, if a value is present and the value matches the given predicate, otherwise an empty Optional

-
- - -
-
-
#   - - - def - map(self, mapper): -
- -
- View Source -
    def map(self, mapper):
-        '''
-        If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional. 
-        If the mapping function returns a null result then this method returns an empty Optional.
-
-        :param Mapper mapper: the mapping function to apply to a value, if present
-        :return: an Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise an empty Optional
-        '''
-        if self.isPresent():
-            return Optional.ofNullable(mapper(self.get()))
-        else:
-            return Optional.empty()
-
- -
- -

If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional. +:raise Exception if a value is present and the given action is null, or no value is present and the given empty-based action is null.

+
+ +Expand source code + +
def ifPresentOrElse(self, action, emptyAction):
+    '''
+    If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
+
+    :param Function action: the action to be performed, if a value is present
+    :param Function emptyAction: the empty-based action to be performed, if no value is present
+    :raise Exception if a value is present and the given action is null, or no value is present and the given empty-based action is null.
+    '''
+    if self.isPresent():
+        action(self.get())
+    else:
+        emptyAction()
+
+ +
+def isEmpty(self) +
+
+

If a value is not present, returns true, otherwise false.

+

:return: true if a value is not present, otherwise false

+
+ +Expand source code + +
def isEmpty(self):
+    '''
+    If a value is not present, returns true, otherwise false.
+
+    :return: true if a value is not present, otherwise false
+    '''
+    return self.__elem is None
+
+
+
+def isPresent(self) +
+
+

If a value is present, returns true, otherwise false.

+

:return: true if a value is present, otherwise false

+
+ +Expand source code + +
def isPresent(self):
+    '''
+    If a value is present, returns true, otherwise false.
+
+    :return: true if a value is present, otherwise false
+    '''
+    return not self.isEmpty()
+
+
+
+def map(self, mapper) +
+
+

If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional. If the mapping function returns a null result then this method returns an empty Optional.

-

:param Mapper mapper: the mapping function to apply to a value, if present -:return: an Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise an empty Optional

+:return: an Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise an empty Optional

+
+ +Expand source code + +
def map(self, mapper):
+    '''
+    If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional. 
+    If the mapping function returns a null result then this method returns an empty Optional.
+
+    :param Mapper mapper: the mapping function to apply to a value, if present
+    :return: an Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise an empty Optional
+    '''
+    if self.isPresent():
+        return Optional.ofNullable(mapper(self.get()))
+    else:
+        return Optional.empty()
+
+
+
+def orElse(self, value) +
+
+

Returns the value if present, or a provided argument otherwise. It's a safe alternative to get() method.

+

:return: the non-null value described by this Optional or the value passed as an argument

+
+ +Expand source code + +
def orElse(self, value):
+    '''
+    Returns the value if present, or a provided argument otherwise. It's a safe alternative to get() method.
+
+    :return: the non-null value described by this Optional or the value passed as an argument
+    '''
+    return self.__elem if self.isPresent() else value
+
+
+
+def orElseGet(self, supplier) +
+
+

Returns either a stored value, or calls a supplier otherwise. It's a safe alternative to get() method.

+

:return: the non-null value described by this Optional or the result of supplier argument

+
+ +Expand source code + +
def orElseGet(self, supplier):
+    '''
+    Returns either a stored value, or calls a supplier otherwise. It's a safe alternative to get() method.
+
+    :return: the non-null value described by this Optional or the result of supplier argument
+    '''
+    return self.__elem if self.isPresent() else supplier()
+
+
+ + + +
+ +
+ + + + \ No newline at end of file diff --git a/stream/stream.py b/stream/stream.py index cf47889..2e0d51e 100644 --- a/stream/stream.py +++ b/stream/stream.py @@ -1,5 +1,4 @@ from functools import cmp_to_key -import random from .util.iterators import IteratorUtils from .util.optional import Optional @@ -47,6 +46,7 @@ def of(elem): return Stream(iter([elem])) @staticmethod + # skipcq: PYL-E0102 def of(*elements): ''' Returns a sequential ordered stream whose elements are the specified values. @@ -256,12 +256,21 @@ def noneMatch(self, predicate): ''' return not self.anyMatch(predicate) - def findFirst(self): - ''' - Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned. + def findFirst(self, predicate=None): + """ + Returns an Optional describing the first element + (with optional filtering by a given predicate) of this stream, + or an empty Optional if the stream is empty. + If the stream has no encounter order, then any element may be returned. + + :param Predicate predicate: optional predicate to apply to elements + of this stream + :return: an Optional describing the first element of this stream, + or an empty Optional if the stream is empty + """ + if predicate: + self.filter(predicate) - :return: an Optional describing the first element of this stream, or an empty Optional if the stream is empty - ''' for elem in self.iterable: return Optional.of(elem) return Optional.ofNullable(None) diff --git a/stream/util/optional.py b/stream/util/optional.py index eb62ff0..7f0becd 100644 --- a/stream/util/optional.py +++ b/stream/util/optional.py @@ -99,6 +99,26 @@ def ifPresentOrElse(self, action, emptyAction): else: emptyAction() + def orElse(self, value): + """ + Returns the value if present, or a provided argument otherwise. + It's a safe alternative to get() method. + + :return: the non-null value described by this Optional + or the value passed as an argument. + """ + return self.__elem if self.isPresent() else value + + def orElseGet(self, supplier): + """ + Returns either a stored value, or calls a supplier otherwise. + It's a safe alternative to get() method. + + :return: the non-null value described by this Optional + or the result of supplier argument. + """ + return self.__elem if self.isPresent() else supplier() + def filter(self, predicate): ''' If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.