Skip to content

Commit

Permalink
add group by maps
Browse files Browse the repository at this point in the history
  • Loading branch information
thejaminator committed Dec 11, 2023
1 parent 351b704 commit 074c647
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool]
[tool.poetry]
name = "slist"
version = "0.2.9"
version = "0.2.10"
homepage = "https://github.com/thejaminator/slist"
description = "A typesafe list with more method chaining!"
authors = ["James Chua <chuajamessh@gmail.com>"]
Expand Down
13 changes: 13 additions & 0 deletions slist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ def __init__(self, name: str, age: int):
d[k] = Slist([elem])
return Slist(Group(key=key, values=value) for key, value in d.items())

def map_on_group_values(self: Slist[Group[A, Sequence[B]]], func: Callable[[Sequence[B]], C]) -> Slist[Group[A, C]]:
# Apply a function on the group's vsalues
return self.map(lambda group: group.map_values(func))


def map_on_group_values_list(
self: Slist[Group[A, Sequence[B]]], func: Callable[[B], C]
) -> Slist[Group[A, Sequence[C]]]:
# If your group's values are a list, and you want to apply a function on each element of the list
# e.g. Slist([Group(1, [1, 2, 3]), Group(2, [4, 5, 6])]).map_on_group_values_list(lambda x: x + 1)
# Slist([Group(1, [2, 3, 4]), Group(2, [5, 6, 7])])
return self.map(lambda group: group.map_values(lambda values: Slist(values).map(func)))

def to_dict(self: Sequence[Tuple[CanHash, B]]) -> typing.Dict[CanHash, B]:
"""
Transforms a Slist of key value pairs to a dictionary
Expand Down

0 comments on commit 074c647

Please sign in to comment.