/** * Turns a sequence of {@code Tuple5} into a Tuple5 of {@code Seq}s. * * @param <T1> 1st component type * @param <T2> 2nd component type * @param <T3> 3rd component type * @param <T4> 4th component type * @param <T5> 5th component type * @param tuples an {@code Iterable} of tuples * @return a tuple of 5 {@link Seq}s. */ static <T1, T2, T3, T4, T5> Tuple5<Seq<T1>, Seq<T2>, Seq<T3>, Seq<T4>, Seq<T5>> sequence5(Iterable<? extends Tuple5<? extends T1, ? extends T2, ? extends T3, ? extends T4, ? extends T5>> tuples) { Objects.requireNonNull(tuples, "tuples is null"); final Stream<Tuple5<? extends T1, ? extends T2, ? extends T3, ? extends T4, ? extends T5>> s = Stream.ofAll(tuples); return new Tuple5<>(s.map(Tuple5::_1), s.map(Tuple5::_2), s.map(Tuple5::_3), s.map(Tuple5::_4), s.map(Tuple5::_5)); }
@Override default Stream<Stream<T>> permutations() { if (isEmpty()) { return Empty.instance(); } else { final Stream<T> tail = tail(); if (tail.isEmpty()) { return Stream.of(this); } else { final Stream<Stream<T>> zero = Empty.instance(); return distinct().foldLeft(zero, (xs, x) -> { final Function<Stream<T>, Stream<T>> prepend = l -> l.prepend(x); return xs.appendAll(remove(x).permutations().map(prepend)); }); } } }
/** * 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))); }
@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))); }
final String ifaceName = declaration.annotation().value(); final Iterable<ClassName> baseScalars = Stream .of(declaration.annotation().has()) .flatMap( n -> Stream .ofAll(manifest.declarations(Scalar.class)) .filter(d -> d.annotation().value().equals(n)) .filter(d -> !d.annotation().value().equals(declaration.annotation().value())) .map(d -> ClassName.get(d.packageName(), d.annotation().value())); final Iterable<ClassName> baseStructures = Stream .ofAll(manifest.declarations(Structure.class)) .filter(d -> has.containsAll(HashSet.of(d.annotation().has()))) .filter(d -> !d.annotation().value().equals(declaration.annotation().value())) .map(d -> ClassName.get(d.packageName(), d.annotation().value())); return TypeSpec.interfaceBuilder(ifaceName) .addSuperinterfaces(baseScalars)
@Override @SuppressWarnings("unchecked") public BitSet<T> addAll(Iterable<? extends T> elements) { final Stream<Integer> source = Stream.ofAll(elements).map(toInt); if (source.isEmpty()) { return this; } else { final long[] copy = copyExpand(1 + (source.max().getOrElse(0) >> ADDRESS_BITS_PER_WORD)); source.forEach(element -> { if (element < 0) { throw new IllegalArgumentException("bitset element must be >= 0"); } setElement(copy, element); }); final BitSet<T> bitSet = fromBitMaskNoCopy(copy); return (bitSet.length() == length()) ? this : bitSet; } }
/** * Recursively builds a non-empty {@code Tree}, starting with the given {@code seed} value and proceeding in depth-first order. * <p> * The children of a node are created by * <ol> * <li>applying the {@code descend} function to the node value</li> * <li>calling this method recursively by using each derived child value as new seed (in iteration order).</li> * </ol> * <p> * Example: * <pre>{@code * // = (1 (2 4 5) 3) * Tree.recurse(1, i -> * (i == 1) ? List.of(2, 3) : * (i == 2) ? List.(4, 5) : * List.empty() * ).toLispString(); * }</pre> * * @param seed The start value for the Tree * @param descend A function to calculate the child values * @param <T> Value type * @return a new, non-empty {@code Tree} instance * @throws NullPointerException if {@code descend} is null */ static <T> Node<T> recurse(T seed, Function<? super T, ? extends Iterable<? extends T>> descend) { Objects.requireNonNull(descend, "descend is null"); return Tree.of(seed, Stream.of(seed).flatMap(descend).map(children -> recurse(children, descend))); }
/** * 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)); }
@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()); } }
/** * Alias for {@link Stream#of(Object)} * * @param <T> Component type of element. * @param element An element. * @return A new {@link Stream} instance containing the given element */ public static <T> Stream<T> Stream(T element) { return Stream.of(element); }
@Override default Stream<Stream<T>> combinations() { return Stream.rangeClosed(0, length()).map(this::combinations).flatMap(Function.identity()); }
BinaryString stripHammingMetadata(EncodedString input) { String raw = Stream.from(0, 1).take(input.getValue().length()) .map(i -> Tuple.of(i + 1, Character.toString(input.getValue().charAt(i)))) .filter(t -> !HammingHelper.isPowerOfTwo(t._1)) .map(i -> i._2) .foldLeft("", String::concat); return BinaryString.of(raw); } }
private List<Integer> indexesOfInvalidParityBits(EncodedString input) { return Stream.iterate(1, i -> i * 2) .takeWhile(it -> it < input.getValue().length()) .filter(it -> helper.parityIndicesSequence(it - 1, input.getValue().length()) .map(v -> toBinaryInt(input, v)) .fold(toBinaryInt(input, it - 1), (a, b) -> a ^ b) != 0) .toList(); }