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))); }