Skip to content

Commit

Permalink
Bug fix: Fix implementation of filter_keys and filter_values
Browse files Browse the repository at this point in the history
These two functions do not align at all with their Kotlin counterparts, so update the implementation accordingly.
  • Loading branch information
AvlWx2014 committed Sep 30, 2024
1 parent 8a0a638 commit 5146395
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
40 changes: 11 additions & 29 deletions src/collektions/_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,28 @@
"map_values_to",
]

from collections.abc import Iterable, Mapping, MutableMapping, MutableSequence
from collections.abc import Mapping, MutableMapping
from typing import Callable

from ._types import K, R, V


def filter_keys(
mapping: Mapping[K, V], predicate: Callable[[K, V], bool]
) -> Iterable[K]:
"""Filter ``mapping``'s key set based on ``predicate``.
The predicate function takes both the key and value from each key/value pair
so that filtering can be done on either.
Returns:
A collection of the key from each key/value pair for which predicate returns ``True``.
mapping: Mapping[K, V], predicate: Callable[[K], bool]
) -> Mapping[K, V]:
"""
result: MutableSequence[K] = []
for key, value in mapping.items():
if predicate(key, value):
result.append(key)
return result
Return a new mapping of all key/value pairs from ``mapping`` where `key` satisfies ``predicate``.
"""
return {k: v for k, v in mapping.items() if predicate(k)}


def filter_values(
mapping: Mapping[K, V], predicate: Callable[[K, V], bool]
) -> Iterable[V]:
"""Filter ``mapping``'s value set based on ``predicate``.
The predicate function takes both the key and value from each key/value pair
so that filtering can be done on either.
Returns:
A collection of the value from each key/value pair for which predicate returns ``True``.
mapping: Mapping[K, V], predicate: Callable[[V], bool]
) -> Mapping[K, V]:
"""
result: MutableSequence[V] = []
for key, value in mapping.items():
if predicate(key, value):
result.append(value)
return result
Return a new mapping of all key/value pairs from ``mapping`` where `value` satisfies ``predicate``.
"""
return {k: v for k, v in mapping.items() if predicate(v)}


def map_keys(mapping: Mapping[K, V], transform: Callable[[K, V], R]) -> Mapping[R, V]:
Expand Down
8 changes: 4 additions & 4 deletions tests/test__mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

def test_filter_keys():
mapping = {i: str(i) for i in range(10)}
expected = [0, 2, 4, 6, 8]
actual = filter_keys(mapping, lambda k, _: k % 2 == 0)
expected = {0: "0", 2: "2", 4: "4", 6: "6", 8: "8"}
actual = filter_keys(mapping, lambda k: k % 2 == 0)
assert_that(actual, equal_to(expected))


def test_filter_values():
mapping = {i: str(i) for i in range(10)}
expected = ["0", "2", "4", "6", "8"]
actual = filter_values(mapping, lambda _, v: int(v) % 2 == 0)
expected = {0: "0", 2: "2", 4: "4", 6: "6", 8: "8"}
actual = filter_values(mapping, lambda v: int(v) % 2 == 0)
assert_that(actual, equal_to(expected))


Expand Down

0 comments on commit 5146395

Please sign in to comment.