Created
May 8, 2014 18:23
-
-
Save SegFaultAX/3f2657c6c338691d812c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def categorize(fn, coll): | |
"""Like group_by, but fn returns multiple keys""" | |
acc = {} | |
for e in coll: | |
keys = fn(e) | |
for key in keys: | |
if key not in acc: | |
acc[key] = [] | |
acc[key].append(e) | |
return acc | |
def group_by(fn, l): | |
return categorize(lambda e: [fn(e)], l) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment