Skip to content

Commit

Permalink
add partition
Browse files Browse the repository at this point in the history
  • Loading branch information
mrocklin committed Apr 4, 2014
1 parent 0bd2c0c commit 4c29e14
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 11 deletions.
1 change: 0 additions & 1 deletion TODO
Expand Up @@ -8,7 +8,6 @@ itertoolz:
get
mapcat
sliding_window
partition
partition_all
pluck

Expand Down
2 changes: 1 addition & 1 deletion coolz/__init__.pxd
Expand Up @@ -2,7 +2,7 @@ from .itertoolz cimport (groupby, frequencies, reduceby,
first, second, nth, take, drop, rest, last,
concat, concatv, isdistinct, interpose, unique,
isiterable, remove, iterate, accumulate,
count, cons, take_nth)
count, cons, take_nth, partition)

from .functoolz cimport (memoize, c_memoize, curry, c_compose, c_thread_first,
c_thread_last, identity, c_pipe, complement, c_juxt,
Expand Down
2 changes: 1 addition & 1 deletion coolz/__init__.py
Expand Up @@ -2,7 +2,7 @@
first, second, nth, take, drop, rest, last,
concat, concatv, isdistinct, interpose, unique,
isiterable, remove, iterate, accumulate,
count, cons, take_nth)
count, cons, take_nth, partition)

from .functoolz import (memoize, curry, compose, thread_first,
thread_last, identity, pipe, complement, juxt, do)
Expand Down
2 changes: 1 addition & 1 deletion coolz/itertoolz/__init__.pxd
@@ -1,4 +1,4 @@
from .core cimport (groupby, remove, concat, concatv, frequencies, interpose,
first, second, nth, take, drop, rest, last, unique,
reduceby, isiterable, isdistinct, cons, iterate,
accumulate, count, take_nth)
accumulate, count, take_nth, partition)
2 changes: 1 addition & 1 deletion coolz/itertoolz/__init__.py
@@ -1,4 +1,4 @@
from .core import (groupby, remove, concat, concatv, frequencies, interpose,
first, second, nth, take, drop, rest, last, unique,
reduceby, isiterable, isdistinct, cons, iterate,
accumulate, count, take_nth)
accumulate, count, take_nth, partition)
3 changes: 3 additions & 0 deletions coolz/itertoolz/core.pxd
Expand Up @@ -79,3 +79,6 @@ cdef class iterate:


cpdef int count(object seq)


cpdef object partition(int n, object seq, object pad=*)
30 changes: 29 additions & 1 deletion coolz/itertoolz/core.pyx
Expand Up @@ -3,7 +3,7 @@ from cpython.list cimport PyList_New, PyList_Append
from cpython.ref cimport PyObject
from cpython.sequence cimport PySequence_Check
from cpython.set cimport PySet_Add, PySet_Contains
from itertools import chain, islice
from itertools import chain, islice, izip_longest


concatv = chain
Expand Down Expand Up @@ -516,3 +516,31 @@ cpdef int count(object seq):
for _ in seq:
i += 1
return i


no_pad = '__no__pad__'


cpdef object partition(int n, object seq, object pad=no_pad):
""" Partition sequence into tuples of length n
>>> list(partition(2, [1, 2, 3, 4]))
[(1, 2), (3, 4)]
If the length of ``seq`` is not evenly divisible by ``n``, the final tuple
is dropped if ``pad`` is not specified, or filled to length ``n`` by pad:
>>> list(partition(2, [1, 2, 3, 4, 5]))
[(1, 2), (3, 4)]
>>> list(partition(2, [1, 2, 3, 4, 5], pad=None))
[(1, 2), (3, 4), (5, None)]
See Also:
partition_all
"""
args = [iter(seq)] * n
if pad is no_pad:
return zip(*args)
else:
return izip_longest(*args, fillvalue=pad)
6 changes: 1 addition & 5 deletions coolz/itertoolz/tests/test_core.py
Expand Up @@ -8,7 +8,7 @@
nth, take, drop, interpose,
rest, last, cons, frequencies,
reduceby, iterate, accumulate,
count,
count, partition,
take_nth)

from toolz.compatibility import range, filter
Expand Down Expand Up @@ -236,16 +236,12 @@ def test_sliding_window_of_short_iterator():
assert list(sliding_window(3, [1, 2])) == []
'''


'''
def test_partition():
assert list(partition(2, [1, 2, 3, 4])) == [(1, 2), (3, 4)]
assert list(partition(3, range(7))) == [(0, 1, 2), (3, 4, 5)]
assert list(partition(3, range(4), pad=-1)) == [(0, 1, 2),
(3, -1, -1)]
assert list(partition(2, [])) == []
'''


'''
def test_partition_all():
Expand Down

0 comments on commit 4c29e14

Please sign in to comment.