Skip to content

Commit 2e2373d

Browse files
sleepytomcatpivovarit
authored andcommitted
Performance improvement for List::unfold, List::unfoldLeft (#2689)
1 parent 1425b74 commit 2e2373d

File tree

1 file changed

+4
-2
lines changed
  • vavr/src/main/java/io/vavr/collection

1 file changed

+4
-2
lines changed

vavr/src/main/java/io/vavr/collection/List.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,8 @@ static <T, U> List<U> unfoldRight(T seed, Function<? super T, Option<Tuple2<? ex
712712
* @throws NullPointerException if {@code f} is null
713713
*/
714714
static <T, U> List<U> unfoldLeft(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends U>>> f) {
715-
return Iterator.unfoldLeft(seed, f).toList();
715+
return Iterator.unfoldRight(seed, f.andThen(tupleOpt -> tupleOpt.map(Tuple2::swap)))
716+
.foldLeft(List.empty(), List::prepend);
716717
}
717718

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

746748
@Override

0 commit comments

Comments
 (0)