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)); }
private static <K, V, K2, V2> TreeMap<K2, V2> flatMap(TreeMap<K, V> map, EntryComparator<K2, V2> entryComparator, BiFunction<? super K, ? super V, ? extends Iterable<Tuple2<K2, V2>>> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return createTreeMap(entryComparator, map.entries.iterator().flatMap(entry -> mapper.apply(entry._1, entry._2))); }
/** * 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); }
@Override public <U> Vector<U> flatMap(Function<? super T, ? extends Iterable<? extends U>> mapper) { Objects.requireNonNull(mapper, "mapper is null"); final Iterator<? extends U> results = iterator().flatMap(mapper); return ofAll(results); }
@Override public <U> TreeSet<U> flatMap(Comparator<? super U> comparator, Function<? super T, ? extends Iterable<? extends U>> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return TreeSet.ofAll(comparator, iterator().flatMap(mapper)); }
@Override default <U> SortedSet<U> flatMap(Comparator<? super U> comparator, Function<? super T, ? extends Iterable<? extends U>> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return TreeSet.ofAll(comparator, iterator().flatMap(mapper)); }
/** * 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(Function6<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? extends R> f) { Objects.requireNonNull(f, "f is null"); return Iterator.ofAll(ts1).flatMap(t1 -> Iterator.ofAll(ts2).flatMap(t2 -> Iterator.ofAll(ts3).flatMap(t3 -> Iterator.ofAll(ts4).flatMap(t4 -> Iterator.ofAll(ts5).flatMap(t5 -> Iterator.ofAll(ts6).map(t6 -> f.apply(t1, t2, t3, t4, t5, t6))))))); }
/** * 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(Function8<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? super T8, ? extends R> f) { Objects.requireNonNull(f, "f is null"); return Iterator.ofAll(ts1).flatMap(t1 -> Iterator.ofAll(ts2).flatMap(t2 -> Iterator.ofAll(ts3).flatMap(t3 -> Iterator.ofAll(ts4).flatMap(t4 -> Iterator.ofAll(ts5).flatMap(t5 -> Iterator.ofAll(ts6).flatMap(t6 -> Iterator.ofAll(ts7).flatMap(t7 -> Iterator.ofAll(ts8).map(t8 -> f.apply(t1, t2, t3, t4, t5, t6, t7, t8))))))))); }
/** * 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(Function7<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? extends R> f) { Objects.requireNonNull(f, "f is null"); return Iterator.ofAll(ts1).flatMap(t1 -> Iterator.ofAll(ts2).flatMap(t2 -> Iterator.ofAll(ts3).flatMap(t3 -> Iterator.ofAll(ts4).flatMap(t4 -> Iterator.ofAll(ts5).flatMap(t5 -> Iterator.ofAll(ts6).flatMap(t6 -> Iterator.ofAll(ts7).map(t7 -> f.apply(t1, t2, t3, t4, t5, t6, t7)))))))); }
/** * Flat-maps this entries to a sequence of values. * <p> * Please use {@link #flatMap(BiFunction)} if the result should be a {@code Map} * * @param mapper A mapper * @param <U> Component type * @return A sequence of flat-mapped values. */ @SuppressWarnings("unchecked") @Override default <U> Seq<U> flatMap(Function<? super Tuple2<K, V>, ? extends Iterable<? extends U>> mapper) { Objects.requireNonNull(mapper, "mapper is null"); // don't remove cast, doesn't compile in Eclipse without it return (Seq<U>) iterator().flatMap(mapper).toStream(); }
/** * Flat-maps this entries to a sequence of values. * <p> * Please use {@link #flatMap(BiFunction)} if the result should be a {@code Multimap} * * @param mapper A mapper * @param <U> Component type * @return A sequence of flat-mapped values. */ @SuppressWarnings("unchecked") @Override default <U> Seq<U> flatMap(Function<? super Tuple2<K, V>, ? extends Iterable<? extends U>> mapper) { Objects.requireNonNull(mapper, "mapper is null"); // don't remove cast, doesn't compile in Eclipse without it return (Seq<U>) iterator().flatMap(mapper).toStream(); }
/** * 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(Function5<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? extends R> f) { Objects.requireNonNull(f, "f is null"); return Iterator.ofAll(ts1).flatMap(t1 -> Iterator.ofAll(ts2).flatMap(t2 -> Iterator.ofAll(ts3).flatMap(t3 -> Iterator.ofAll(ts4).flatMap(t4 -> Iterator.ofAll(ts5).map(t5 -> f.apply(t1, t2, t3, t4, t5)))))); }
/** * 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))); }
/** * 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(Function4<? super T1, ? super T2, ? super T3, ? super T4, ? extends R> f) { Objects.requireNonNull(f, "f is null"); return Iterator.ofAll(ts1).flatMap(t1 -> Iterator.ofAll(ts2).flatMap(t2 -> Iterator.ofAll(ts3).flatMap(t3 -> Iterator.ofAll(ts4).map(t4 -> f.apply(t1, t2, t3, t4))))); }
/** * 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(Function3<? super T1, ? super T2, ? super T3, ? extends R> f) { Objects.requireNonNull(f, "f is null"); return Iterator.ofAll(ts1).flatMap(t1 -> Iterator.ofAll(ts2).flatMap(t2 -> Iterator.ofAll(ts3).map(t3 -> f.apply(t1, t2, t3)))); }
/** * Calculates the cross product {@code this x that}. * <p> * Example: * <pre> * <code> * // = List of Tuples (1, 'a'), (1, 'b'), (2, 'a'), (2, 'b'), (3, 'a'), (3, 'b') * List.of(1, 2, 3).crossProduct(List.of('a', 'b'); * </code> * </pre> * * @param that Another iterable * @param <U> Component type * @return a new Iterator containing the cross product {@code this x that} * @throws NullPointerException if that is null */ default <U> Iterator<Tuple2<T, U>> crossProduct(Iterable<? extends U> that) { Objects.requireNonNull(that, "that is null"); final Stream<U> other = Stream.ofAll(that); return Iterator.ofAll(this).flatMap(a -> other.map(b -> Tuple.of(a, b))); }
@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)))); } }
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)); }
@Override public <U> Vector<U> flatMap(Function<? super T, ? extends Iterable<? extends U>> mapper) { Objects.requireNonNull(mapper, "mapper is null"); final Iterator<? extends U> results = iterator().flatMap(mapper); return ofAll(results); }