@Override public boolean hasNext() { return that.hasNext(); }
@Override public boolean hasNext() { return that.hasNext(); }
@Override public boolean hasNext() { return that.hasNext(); }
@Override default T head() { if (!hasNext()) { throw new NoSuchElementException("head() on empty iterator"); } return next(); }
@Override default Iterator<T> tail() { if (!hasNext()) { throw new UnsupportedOperationException(); } else { next(); // remove first element return this; } }
@Override default Iterator<T> init() { if (!hasNext()) { throw new UnsupportedOperationException(); } else { return dropRight(1); } }
private static void drop(Iterator<?> source, int count) { for (int i = 0; i < count && source.hasNext(); i++) { source.next(); } }
@Override public <U, R> IndexedSeq<R> zipWith(Iterable<? extends U> that, BiFunction<? super Character, ? super U, ? extends R> mapper) { Objects.requireNonNull(that, "that is null"); Objects.requireNonNull(mapper, "mapper is null"); IndexedSeq<R> result = Vector.empty(); final io.vavr.collection.Iterator<Character> list1 = iterator(); final java.util.Iterator<? extends U> list2 = that.iterator(); while (list1.hasNext() && list2.hasNext()) { result = result.append(mapper.apply(list1.next(), list2.next())); } return result; }
default Iterator<T> concat(java.util.Iterator<? extends T> that) { Objects.requireNonNull(that, "that is null"); if (!that.hasNext()) { return this; } else if (!hasNext()) { return ofAll(that); } else { return concat(this, ofAll(that)); } }
@Override default Iterator<T> distinct() { if (!hasNext()) { return empty(); } else { return new DistinctIterator<>(this, io.vavr.collection.HashSet.empty(), Function.identity()); } }
@Override default <U> Iterator<T> distinctBy(Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor, "keyExtractor is null"); if (!hasNext()) { return empty(); } else { return new DistinctIterator<>(this, io.vavr.collection.HashSet.empty(), keyExtractor); } }
@Override public boolean hasNext() { while (next == null && that.hasNext()) { final T elem = that.next(); final U key = keyExtractor.apply(elem); if (!known.contains(key)) { known = known.add(key); next = elem; } } return next != null; }
@Override default Option<Iterator<T>> tailOption() { if (hasNext()) { next(); return Option.some(this); } else { return Option.none(); } }
static <T> T last(Traversable<T> source){ if (source.isEmpty()) { throw new NoSuchElementException("last of empty " + source.stringPrefix()); } else { final Iterator<T> it = source.iterator(); T result = null; while (it.hasNext()) { result = it.next(); } return result; } }
@Override public boolean hasNext() { while (that.hasNext()) { queue = queue.enqueue(that.next()); if (queue.length() > n) { queue = queue.dequeue()._2; } } return queue.length() > 0; }
@Override default Tuple2<Iterator<T>, Iterator<T>> span(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); if (!hasNext()) { return Tuple.of(empty(), empty()); } else { final Stream<T> that = Stream.ofAll(this); return Tuple.of(that.iterator().takeWhile(predicate), that.iterator().dropWhile(predicate)); } }
@Override default <T1, T2> Tuple2<Iterator<T1>, Iterator<T2>> unzip( Function<? super T, Tuple2<? extends T1, ? extends T2>> unzipper) { Objects.requireNonNull(unzipper, "unzipper is null"); if (!hasNext()) { return Tuple.of(empty(), empty()); } else { final Stream<Tuple2<? extends T1, ? extends T2>> source = Stream.ofAll(this.map(unzipper)); return Tuple.of(source.map(t -> (T1) t._1).iterator(), source.map(t -> (T2) t._2).iterator()); } }
@Override default <T1, T2, T3> Tuple3<Iterator<T1>, Iterator<T2>, Iterator<T3>> unzip3( Function<? super T, Tuple3<? extends T1, ? extends T2, ? extends T3>> unzipper) { Objects.requireNonNull(unzipper, "unzipper is null"); if (!hasNext()) { return Tuple.of(empty(), empty(), empty()); } else { final Stream<Tuple3<? extends T1, ? extends T2, ? extends T3>> source = Stream.ofAll(this.map(unzipper)); return Tuple.of(source.map(t -> (T1) t._1).iterator(), source.map(t -> (T2) t._2).iterator(), source.map(t -> (T3) t._3).iterator()); } }