Skip to content

Commit

Permalink
Merge branch 'master' of github.com:dgilland/pydash
Browse files Browse the repository at this point in the history
  • Loading branch information
dgilland committed Jul 26, 2014
2 parents 96f2cbf + dea460b commit dcda594
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
22 changes: 18 additions & 4 deletions pydash/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,32 @@ def for_each_right(*args, **kargs): # pragma: no cover
each_right = for_each_right


def group_by(*args, **kargs): # pragma: no cover
def group_by(collection, callback):
"""Creates an object composed of keys generated from the results of running
each element of a `collection` through the callback.
"""
raise NotImplementedError
ret = dict()
cb = _make_callback(callback)

for value in collection:
key = cb(value)
ret.setdefault(key, [])
ret[key].append(value)

return ret


def index_by(*args, **kargs): # pragma: no cover
def index_by(collection, callback):
"""Creates an object composed of keys generated from the results of running
each element of the collection through the given callback.
"""
raise NotImplementedError
ret = dict()
cb = _make_callback(callback)

for value in collection:
ret[cb(value)] = value

return ret


def invoke(*args, **kargs): # pragma: no cover
Expand Down
17 changes: 17 additions & 0 deletions tests/test_pydash.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,23 @@ def test_find_last(case, expected):
assert pyd.find_last(*case) == expected


@parametrize('case,expected', [
(([4.2, 6.1, 6.4],
lambda num, *args: int(math.floor(num))),
{4: [4.2], 6: [6.1, 6.4]}),
])
def test_group_by(case, expected):
assert pyd.group_by(*case) == expected


@parametrize('case,expected', [
(([{'dir': 'left', 'code': 97}, {'dir': 'right', 'code': 100}], 'dir'),
{'left': {'dir': 'left', 'code': 97}, 'right': {'dir': 'right', 'code': 100}}),
])
def test_index_by(case, expected):
assert pyd.index_by(*case) == expected


@parametrize('case', [
fixtures.data.sample,
])
Expand Down

0 comments on commit dcda594

Please sign in to comment.