dgilland / pydash

The kitchen sink of Python utility libraries for doing "stuff" in a functional way. Based on the Lo-Dash Javascript library.
http://pydash.readthedocs.io
MIT License
1.28k stars 89 forks source link

Take while does not support generators #204

Open Drvanon opened 11 months ago

Drvanon commented 11 months ago

If I add a generator as the object of my chain and I use the take_while function, it tries to call the nth index of the generator which is not supported.

A suggesteion for this would be

def take_while(array, predicate=None):
    array = list(array)
    n = 0
    for is_true, _, _, _ in iteriteratee(array, predicate):
         if is_true:
             n += 1
         else:
             break
      return array[:n]

or

def take_while(array, predicate=None):
    new_array = []
    while True:
         is_true, n, _, _ in iteriteratee(array, predicate):
         if not is_true:
             break  
         new_array.append(array[n])
    return new_array