From 57c9949dd6cbfe1364acc36eb98f50945f72d6f9 Mon Sep 17 00:00:00 2001 From: Joshix-1 Date: Wed, 8 Feb 2023 20:00:00 +0000 Subject: [PATCH] v0.0.4 --- test/__main__.py | 7 +++++++ typed_stream/__init__.py | 25 +++++++++++++++++++++---- typed_stream/version.py | 2 +- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/test/__main__.py b/test/__main__.py index aa1acaa..353b15e 100644 --- a/test/__main__.py +++ b/test/__main__.py @@ -1,4 +1,5 @@ """The tests for the Stream.""" +from operator import add from pathlib import Path from typing import TypeGuard @@ -40,10 +41,16 @@ def create_int_stream() -> Stream[int]: sum(create_int_stream()) == create_int_stream().reduce(lambda x, y: x + y) == create_int_stream().reduce(int.__add__) + == create_int_stream().reduce(add) == create_int_stream().sum() == create_int_stream().collect(lambda x: sum(x)) ) +max_: int = Stream([1, 2, 3, -1]).max() +assert max_ == 3 +min_: int = Stream([1, 2, -1, 3]).min() +assert min_ == -1 + assert tuple(Stream([1, 2, 2, 2, 3]).distinct()) == (1, 2, 3) assert tuple(Stream([1, 2, 1, 1, 2, 1, 2, 3, 3, 3, 2, 2, 1]).distinct()) == ( 1, diff --git a/typed_stream/__init__.py b/typed_stream/__init__.py index fe898a6..6408547 100644 --- a/typed_stream/__init__.py +++ b/typed_stream/__init__.py @@ -19,11 +19,13 @@ import contextlib from collections.abc import Callable, Iterable, Iterator from itertools import chain, count, repeat +from operator import add from os import PathLike from types import EllipsisType from typing import ( TYPE_CHECKING, Any, + Literal, Protocol, TextIO, TypeGuard, @@ -101,6 +103,21 @@ class StreamEmptyError(_StreamErrorBase): """The Stream is empty.""" +def one(*args: Any) -> Literal[1]: + """Return the smallest positive odd number.""" + return 1 + + +def bigger_one(xxx: SLT, yyy: SLT) -> SLT: + """Return the bigger element.""" + return xxx if xxx > yyy else yyy + + +def smaller_one(xxx: SLT, yyy: SLT) -> SLT: + """Return the smaller element.""" + return yyy if xxx > yyy else xxx + + class Stream(Iterable[T]): """Stream class providing an interface similar to Stream in Java. @@ -237,7 +254,7 @@ def collect(self, fun: Callable[[Iterator[T]], K]) -> K: def count(self) -> int: """Count the elements in this Stream. This finishes the Stream.""" self._check_finished() - return sum(self.map(lambda _: 1)) + return sum(self.map(one)) def distinct(self, use_set: bool = True) -> "Stream[T]": """Remove duplicate values. @@ -364,11 +381,11 @@ def map(self, fun: Callable[[T], K]) -> "Stream[K]": def max(self: "Stream[SLT]") -> SLT: """Return the biggest element of the stream.""" - return self.reduce(lambda x, y: x if y < x else y) + return self.reduce(bigger_one) def min(self: "Stream[SLT]") -> SLT: """Return the smallest element of the stream.""" - return self.reduce(lambda x, y: y if y < x else x) + return self.reduce(smaller_one) def concurrent_map( self, fun: Callable[[T], K], max_workers: int | None = None @@ -424,7 +441,7 @@ def starcollect(self, fun: StarCallable[T, K]) -> K: def sum(self: "Stream[SA]") -> SA: """Calculate the sum of the elements.""" - return self.reduce(lambda x, y: x + y) + return self.reduce(add) class LazyFileIterator(Iterator[str]): diff --git a/typed_stream/version.py b/typed_stream/version.py index af2f208..1ff3fa7 100755 --- a/typed_stream/version.py +++ b/typed_stream/version.py @@ -5,7 +5,7 @@ To get the current version run this script. """ -VERSION = "0.0.2" +VERSION = "0.0.4" if __name__ == "__main__": print(VERSION)