@Override public Vector<Vector<T>> permutations() { if (isEmpty()) { return empty(); } else if (length() == 1) { return of(this); } else { Vector<Vector<T>> results = empty(); for (T t : distinct()) { for (Vector<T> ts : remove(t).permutations()) { results = results.append(of(t).appendAll(ts)); } } return results; } }
@Override public Vector<T> reverse() { return (length() <= 1) ? this : ofAll(reverseIterator()); }
@Override public <T1, T2> Tuple2<Vector<T1>, Vector<T2>> unzip(Function<? super T, Tuple2<? extends T1, ? extends T2>> unzipper) { Objects.requireNonNull(unzipper, "unzipper is null"); Vector<T1> xs = empty(); Vector<T2> ys = empty(); for (int i = 0; i < length(); i++) { final Tuple2<? extends T1, ? extends T2> t = unzipper.apply(get(i)); xs = xs.append(t._1); ys = ys.append(t._2); } return Tuple.of(xs, ys); }
@SuppressWarnings("ObjectEquality") private Vector<T> wrap(BitMappedTrie<T> trie) { return (trie == this.trie) ? this : ofAll(trie); }
/** * Reduces many {@code Lazy} values into a single {@code Lazy} by transforming an * {@code Iterable<Lazy<? extends T>>} into a {@code Lazy<Seq<T>>}. * * @param <T> Type of the lazy values. * @param values An iterable of lazy values. * @return A lazy sequence of values. * @throws NullPointerException if values is null */ @SuppressWarnings("Convert2MethodRef") // TODO should be fixed in JDK 9 and Idea public static <T> Lazy<Seq<T>> sequence(Iterable<? extends Lazy<? extends T>> values) { Objects.requireNonNull(values, "values is null"); return Lazy.of(() -> Vector.ofAll(values).map(lazy -> lazy.get())); }
@Override public Tuple2<Vector<T>, Vector<T>> partition(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); final ArrayList<T> left = new ArrayList<>(), right = new ArrayList<>(); for (int i = 0; i < length(); i++) { final T t = get(i); (predicate.test(t) ? left : right).add(t); } return Tuple.of(ofAll(left), ofAll(right)); }
@Override public Tuple2<Vector<T>, Vector<T>> splitAtInclusive(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); for (int i = 0; i < length(); i++) { final T value = get(i); if (predicate.test(value)) { return (i == (length() - 1)) ? Tuple.of(this, empty()) : Tuple.of(take(i + 1), drop(i + 1)); } } return Tuple.of(this, empty()); }
/** * Reduces many {@code Option}s into a single {@code Option} by transforming an * {@code Iterable<Option<? extends T>>} into a {@code Option<Seq<T>>}. If any of * the Options are {@link Option.None}, then this returns {@link Option.None}. * * @param values An {@code Iterable} of {@code Option}s * @param <T> type of the Options * @return An {@code Option} of a {@link Seq} of results * @throws NullPointerException if {@code values} is null */ static <T> Option<Seq<T>> sequence(Iterable<? extends Option<? extends T>> values) { Objects.requireNonNull(values, "values is null"); Vector<T> vector = Vector.empty(); for (Option<? extends T> value : values) { if (value.isEmpty()) { return Option.none(); } vector = vector.append(value.get()); } return Option.some(vector); }
@Override public Vector<Vector<T>> combinations() { return rangeClosed(0, length()).map(this::combinations).flatMap(Function.identity()); }
/** * Alias for {@link Vector#empty()} * * @param <T> Component type of element. * @return A singleton instance of empty {@link Vector} */ public static <T> Vector<T> Vector() { return Vector.empty(); }
/** * 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())); }
@Override public IndexedSeq<CharSeq> combinations() { return Vector.rangeClosed(0, length()).map(this::combinations).flatMap(Function.identity()); }
/** * Alias for {@link Vector#of(Object)} * * @param <T> Component type of element. * @param element An element. * @return A new {@link Vector} instance containing the given element */ public static <T> Vector<T> Vector(T element) { return Vector.of(element); }
@Override public Vector<T> sorted() { if (isEmpty()) { return this; } else { @SuppressWarnings("unchecked") final T[] list = (T[]) toJavaArray(); Arrays.sort(list); return Vector.of(list); } }