Skip to content

Commit 02580b1

Browse files
committed
Fix Stream.length to use an iterative approach here as the previous implementation (toList().length()) took very long (several seconds) even for some 10000 elements.
1 parent 476575d commit 02580b1

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

core/src/main/java/fj/data/Stream.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,15 @@ public final A last() {
12391239
* @return The length of this stream.
12401240
*/
12411241
public final int length() {
1242-
return toList().length();
1242+
// we're using an iterative approach here as the previous implementation (toList().length()) took
1243+
// very long even for some 10000 elements.
1244+
Stream<A> xs = this;
1245+
int i = 0;
1246+
while (!xs.isEmpty()) {
1247+
xs = xs.tail()._1();
1248+
i += 1;
1249+
}
1250+
return i;
12431251
}
12441252

12451253
/**

0 commit comments

Comments
 (0)