iterator - Feeling stupid while trying to implement lazy partitioning in Python -


i trying implement lazy partitioning of iterator object yields slices of iterator when function on element of iterator changes values. mimick behavior of clojure's partition-by (although semantics of output different, since python genuinely "consume" elements). implementation optimal in number of operations performs not in memory requires. don't see why implementation need more o(1) memory, implementation takes o(k) memory, k size of partition. able handle cases k large. know of implementation?

the correct behavior should like

>>>unagi = [-1, 3, 4, 7, -2, 1, -3, -5] >>> parts = partitionby(lambda x: x < 0,unagi) >>> print [[y y in x] x in parts] [[-1], [3, 4, 7], [-2], [1], [-3, -5]] 

here current version

from itertools import *  def partitionby(f,iterable):     seq = iter(iterable)     current = next(seq)     justseen = next(seq)     partition = iter([current])     while true:         if f(current) == f(justseen):              partition = chain(partition,iter([justseen]))             try:                 justseen = next(seq)             except stopiteration:                 yield partition                 break         else:             yield partition             current = justseen             partition = iter([]) 

why not reuse groupby? think o(1).

def partitionby(f, iterable):     return (g[1] g in groupby(iterable, f)) 

the difference of groupby's implementation yours partition specialized iterator object, instead of chain of chain of chain ...


Comments