@Override public Vector<Object> flatten() { return flatMap( t -> (t instanceof java.lang.Iterable) ? Vector.ofAll((java.lang.Iterable<?>) t).flatten() : Vector.of(t)); }
Vector<Vector<T>> combinations(Vector<T> elements, int k) { return (k == 0) ? Vector.of(Vector.empty()) : elements .zipWithIndex() .flatMap( t -> combinations(elements.drop(t._2 + 1), (k - 1)) .map((Vector<T> c) -> c.prepend(t._1))); }
@Override public Vector<Vector<T>> permutations() { if (isEmpty()) { return Vector.empty(); } else { final Vector<T> tail = tail(); if (tail.isEmpty()) { return Vector.of(this); } else { final Vector<Vector<T>> zero = empty(); // TODO: IntelliJ IDEA 14.1.1 needs a redundant cast here, jdk 1.8.0_40 compiles fine return distinct() .foldLeft( zero, (xs, x) -> xs.appendAll( remove(x) .permutations() .map((Function<Vector<T>, Vector<T>>) l -> l.prepend(x)))); } } }