Skip to content

Commit

Permalink
improve Array.permutations() performance #1995 (#1996)
Browse files Browse the repository at this point in the history
* improve Array.permutations() performance #1995

* revert to very old algorithm #1995
  • Loading branch information
ruslansennov authored and danieldietrich committed Sep 16, 2017
1 parent 4a6ef47 commit af215c6
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions vavr/src/main/java/io/vavr/collection/Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -989,13 +989,11 @@ public Array<Array<T>> permutations() {
} else if (delegate.length == 1) {
return of(this);
} else {
Array<Array<T>> results = empty();
for (T t : distinct()) {
for (Array<T> ts : remove(t).permutations()) {
results = results.append(of(t).appendAll(ts));
}
}
return results;
final Array<Array<T>> zero = empty();
return distinct().foldLeft(zero, (xs, x) -> {
final Function<Array<T>, Array<T>> prepend = l -> l.prepend(x);
return xs.appendAll(remove(x).permutations().map(prepend));
});
}
}

Expand Down

0 comments on commit af215c6

Please sign in to comment.