/** * @param key the function defining what duplicates are * @param <K> any type * @return an iterator skipping consecutive duplicates * Elements x, y are duplicates * iff weakEquals(key.apply(x), (key.apply(y)) is true. * Of n consecutive equal elements, the first of is retained. */ public <K> FluentIterator<T> skipDuplicates(Function<T, K> key) { return fluent(Iterators.skipDuplicates(this, key)); }
/** * @param key the function defining what duplicates are * @return an iterator skipping consecutive duplicates * Elements x, y are duplicates * iff weakEquals(key.apply(x), (key.apply(y)) is true. * Of n consecutive equal elements, the first one is retained. */ public <K> FluentIterator<T> keepFirst(Function<? super T, ? extends K> key) { return fluent(new SkipDuplicates<>(this, key, true)); }
/** * @param delta the value added at each step * @param start the start value * @return the arithmetic series starting at start value * Example: start = 7, delta = 5 produces 7, 12, 17, 22, .. */ public static InfiniteIterator<Double> arithmeticSeries(double start, double delta) { return unaryGenerator(start, increment(delta)); }
/** * @param s an iterator * @param t another iterator which must contain at least of non zero element * @return the polynomial quotient of s and t. Thus * multiply(s, divide(t, s)) is t */ public static ImmutableIterator<Double> divide(Iterator<Double> s, Iterator<Double> t) { return new Product(s, inverse(t)); }
/** * @param factor the factor multiplied with at each step * @param start the start value * @return the geometric series */ public static InfiniteIterator<Double> geometricSeries(double start, double factor) { return unaryGenerator(start, times(factor)); }
/** * This function groups an iterator by a key function: * consecutive elements with the same key value form a group. * The result is an Iterator of Lists of objects * Example: 4 7 10 5 8 11 3, key(x) = x%3 produces ((4 7 10) (5 8 11) (3)) * * @param key the key function controlling the grouping * @param <K> any type * @return the grouped iterator */ public <K> FluentIterator<List<T>> groupBy(Function<T, K> key) { return fluent(Iterators.groupBy(this, key)); }
/** * an iterator yielding the Fibonacci series * * @return the fibonacci series as InfiniteIterator */ static InfiniteIterator<Integer> fibonacci() { return binaryGenerator(1, 1, (a, b) -> a + b); }
/** * @param <T> any type * @param element any element, even null * @return an empty iterator */ public static <T> ImmutableIterator<T> of(T element) { return new Repeat<>(element, 1); }
/** * @param s an iterator to be compared to this * @param n the number of elements to be compared * @return true iff the first n elements of s and this are weakEqual. */ public boolean weakEquals(Iterator<T> s, int n) { return Iterators.weakEquals(this, s, n); }
/** * @param key the function defining what duplicates are * @return an iterator skipping consecutive duplicates * Elements x, y are duplicates * iff weakEquals(key.apply(x), (key.apply(y)) is true. * Of n consecutive equal elements, the last one is retained. */ public <K> FluentIterator<T> keepLast(Function<? super T, ? extends K> key) { return fluent(new SkipDuplicates<>(this, key, false)); }
/** * @param t the second iterator to be mapped, may contain nulls * @param f the function to be applied to each pair of elements from this and t * @return the iterator of mapped elements */ public FluentIterator<T> map(Iterator<T> t, BinaryOperator<T> f) { return fluent(Iterators.map(this, t, f)); }
/** * @return an iterator skipping consecutive duplicates * thus: 1 1 2 2 3 3 3 4 produces 1 2 3 4 * but: 1 2 3 4 1 2 3 4 produces 1 2 3 4 1 2 3 4 * Of n consecutive equal elements, the first of is retained. * Warning: If the input iterator contains indefinitely many * consecutive equal elements, next() will loop indefinitely */ public FluentIterator<T> skipDuplicates() { return fluent(Iterators.skipDuplicates(this, x -> x)); }
/** * This function groups an iterator. * consecutive elements which are equal form a group. * The result is an Iterator of List of objects. * Example: x x x y y z produces ((x x x) (y y) (z)) * * @return the grouped iterator */ public FluentIterator<List<T>> groupBy() { return fluent(Iterators.groupBy(this, x -> x)); }