Skip to content

Commit

Permalink
take_nth, first, second, nth, last, and rest. Faster isiterable.
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknw committed Apr 4, 2014
1 parent 62dd5bf commit 0bd2c0c
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 35 deletions.
6 changes: 0 additions & 6 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ dicttoolz:
itertoolz:
merge_sorted
interleave
take_nth
first
second
nth
last
rest
get
mapcat
sliding_window
Expand Down
9 changes: 5 additions & 4 deletions coolz/__init__.pxd
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from .itertoolz cimport (groupby, frequencies, reduceby, take, drop, concat,
concatv, isdistinct, interpose, unique, isiterable,
remove, iterate, accumulate, count, cons)
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)

from .functoolz cimport (memoize, c_memoize, curry, c_compose, c_thread_first,
c_thread_last, identity, c_pipe, complement, c_juxt,
do)

from .dicttoolz cimport (c_merge, c_merge_with, keymap, valmap, assoc,
keyfilter, valfilter)

8 changes: 5 additions & 3 deletions coolz/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .itertoolz import (groupby, frequencies, reduceby, take, drop, concat,
concatv, isdistinct, interpose, unique, isiterable,
remove, iterate, accumulate, count, cons)
from .itertoolz import (groupby, frequencies, reduceby,
first, second, nth, take, drop, rest, last,
concat, concatv, isdistinct, interpose, unique,
isiterable, remove, iterate, accumulate,
count, cons, take_nth)

from .functoolz import (memoize, curry, compose, thread_first,
thread_last, identity, pipe, complement, juxt, do)
Expand Down
5 changes: 3 additions & 2 deletions coolz/itertoolz/__init__.pxd
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .core cimport (groupby, remove, concat, concatv, frequencies, interpose,
take, drop, unique, reduceby, isiterable, isdistinct,
cons, iterate, accumulate, count)
first, second, nth, take, drop, rest, last, unique,
reduceby, isiterable, isdistinct, cons, iterate,
accumulate, count, take_nth)
5 changes: 3 additions & 2 deletions coolz/itertoolz/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .core import (groupby, remove, concat, concatv, frequencies, interpose,
take, drop, unique, reduceby, isiterable, isdistinct,
cons, iterate, accumulate, count)
first, second, nth, take, drop, rest, last, unique,
reduceby, isiterable, isdistinct, cons, iterate,
accumulate, count, take_nth)
18 changes: 18 additions & 0 deletions coolz/itertoolz/core.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ cpdef inline object take(int n, object seq)
cpdef object drop(int n, object seq)


cpdef inline object take_nth(int n, object seq)


cpdef object first(object seq)


cpdef object second(object seq)


cpdef object nth(int n, object seq)


cpdef object last(object seq)


cpdef object rest(object seq)


cpdef inline object cons(object el, object seq)


Expand Down
72 changes: 71 additions & 1 deletion coolz/itertoolz/core.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from cpython.dict cimport PyDict_Contains, PyDict_GetItem, PyDict_New, PyDict_SetItem
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

Expand Down Expand Up @@ -202,7 +203,8 @@ cpdef bint isiterable(object x):
iter(x)
return True
except TypeError:
return False
pass
return False


cpdef bint isdistinct(object seq):
Expand Down Expand Up @@ -259,6 +261,74 @@ cpdef object drop(int n, object seq):
return iter_seq


cpdef inline object take_nth(int n, object seq):
""" Every nth item in seq
>>> list(take_nth(2, [10, 20, 30, 40, 50]))
[10, 30, 50]
"""
return islice(seq, 0, None, n)


cpdef object first(object seq):
""" The first element in a sequence
>>> first('ABC')
'A'
"""
return next(iter(seq))


cpdef object second(object seq):
""" The second element in a sequence
>>> second('ABC')
'B'
"""
seq = iter(seq)
next(seq)
return next(seq)


cpdef object nth(int n, object seq):
""" The nth element in a sequence
>>> nth(1, 'ABC')
'B'
"""
if PySequence_Check(seq):
return seq[n]
seq = iter(seq)
while n > 0:
n -= 1
next(seq)
return next(seq)


cpdef object last(object seq):
""" The last element in a sequence
>>> last('ABC')
'C'
"""
cdef object val
if PySequence_Check(seq):
return seq[-1]
seq = iter(seq)
try:
while True:
val = next(seq)
except StopIteration:
pass
return val


cpdef object rest(object seq):
seq = iter(seq)
next(seq)
return seq


cpdef inline object cons(object el, object seq):
""" Add el to beginning of (possibly infinite) sequence seq.
Expand Down
23 changes: 6 additions & 17 deletions coolz/itertoolz/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from coolz.itertoolz.core import (remove, groupby,
concat, concatv, unique,
identity, isiterable,
isdistinct,
take, drop, interpose,
cons, frequencies,
isdistinct, first, second,
nth, take, drop, interpose,
rest, last, cons, frequencies,
reduceby, iterate, accumulate,
count)
count,
take_nth)

from toolz.compatibility import range, filter
from operator import add, mul
Expand Down Expand Up @@ -88,45 +89,35 @@ def test_isdistinct():
assert isdistinct(iter([1, 2, 1])) is False


'''
def test_nth():
assert nth(2, 'ABCDE') == 'C'
assert nth(2, iter('ABCDE')) == 'C'
assert nth(1, (3, 2, 1)) == 2
assert nth(0, {'foo': 'bar'}) == 'foo'
assert raises(StopIteration, lambda: nth(10, {10: 'foo'}))
'''


'''
def test_first():
assert first('ABCDE') == 'A'
assert first((3, 2, 1)) == 3
assert isinstance(first({0: 'zero', 1: 'one'}), int)
'''


'''
def test_second():
assert second('ABCDE') == 'B'
assert second((3, 2, 1)) == 2
assert isinstance(second({0: 'zero', 1: 'one'}), int)
'''


'''
def test_last():
assert last('ABCDE') == 'E'
assert last((3, 2, 1)) == 1
assert isinstance(last({0: 'zero', 1: 'one'}), int)
'''


'''
def test_rest():
assert list(rest('ABCDE')) == list('BCDE')
assert list(rest((3, 2, 1))) == list((2, 1))
'''


def test_take():
Expand All @@ -139,10 +130,8 @@ def test_drop():
assert list(drop(1, (3, 2, 1))) == list((2, 1))


'''
def test_take_nth():
assert list(take_nth(2, 'ABCDE')) == list('ACE')
'''


'''
Expand Down Expand Up @@ -193,7 +182,7 @@ def test_concatv():


def test_interpose():
# assert "a" == first(rest(interpose("a", range(1000000000))))
assert "a" == first(rest(interpose("a", range(1000000000))))
assert "tXaXrXzXaXn" == "".join(interpose("X", "tarzan"))
assert list(interpose(0, itertools.repeat(1, 4))) == [1, 0, 1, 0, 1, 0, 1]
assert list(interpose('.', ['a', 'b', 'c'])) == ['a', '.', 'b', '.', 'c']
Expand Down

0 comments on commit 0bd2c0c

Please sign in to comment.