Skip to content

Commit

Permalink
Add partition_all
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknw committed Apr 10, 2014
1 parent c0164e4 commit 1bd6248
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
itertoolz:
merge_sorted
partition_all
pluck

curried
4 changes: 2 additions & 2 deletions cytoolz/__init__.pxd
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from cytoolz.itertoolz cimport (
accumulate, cons, count, drop, get, groupby, first, frequencies,
interleave, interpose, isdistinct, isiterable, iterate, last, mapcat, nth,
partition, reduceby, remove, rest, second, sliding_window, take, take_nth,
unique)
partition, partition_all, reduceby, remove, rest, second, sliding_window,
take, take_nth, unique)


from cytoolz.functoolz cimport (
Expand Down
5 changes: 5 additions & 0 deletions cytoolz/itertoolz.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,9 @@ cdef class sliding_window:
cpdef object partition(int n, object seq, object pad=*)


cdef class partition_all:
cdef int n
cdef object iterseq


cpdef int count(object seq)
43 changes: 41 additions & 2 deletions cytoolz/itertoolz.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ from cpython.list cimport (PyList_Append, PyList_Check, PyList_GET_ITEM,
from cpython.ref cimport PyObject, Py_DECREF, Py_INCREF
from cpython.sequence cimport PySequence_Check
from cpython.set cimport PySet_Add, PySet_Contains
from cpython.tuple cimport PyTuple_New, PyTuple_SET_ITEM
from cpython.tuple cimport PyTuple_GetSlice, PyTuple_New, PyTuple_SET_ITEM

# Locally defined bindings that differ from `cython.cpython` bindings
from .cpython cimport PyIter_Next, PyObject_GetItem
Expand All @@ -20,7 +20,7 @@ __all__ = ['remove', 'accumulate', 'groupby', 'interleave',
'unique', 'isiterable', 'isdistinct', 'take', 'drop', 'take_nth',
'first', 'second', 'nth', 'last', 'get', 'concat', 'concatv',
'mapcat', 'cons', 'interpose', 'frequencies', 'reduceby', 'iterate',
'sliding_window', 'partition', 'count']
'sliding_window', 'partition', 'partition_all', 'count']
# 'sliding_window', 'partition', 'partition_all', 'count', 'pluck']


Expand Down Expand Up @@ -715,6 +715,45 @@ cpdef object partition(int n, object seq, object pad=no_pad):
return zip_longest(*args, fillvalue=pad)


cdef class partition_all:
""" Partition all elements of sequence into tuples of length at most n
The final tuple may be shorter to accommodate extra elements.
>>> list(partition_all(2, [1, 2, 3, 4]))
[(1, 2), (3, 4)]
>>> list(partition_all(2, [1, 2, 3, 4, 5]))
[(1, 2), (3, 4), (5,)]
See Also:
partition
"""
def __cinit__(self, int n, object seq):
self.n = n
self.iterseq = iter(seq)

def __iter__(self):
return self

def __next__(self):
cdef tuple result
cdef object item
cdef int i = 0
result = PyTuple_New(self.n)
for item in self.iterseq:
Py_INCREF(item)
PyTuple_SET_ITEM(result, i, item)
i += 1
if i == self.n:
break
if i == 0:
raise StopIteration
if i != self.n:
return PyTuple_GetSlice(result, 0, i)
return result


cpdef int count(object seq):
""" Count the number of items in seq
Expand Down
4 changes: 1 addition & 3 deletions cytoolz/tests/test_itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
rest, last, cons, frequencies,
reduceby, iterate, accumulate,
sliding_window, count, partition,
take_nth)
partition_all, take_nth)

from cytoolz.compatibility import range, filter
from operator import add, mul
Expand Down Expand Up @@ -237,12 +237,10 @@ def test_partition():
assert list(partition(2, [])) == []


'''
def test_partition_all():
assert list(partition_all(2, [1, 2, 3, 4])) == [(1, 2), (3, 4)]
assert list(partition_all(3, range(5))) == [(0, 1, 2), (3, 4)]
assert list(partition_all(2, [])) == []
'''


def test_count():
Expand Down

0 comments on commit 1bd6248

Please sign in to comment.