/** * Yields a result for elements of the cross product of the underlying Iterables. * * @param f a function that maps an element of the cross product to a result * @param <R> type of the resulting {@code Iterator} elements * @return an {@code Iterator} of mapped results */ public <R> Iterator<R> yield(BiFunction<? super T1, ? super T2, ? extends R> f) { Objects.requireNonNull(f, "f is null"); return Iterator.ofAll(ts1).flatMap(t1 -> Iterator.ofAll(ts2).map(t2 -> f.apply(t1, t2))); }
static <T> Iterator<T> fillObject(int n, T element) { if (n <= 0) { return Iterator.empty(); } else { return Iterator.continually(element).take(n); } }
/** * Lazily creates a Stream in O(1) which traverses along the concatenation of the given iterables. * * @param iterables The iterable of iterables * @param <T> Component type. * @return A new {@code Stream} */ static <T> Stream<T> concat(Iterable<? extends Iterable<? extends T>> iterables) { return Iterator.<T> concat(iterables).toStream(); }
@Override public T getNext() { count--; return that.next(); } };
try { final Iterator<?> iter = iterator(); final Object o = iter.next(); if (o instanceof Integer || o instanceof Long || o instanceof Byte || o instanceof Short) { return ((Iterator<Number>) iter).foldLeft(((Number) o).longValue(), (sum, number) -> sum + number.longValue()); } else if (o instanceof BigInteger) { return ((Iterator<BigInteger>) iter).foldLeft(((BigInteger) o), BigInteger::add); } else if (o instanceof BigDecimal) { return ((Iterator<BigDecimal>) iter).foldLeft(((BigDecimal) o), BigDecimal::add); } else { return TraversableModule.neumaierSum(Iterator.of(o).concat(iter), t -> ((Number) t).doubleValue())[0];
@Override public Array<T> padTo(int length, T element) { final int actualLength = length(); if (length <= actualLength) { return this; } else { return appendAll(Iterator.continually(element).take(length - actualLength)); } }
/** * Creates a BitSet from int values. * * @param elements int values * @return A new BitSet of int values * @throws NullPointerException if elements is null */ static BitSet<Integer> ofAll(int... elements) { Objects.requireNonNull(elements, "elements is null"); return BitSet.ofAll(Iterator.ofAll(elements)); }
@Override default Tuple2<Iterator<T>, Iterator<T>> partition(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); final Iterator<T> first = that.iterator().filter(predicate); final Iterator<T> second = that.iterator().filter(predicate.negate()); return Tuple.of(first, second); } }
@SuppressWarnings("unchecked") static <T, S extends Seq<T>> Iterator<S> crossProduct(S empty, S seq, int power) { if (power < 0) { return Iterator.empty(); } else { return Iterator.range(0, power) .foldLeft(Iterator.of(empty), (product, ignored) -> product.flatMap(el -> seq.map(t -> (S) el.append(t)))); } }
/** * Chooses one of the given generators according to their frequency. * Only generators with positive frequencies ares used in returned * generator. * * @param generators A non-empty traversable of Tuples (frequency, generator) * @param <T> Type to be generated * @return A new T generator * @throws java.lang.NullPointerException if generators is null * @throws java.lang.IllegalArgumentException if generators doesn't contain any generator with positive frequency */ static <T> Gen<T> frequency(Iterable<Tuple2<Integer, Gen<T>>> generators) { Objects.requireNonNull(generators, "generators is null"); final Vector<Tuple2<Integer, Gen<T>>> filtered = Iterator.ofAll(generators) .filter(t -> t._1() > 0).toVector(); if (filtered.isEmpty()) { throw new IllegalArgumentException("no generator with positive weight"); } final int size = filtered.map(t -> t._1).sum().intValue(); return choose(1, size).flatMap(n -> GenModule.frequency(n, filtered.iterator())); }
/** * Joins the string representations of this elements using a specific delimiter, prefix and suffix. * <p> * Example: {@code List.of("a", "b", "c").mkString("Chars(", ", ", ")") = "Chars(a, b, c)"} * * @param prefix prefix of the resulting string * @param delimiter A delimiter string put between string representations of elements of this * @param suffix suffix of the resulting string * @return a new String */ default String mkString(CharSequence prefix, CharSequence delimiter, CharSequence suffix) { final StringBuilder builder = new StringBuilder(prefix); iterator().map(String::valueOf).intersperse(String.valueOf(delimiter)).forEach(builder::append); return builder.append(suffix).toString(); }
@Override default Iterator<T> reject(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); return filter(predicate.negate()); }
/** * A shortcut for {@code Iterator.ofAll(ts).flatMap(f)} which allows us to write real for-comprehensions using * {@code For(...).yield(...)}. * <p> * Example: * <pre><code> * For(getPersons(), person -> * For(person.getTweets(), tweet -> * For(tweet.getReplies()) * .yield(reply -> person + ", " + tweet + ", " + reply))); * </code></pre> * * @param ts An iterable * @param f A function {@code T -> Iterable<U>} * @param <T> element type of {@code ts} * @param <U> component type of the resulting {@code Iterator} * @return A new Iterator */ public static <T, U> Iterator<U> For(Iterable<T> ts, Function<? super T, ? extends Iterable<U>> f) { return Iterator.ofAll(ts).flatMap(f); }
/** * Intersperse values from this generator instance with those of another. * * @param other another T generator to accept values from. * @return A new T generator */ default Gen<T> intersperse(Gen<T> other) { final Iterator<Gen<T>> iter = Iterator.continually(this).intersperse(other); return random -> iter.get().apply(random); }
public <U> PriorityQueue<U> flatMap(Comparator<U> comparator, Function<? super T, ? extends Iterable<? extends U>> mapper) { Objects.requireNonNull(comparator, "comparator is null"); Objects.requireNonNull(mapper, "mapper is null"); return ofAll(comparator, iterator().flatMap(mapper)); }