Skip to content

Commit b85977b

Browse files
authored
Performance improvement for List::unfold, List::unfoldLeft (#2689)
1 parent 8b9cb31 commit b85977b

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,8 @@ public static <T, U> List<U> unfoldRight(T seed, Function<? super T, Option<Tupl
696696
* @throws NullPointerException if {@code f} is null
697697
*/
698698
public static <T, U> List<U> unfoldLeft(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends U>>> f) {
699-
return Iterator.unfoldLeft(seed, f).toList();
699+
return Iterator.unfoldRight(seed, f.andThen(tupleOpt -> tupleOpt.map(Tuple2::swap)))
700+
.foldLeft(List.empty(), List::prepend);
700701
}
701702

702703
/**
@@ -724,7 +725,8 @@ public static <T, U> List<U> unfoldLeft(T seed, Function<? super T, Option<Tuple
724725
* @throws NullPointerException if {@code f} is null
725726
*/
726727
public static <T> List<T> unfold(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends T>>> f) {
727-
return Iterator.unfold(seed, f).toList();
728+
return Iterator.unfoldRight(seed, f.andThen(tupleOpt -> tupleOpt.map(Tuple2::swap)))
729+
.foldLeft(List.empty(), List::prepend);
728730
}
729731

730732
@Override

0 commit comments

Comments
 (0)