예제 #1
1
 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)));
 }
예제 #2
1
 @Override
 public Vector<T> slice(int beginIndex, int endIndex) {
   if (beginIndex < 0 || beginIndex > endIndex || endIndex > length()) {
     throw new IndexOutOfBoundsException(
         String.format("slice(%s, %s) on List of length %s", beginIndex, endIndex, length()));
   }
   if (beginIndex == endIndex) {
     return Vector.empty();
   }
   HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty();
   for (int i = beginIndex; i < endIndex; i++) {
     trie = trie.put(trie.size(), get(i));
   }
   return trie.isEmpty() ? empty() : new Vector<>(trie);
 }
예제 #3
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))));
     }
   }
 }
예제 #4
1
 @Override
 public Vector<T> clear() {
   return Vector.empty();
 }