Skip to content

Commit

Permalink
Performance improvement for List::unfold, List::unfoldLeft (#2689)
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepytomcat authored and pivovarit committed Aug 19, 2024
1 parent 1425b74 commit 2e2373d
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions vavr/src/main/java/io/vavr/collection/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,8 @@ static <T, U> List<U> unfoldRight(T seed, Function<? super T, Option<Tuple2<? ex
* @throws NullPointerException if {@code f} is null
*/
static <T, U> List<U> unfoldLeft(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends U>>> f) {
return Iterator.unfoldLeft(seed, f).toList();
return Iterator.unfoldRight(seed, f.andThen(tupleOpt -> tupleOpt.map(Tuple2::swap)))
.foldLeft(List.empty(), List::prepend);
}

/**
Expand Down Expand Up @@ -740,7 +741,8 @@ static <T, U> List<U> unfoldLeft(T seed, Function<? super T, Option<Tuple2<? ext
* @throws NullPointerException if {@code f} is null
*/
static <T> List<T> unfold(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends T>>> f) {
return Iterator.unfold(seed, f).toList();
return Iterator.unfoldRight(seed, f.andThen(tupleOpt -> tupleOpt.map(Tuple2::swap)))
.foldLeft(List.empty(), List::prepend);
}

@Override
Expand Down

0 comments on commit 2e2373d

Please sign in to comment.