Skip to content

Commit dec8706

Browse files
committed
add itertools.dropwhile and itertools.filterfalse
1 parent 44272dc commit dec8706

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

graalpython/lib-graalpython/itertools.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,52 @@ def __next__(self):
369369
else:
370370
self.total = self.func(total, value)
371371
return self.total
372+
373+
374+
class dropwhile(object):
375+
"""
376+
dropwhile(predicate, iterable) --> dropwhile object
377+
378+
Drop items from the iterable while predicate(item) is true.
379+
Afterwards, return every element until the iterable is exhausted.
380+
"""
381+
382+
def __init__(self, predicate, iterable):
383+
self.predicate = predicate
384+
self.iterable = iter(iterable)
385+
self.done_dropping = False
386+
387+
def __iter__(self):
388+
return self
389+
390+
def __next__(self):
391+
while not self.done_dropping:
392+
n = next(self.iterable)
393+
if self.predicate(n):
394+
continue
395+
else:
396+
self.done_dropping = True
397+
return n
398+
return next(self.iterable)
399+
400+
401+
class filterfalse(object):
402+
"""
403+
filterfalse(function or None, sequence) --> filterfalse object
404+
405+
Return those items of sequence for which function(item) is false.
406+
If function is None, return the items that are false.
407+
"""
408+
409+
def __init__(self, func, sequence):
410+
self.func = func or (lambda x: False)
411+
self.iterator = iter(sequence)
412+
413+
def __iter__(self):
414+
return self
415+
416+
def __next__(self):
417+
while True:
418+
n = next(self.iterator)
419+
if not self.func(n):
420+
return n

0 commit comments

Comments
 (0)