diff options
author | Bryan Bishop <kanzure@gmail.com> | 2013-09-01 15:33:52 -0500 |
---|---|---|
committer | Bryan Bishop <kanzure@gmail.com> | 2013-09-01 15:33:52 -0500 |
commit | 74a9f14502a51f25f16be62135d28dc7adba6fbb (patch) | |
tree | 8011150e4b6e7a5cde5760ff83f996186be00fb9 /pokemontools/helpers.py | |
parent | adb89bb985b02f2072da9770072b54e8f1d38bba (diff) |
move index and grouper into helpers.py
Diffstat (limited to 'pokemontools/helpers.py')
-rw-r--r-- | pokemontools/helpers.py | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/pokemontools/helpers.py b/pokemontools/helpers.py new file mode 100644 index 0000000..e2f62e0 --- /dev/null +++ b/pokemontools/helpers.py @@ -0,0 +1,14 @@ +""" +Generic functions that should be reusable anywhere in pokemontools. +""" + +def index(seq, f): + """return the index of the first item in seq + where f(item) == True.""" + return next((i for i in xrange(len(seq)) if f(seq[i])), None) + +def grouper(some_list, count=2): + """splits a list into sublists + given: [1, 2, 3, 4] + returns: [[1, 2], [3, 4]]""" + return [some_list[i:i+count] for i in range(0, len(some_list), count)] |