Skip to content

Commit

Permalink
v0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshix-1 committed Feb 8, 2023
1 parent f935512 commit 57c9949
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
7 changes: 7 additions & 0 deletions test/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The tests for the Stream."""
from operator import add
from pathlib import Path
from typing import TypeGuard

Expand Down Expand Up @@ -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,
Expand Down
25 changes: 21 additions & 4 deletions typed_stream/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]):
Expand Down
2 changes: 1 addition & 1 deletion typed_stream/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 57c9949

Please sign in to comment.