This function passes a running window along the length of the given
        iterable.  By default, the return value is a tuple, but the cast
        parameter can be used to change the final result.
356751
def window(iterable, n=2, cast=tuple):
    
    it = iter(iterable)
    win = deque((next(it) for _ in repeat(None, n)), maxlen=n)
    if len(win) < n:
        raise ValueError('Window size was greater than iterable length')
    yield cast(win)
    append = win.append
    for e in it:
        append(e)
        yield cast(win)
429491
def extend(self, iterable):
        
        # perhaps: self[len(self):len(self)] = iterable
        self._values.extend(self.coerce_value(item) for item in iterable)
399190
def randiter(iterable):
    '''
'''

    iter_len = len(iterable)

    for index in xrandrange(iter_len):
        yield iterable[index]
160844
def _chunk(iterable, size):
    """"""
    # We're going to use some star magic to chunk the iterable. We create a
    # copy of the iterator size times, then pull a value from each to form a
    # chunk. The last chunk may have some trailing Nones if the length of the
    # iterable isn't a multiple of size, so we filter them out.

    args = (iter(iterable),) * size

    return (
        # pylint: disable=star-args
        itertools.takewhile(lambda x: x is not None, group)
        for group in itertools.zip_longest(*args)
    )
1212763
static MutableIntTuple wrap(MutableIntTuple tuple, IntTuple bounds)
    {
        if (tuple.getSize() != bounds.getSize())
        {
            throw new IllegalArgumentException(
                "A tuple with size " + tuple.getSize() + 
                " can not be wrapped at bounds of size " + bounds.getSize());
        }
        for (int i=0; i<tuple.getSize(); i++)
        {
            int coordinate = tuple.get(i);
            int wrappedCoordinate = wrap(coordinate, bounds.get(i));
            tuple.set(i, wrappedCoordinate);
        }
        return tuple;
    }
1340420
public static int sizeOf(Iterable<?> iterable)
    {
        if (iterable instanceof Collection)
        {
            return ((Collection<?>) iterable).size();
        }
        if (iterable instanceof RichIterable)
        {
            return ((RichIterable<?>) iterable).size();
        }
        return Iterate.count(iterable, Predicates.alwaysTrue());
    }
633809
@SafeVarargs
    @SuppressWarnings("unchecked")
    public static <T> Iterable<T> compositeIterable(Iterable<T>...iterables) {
        Require.nonNull(iterables, "iterables");
        final Iterator<T> it;
        if (iterables.length == 0) {
            it = Collections.emptyIterator();
        } else if (iterables.length == 1) {
            return iterables[0];
        } else {
            final Iterator<T>[] iterators = Arrays.stream(iterables)
                    .map(Iterable::iterator)
                    .toArray(size -> new Iterator[size]);
            it = new CompoundIterator<T>(iterators);
        }
        return iterableOf(it);
    }
394924
function race(iterable) {
    var C = this;
    var capability = newPromiseCapability(C);
    var reject = capability.reject;
    var result = perform(function () {
      forOf(iterable, false, function (promise) {
        C.resolve(promise).then(capability.resolve, reject);
      });
    });
    if (result.e) reject(result.v);
    return capability.promise;
  }
574050
def run(self, n_iter=-1, bg=False):
        
        if n_iter == -1:
            if not self.eval_at:
                raise ValueError('Set n_iter or define evaluate_at.')

            n_iter = self.eval_at[-1] + 1

        self._running.set()

        if bg:
            self._t = threading.Thread(target=lambda: self._run(n_iter))
            self._t.start()
        else:
            self._run(n_iter)
57520
def count(cls, iterable):
    

    iterable = iter(iterable)
    count = 0
    while True:
      try:
        next(iterable)
      except StopIteration:
        break
      count += 1
    return count
464024
def chunk_iter(iterable, chunk_size):
    ''' '''
    while True:
        chunk = []
        try:
            for _ in range(chunk_size): chunk.append(next(iterable))
            yield chunk
        except StopIteration:
            if chunk: yield chunk
            break
1277679
def from_iterable(cls, iterable: Iterable) -> 'List':
        """"""

        iterator = iter(iterable)

        def recurse() -> List:
            try:
                value = next(iterator)
            except StopIteration:

                return List.empty()
            return List.unit(value).append(recurse())
        return List.empty().append(recurse())
91387
public static <T, A, R> Function<Stream<Timed<T>>, Stream<R>> window(final int maxWindowSize, final Duration maxDuration, final LongSupplier startTime,
            final Collector<? super Timed<T>, A, R> collector) {
        return new Function<Stream<Timed<T>>, Stream<R>>() {
            @Override
            public Stream<R> apply(final Stream<Timed<T>> s) {
                final ObjIterator<R> iter = new ObjIteratorEx<R>() {
                    private long maxDurationInMillis;

                    private Supplier<A> supplier;
                    private BiConsumer<A, ? super Timed<T>> accumulator;
                    private Function<A, R> finisher;

                    private ObjIterator<Timed<T>> iter;
                    private Timed<T> next = null;

                    private long fromTime;
                    private long endTime;

                    private boolean initialized = false;

                    @Override
                    public boolean hasNext() {
                        if (initialized == false) {
                            init();
                        }

                        while ((next == null || next.timestamp() < endTime) && iter.hasNext()) {
                            next = iter.next();
                        }

                        return next != null && next.timestamp() >= endTime;
                    }

                    @Override
                    public R next() {
                        if (hasNext() == false) {
                            throw new NoSuchElementException();
                        }

                        fromTime = endTime;
                        endTime = fromTime + maxDurationInMillis;
                        int cnt = 0;
                        final A container = supplier.get();

                        if (next != null && next.timestamp() < endTime) {
                            accumulator.accept(container, next);
                            next = null;
                            cnt++;
                        }

                        if (next == null) {
                            while (cnt < maxWindowSize && iter.hasNext()) {
                                next = iter.next();

                                if (next.timestamp() < endTime) {
                                    accumulator.accept(container, next);
                                    next = null;
                                    cnt++;
                                } else {
                                    break;
                                }
                            }
                        }

                        endTime = N.min(endTime, next == null ? System.currentTimeMillis() : next.timestamp());

                        return finisher.apply(container);
                    }

                    private void init() {
                        if (initialized == false) {
                            initialized = true;

                            N.checkArgNotNull(maxDuration, "maxDuration");
                            N.checkArgPositive(maxDuration.toMillis(), "maxDuration");
                            N.checkArgPositive(maxWindowSize, "maxWindowSize");
                            N.checkArgNotNull(startTime, "startTime");
                            N.checkArgNotNull(collector, "collector");

                            supplier = collector.supplier();
                            accumulator = collector.accumulator();
                            finisher = collector.finisher();

                            iter = s.iterator();

                            maxDurationInMillis = maxDuration.toMillis();

                            fromTime = startTime.getAsLong() - maxDurationInMillis;
                            endTime = fromTime + maxDurationInMillis;
                        }
                    }
                };

                return Stream.of(iter).onClose(new Runnable() {
                    @Override
                    public void run() {
                        s.close();
                    }
                });
            }
        };
    }
768088
public static <E> long count(Iterable<E> iterable) {
        dbc.precondition(iterable != null, "cannot call count with a null iterable");
        return count(iterable.iterator());
    }
523509
def butlast(iterable):
    
    iterable = iter(iterable)
    try:
        first = next(iterable)
    except StopIteration:
        return
    for second in iterable:
        yield first
        first = second
1037248
def _truncate(value, arg):
        
        length = int(arg)
        if value is None:
            value = ""
        if len(value) > length:
            return value[:length] + "..."
        else:
            return value
732426
def lim(self, lim):
        
        if lim is None:
            self._lim = lim
            return

        typeerror_msg = "lim must be of type tuple, float, None, or in ['fixed', 'symmetric', 'frame', 'sliding']"

        if isinstance(lim, str):
            if lim in ['fixed', 'symmetric', 'frame', 'sliding']:
                self._lim = lim
                return
            else:
                raise ValueError(typeerror_msg)

        if isinstance(lim, int):
            lim = float(lim)

        if isinstance(lim, float):
            if lim <= 0.0:
                raise ValueError("lim cannot be <= 0")
            self._lim = lim
            return

        if not isinstance(lim, tuple):
            try:
                lim = tuple(lim)
            except:
                raise TypeError(typeerror_msg)

        if not len(lim)==2:
            raise ValueError('lim must have length 2')

        for l in lim:
            if not (isinstance(l, float) or isinstance(l, int) or l is None):
                raise ValueError("each item in limit must be of type float, int, or None")

        self._lim = lim