Skip to content

Commit b95c232

Browse files
committed
#188: Made List.intersperse use heap, not stack
1 parent b8a8d7d commit b95c232

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ public final Option<A> find(final F<A, Boolean> f) {
10691069
public final List<A> intersperse(final A a) {
10701070
return isEmpty() || tail().isEmpty() ?
10711071
this :
1072-
cons(head(), cons(a, tail().intersperse(a)));
1072+
cons(head(), tail().bind(a2 -> List.list(a, a2)));
10731073
}
10741074

10751075
/**

core/src/test/java/fj/data/ListTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@ public void partition() {
6464
assertTrue(e.eq(p._2(), List.list(1, 3)));
6565
}
6666

67+
@Test
68+
public void intersperseOverflow() {
69+
// should not overflow
70+
int n = 100000;
71+
List<Integer> list = List.replicate(n, 1).intersperse(2);
72+
String s = list.toString();
73+
}
74+
6775
}

0 commit comments

Comments
 (0)