Skip to content

Commit

Permalink
misc.
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff committed Nov 22, 2023
1 parent 46b6f73 commit 0b195f8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 143 deletions.
42 changes: 19 additions & 23 deletions functionalpy/_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,53 @@
from functools import reduce
from typing import Generic, TypeVar

_T0 = TypeVar("_T0")
_T1 = TypeVar("_T1")
_S = TypeVar("_S")
_T = TypeVar("_T")


@dataclass(frozen=True)
class Group(Generic[_T0]):
class Group(Generic[_T]):
key: str
value: _T0
value: _T


class Seq(Generic[_T0]):
def __init__(self, iterable: Iterable[_T0]):
class Seq(Generic[_T]):
def __init__(self, iterable: Iterable[_T]):
self._seq = iterable

### Reductions
def count(self) -> int:
return sum(1 for _ in self._seq)

### Output
def to_list(self) -> list[_T0]:
def to_list(self) -> list[_T]:
return list(self._seq)

def to_tuple(self) -> tuple[_T0, ...]:
def to_tuple(self) -> tuple[_T, ...]:
return tuple(self._seq)

def to_iter(self) -> Iterator[_T0]:
def to_iter(self) -> Iterator[_T]:
return iter(self._seq)

def to_set(self) -> set[_T0]:
def to_set(self) -> set[_T]:
return set(self._seq)

### Transformations
def map( # noqa: A003 # Ignore that it's shadowing a python built-in
self,
func: Callable[[_T0], _T1],
) -> "Seq[_T1]":
func: Callable[[_T], _S],
) -> "Seq[_S]":
return Seq(map(func, self._seq))

def filter(self, func: Callable[[_T0], bool]) -> "Seq[_T0]": # noqa: A003
def filter(self, func: Callable[[_T], bool]) -> "Seq[_T]": # noqa: A003
return Seq(filter(func, self._seq))

def reduce(self, func: Callable[[_T0, _T0], _T0]) -> _T0:
def reduce(self, func: Callable[[_T, _T], _T]) -> _T:
return reduce(func, self._seq)

def groupby(
self, func: Callable[[_T0], str]
) -> "Seq[Group[tuple[_T0, ...]]]":
self, func: Callable[[_T], str]
) -> "Seq[dict[str, tuple[_T, ...]]]":
# Itertools.groupby requires the input to be sorted
sorted_input = sorted(self._seq, key=func)

Expand All @@ -60,15 +60,11 @@ def groupby(
sorted_input, key=func
)
}
items = [{k: v} for k, v in result.items()]

groups = (
Group(key=key, value=value)
for key, value in result.items()
)

return Seq(groups)
return Seq(items)

def flatten(self) -> "Seq[_T0]":
def flatten(self) -> "Seq[_T]":
return Seq(
item
for sublist in self._seq
Expand Down
116 changes: 0 additions & 116 deletions functionalpy/benchmark/query_1/iterators_q1.py

This file was deleted.

16 changes: 12 additions & 4 deletions functionalpy/test_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,27 @@ class GroupbyInput:
value: int


# TODO: Properly specify groupby
# Some desirable properties:
# - Direct indexing by group key
# - Still a sequence we can use .map etc. on
def test_groupby():
groupby_inputs = [
GroupbyInput(key="a", value=1),
GroupbyInput(key="a", value=2),
GroupbyInput(key="b", value=3),
]
sequence = Seq(groupby_inputs)
result = sequence.groupby(lambda x: x.key)
result = sequence.groupby(lambda x: x.key).to_tuple()

assert len(result) == 2
assert list(result.keys()) == ["a", "b"]
assert result["a"] == (groupby_inputs[0], groupby_inputs[1])
assert result["b"] == (groupby_inputs[2],)
assert [x.key for x in result] == ["a", "b"]
assert [x.value.to_list() for x in result if x.key == "a"] == [
(1, 2)
]
assert [x.value.to_list() for x in result if x.key == "b"] == [
(3,)
]


class TestFlattenTypes:
Expand Down

0 comments on commit 0b195f8

Please sign in to comment.