Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Itertoolz
partition
partition_all
peek
peekn
pluck
random_sample
reduceby
Expand Down
1 change: 1 addition & 0 deletions toolz/curried/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
partition = toolz.curry(toolz.partition)
partition_all = toolz.curry(toolz.partition_all)
partitionby = toolz.curry(toolz.partitionby)
peekn = toolz.curry(toolz.peekn)
pluck = toolz.curry(toolz.pluck)
random_sample = toolz.curry(toolz.random_sample)
reduce = toolz.curry(toolz.reduce)
Expand Down
22 changes: 20 additions & 2 deletions toolz/itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'first', 'second', 'nth', 'last', 'get', 'concat', 'concatv',
'mapcat', 'cons', 'interpose', 'frequencies', 'reduceby', 'iterate',
'sliding_window', 'partition', 'partition_all', 'count', 'pluck',
'join', 'tail', 'diff', 'topk', 'peek', 'random_sample')
'join', 'tail', 'diff', 'topk', 'peek', 'peekn', 'random_sample')


def remove(predicate, seq):
Expand Down Expand Up @@ -942,7 +942,25 @@ def peek(seq):
"""
iterator = iter(seq)
item = next(iterator)
return item, itertools.chain([item], iterator)
return item, itertools.chain((item,), iterator)


def peekn(n, seq):
""" Retrieve the next n elements of a sequence

Returns a tuple of the first n elements and an iterable equivalent
to the original, still having the elements retrieved.

>>> seq = [0, 1, 2, 3, 4]
>>> first_two, seq = peekn(2, seq)
>>> first_two
(0, 1)
>>> list(seq)
[0, 1, 2, 3, 4]
"""
iterator = iter(seq)
peeked = tuple(take(n, iterator))
return peeked, itertools.chain(iter(peeked), iterator)


def random_sample(prob, seq, random_state=None):
Expand Down
15 changes: 13 additions & 2 deletions toolz/tests/test_itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
reduceby, iterate, accumulate,
sliding_window, count, partition,
partition_all, take_nth, pluck, join,
diff, topk, peek, random_sample)
diff, topk, peek, peekn, random_sample)
from toolz.compatibility import range, filter
from operator import add, mul

Expand Down Expand Up @@ -496,12 +496,23 @@ def test_topk_is_stable():
def test_peek():
alist = ["Alice", "Bob", "Carol"]
element, blist = peek(alist)
element == alist[0]
assert element == alist[0]
assert list(blist) == alist

assert raises(StopIteration, lambda: peek([]))


def test_peekn():
alist = ("Alice", "Bob", "Carol")
elements, blist = peekn(2, alist)
assert elements == alist[:2]
assert tuple(blist) == alist

elements, blist = peekn(len(alist) * 4, alist)
assert elements == alist
assert tuple(blist) == alist


def test_random_sample():
alist = list(range(100))

Expand Down