Skip to content

Commit

Permalink
feature: add type hints for recipes (pytoolz#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiehong committed Oct 8, 2020
1 parent e9bc1e1 commit 2d91d85
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ dist/
*.egg-info/
bench/shakespeare.txt
.coverage
.idea

\.tox/
11 changes: 8 additions & 3 deletions toolz/recipes.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import itertools
from .itertoolz import frequencies, pluck, getter
from typing import TypeVar, Iterable, Dict, Tuple, Callable

from .itertoolz import frequencies, pluck, getter

__all__ = ('countby', 'partitionby')

A = TypeVar('A')
B = TypeVar('B')


def countby(key, seq):
def countby(key: Callable[[A], B], seq: Iterable[A]) -> Dict[B, int]:
""" Count elements of a collection by a key function
>>> countby(len, ['cat', 'mouse', 'dog'])
Expand All @@ -23,7 +27,8 @@ def countby(key, seq):
return frequencies(map(key, seq))


def partitionby(func, seq):
def partitionby(func: Callable[[A], bool],
seq: Iterable[A]) -> Iterable[Tuple[A, ...]]:
""" Partition a sequence according to a function
Partition `s` into a sequence of lists such that, when traversing
Expand Down

0 comments on commit 2d91d85

Please sign in to comment.