/** * Returns an infinitely long Stream of {@code long} values starting from {@code from}. * <p> * The {@code Stream} extends to {@code Integer.MIN_VALUE} when passing {@code Long.MAX_VALUE}. * * @param value a start long value * @return a new Stream of long values starting from {@code from} */ static Stream<Long> from(long value) { return Stream.ofAll(Iterator.from(value)); }
/** * Creates a Stream from byte values. * * @param elements byte values * @return A new Stream of Byte values * @throws NullPointerException if elements is null */ static Stream<Byte> ofAll(byte... elements) { Objects.requireNonNull(elements, "elements is null"); return Stream.ofAll(Iterator.ofAll(elements)); }
/** * Returns a Stream containing {@code n} times the given {@code element} * * @param <T> Component type of the Stream * @param n The number of elements in the Stream * @param element The element * @return A Stream of size {@code n}, where each element is the given {@code element}. */ static <T> Stream<T> fill(int n, T element) { return Stream.ofAll(io.vavr.collection.Collections.fillObject(n, element)); }
/** * Creates a Stream from float values. * * @param elements float values * @return A new Stream of Float values * @throws NullPointerException if elements is null */ static Stream<Float> ofAll(float... elements) { Objects.requireNonNull(elements, "elements is null"); return Stream.ofAll(Iterator.ofAll(elements)); }
/** * Creates a Stream from int values. * * @param elements int values * @return A new Stream of Integer values * @throws NullPointerException if elements is null */ static Stream<Integer> ofAll(int... elements) { Objects.requireNonNull(elements, "elements is null"); return Stream.ofAll(Iterator.ofAll(elements)); }
/** * Creates a Stream from short values. * * @param elements short values * @return A new Stream of Short values * @throws NullPointerException if elements is null */ static Stream<Short> ofAll(short... elements) { Objects.requireNonNull(elements, "elements is null"); return Stream.ofAll(Iterator.ofAll(elements)); }
/** * Repeats an element infinitely often. * * @param t An element * @param <T> Element type * @return A new Stream containing infinite {@code t}'s. */ static <T> Stream<T> continually(T t) { return Stream.ofAll(Iterator.continually(t)); }
/** * Creates a Stream from char values. * * @param elements char values * @return A new Stream of Character values * @throws NullPointerException if elements is null */ static Stream<Character> ofAll(char... elements) { Objects.requireNonNull(elements, "elements is null"); return Stream.ofAll(Iterator.ofAll(elements)); }
@SuppressWarnings("unchecked") static <T, K, V, M extends Map<K, V>> M ofStream(M map, java.util.stream.Stream<? extends T> stream, Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) { Objects.requireNonNull(stream, "stream is null"); Objects.requireNonNull(keyMapper, "keyMapper is null"); Objects.requireNonNull(valueMapper, "valueMapper is null"); return Stream.ofAll(stream).foldLeft(map, (m, el) -> (M) m.put(keyMapper.apply(el), valueMapper.apply(el))); }
@SuppressWarnings("unchecked") static <K, V, M extends Multimap<K, V>> M ofJavaMap(M source, java.util.Map<? extends K, ? extends V> map) { Objects.requireNonNull(map, "map is null"); return Stream.ofAll(map.entrySet()).foldLeft(source, (m, el) -> (M) m.put(el.getKey(), el.getValue())); }
@SuppressWarnings("unchecked") static <T, K, V, M extends Map<K, V>> M ofStream(M map, java.util.stream.Stream<? extends T> stream, Function<? super T, Tuple2<? extends K, ? extends V>> entryMapper) { Objects.requireNonNull(stream, "stream is null"); Objects.requireNonNull(entryMapper, "entryMapper is null"); return Stream.ofAll(stream).foldLeft(map, (m, el) -> (M) m.put(entryMapper.apply(el))); }
@Override default <U, R> Seq<R> zipWith(Iterable<? extends U> that, BiFunction<? super Tuple2<K, V>, ? super U, ? extends R> mapper) { Objects.requireNonNull(that, "that is null"); Objects.requireNonNull(mapper, "mapper is null"); return Stream.ofAll(iterator().zipWith(that, mapper)); }
@Override default <U, R> Seq<R> zipWith(Iterable<? extends U> that, BiFunction<? super Tuple2<K, V>, ? super U, ? extends R> mapper) { Objects.requireNonNull(that, "that is null"); Objects.requireNonNull(mapper, "mapper is null"); return Stream.ofAll(iterator().zipWith(that, mapper)); }
/** * Turns a sequence of {@code Tuple1} into a Tuple1 of {@code Seq}. * * @param <T1> 1st component type * @param tuples an {@code Iterable} of tuples * @return a tuple of one {@link Seq}. */ static <T1> Tuple1<Seq<T1>> sequence1(Iterable<? extends Tuple1<? extends T1>> tuples) { Objects.requireNonNull(tuples, "tuples is null"); final Stream<Tuple1<? extends T1>> s = Stream.ofAll(tuples); return new Tuple1<>(s.map(Tuple1::_1)); }
/** * Extends (continues) this {@code Stream} with values provided by a {@code Supplier} * * @param nextSupplier a supplier which will provide values for extending a stream * @return new {@code Stream} composed from this stream extended with values provided by the supplier */ default Stream<T> extend(Supplier<? extends T> nextSupplier) { Objects.requireNonNull(nextSupplier, "nextSupplier is null"); return Stream.ofAll(appendAll(Stream.continually(nextSupplier))); }
@Override default <U> Seq<Tuple2<Tuple2<K, V>, U>> zipAll(Iterable<? extends U> that, Tuple2<K, V> thisElem, U thatElem) { Objects.requireNonNull(that, "that is null"); return Stream.ofAll(iterator().zipAll(that, thisElem, thatElem)); }
@Override default <U> Seq<U> zipWithIndex(BiFunction<? super Tuple2<K, V>, ? super Integer, ? extends U> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return Stream.ofAll(iterator().zipWithIndex(mapper)); }
@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); } }
@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()); } }