Skip to content

Commit 1d52ca3

Browse files
committed
Fixed some methods that were implemented inefficiently or failed with StackOverflow.
1 parent bc5913e commit 1d52ca3

6 files changed

Lines changed: 58 additions & 19 deletions

File tree

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package fj.data;
22

3-
import fj.Equal;
4-
import fj.F;
5-
import fj.F2;
3+
import fj.*;
4+
65
import static fj.Function.compose;
76
import static fj.Function.curry;
87
import static fj.P.p;
9-
import fj.P1;
10-
import fj.P2;
118
import static fj.data.Option.none;
129
import static fj.data.Option.some;
1310
import static fj.data.Stream.join;
@@ -108,7 +105,9 @@ public CharSequence subSequence(final int start, final int end) {
108105
* @return The String representation of this lazy string.
109106
*/
110107
public String toString() {
111-
return new StringBuilder(length() + 16).append(this).toString();
108+
final StringBuilder builder = new StringBuilder(length() + 16);
109+
s.foreachDoEffect(c -> builder.append(c.charValue()));
110+
return builder.toString();
112111
}
113112

114113
/**

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ public final <X> Either<X, A> toEither(final P1<X> x) {
159159
* @return A stream projection of this list.
160160
*/
161161
public final Stream<A> toStream() {
162-
final Stream<A> nil = Stream.nil();
163-
return foldRight(a -> as -> as.cons(a), nil);
162+
return isEmpty() ? Stream.nil() : Stream.cons(head(), P.lazy(() -> tail().toStream()));
164163
}
165164

166165
/**
@@ -1569,7 +1568,11 @@ public static <A> List<A> replicate(final int n, final A a) {
15691568
* <code>to</code> value (exclusive).
15701569
*/
15711570
public static List<Integer> range(final int from, final int to) {
1572-
return from >= to ? List.<Integer>nil() : cons(from, range(from + 1, to));
1571+
final Buffer<Integer> buf = Buffer.empty();
1572+
for (int i = from; i < to; i++) {
1573+
buf.snoc(i);
1574+
}
1575+
return buf.toList();
15731576
}
15741577

15751578
/**

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,7 @@ public final <B, C> F<B, Stream<C>> mapM(final F<A, F<B, C>> f) {
386386
* @return A new stream after performing the map, then final join.
387387
*/
388388
public final <B> Stream<B> bind(final F<A, Stream<B>> f) {
389-
return map(f).foldLeft((accumulator, element) -> {
390-
Stream<B> result = accumulator;
391-
for (B single : element) {
392-
result = result.cons(single);
393-
}
394-
return result;
395-
}, Stream.<B>nil()).reverse();
389+
return foldRight(h -> (t -> f.f(h).append(t)), nil());
396390
}
397391

398392
/**
@@ -1554,9 +1548,7 @@ public static <A> Stream<A> cons(final A head, final P1<Stream<A>> tail) {
15541548
* @param o The stream of streams to join.
15551549
* @return A new stream that is the join of the given streams.
15561550
*/
1557-
public static <A> Stream<A> join(final Stream<Stream<A>> o) {
1558-
return Monoid.<A>streamMonoid().sumRight(o);
1559-
}
1551+
public static <A> Stream<A> join(final Stream<Stream<A>> o) { return o.bind(identity()); }
15601552

15611553
/**
15621554
* A first-class version of join

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.Arrays;
66

7+
import static org.junit.Assert.assertEquals;
78
import static org.junit.Assert.assertFalse;
89
import static org.junit.Assert.assertTrue;
910

@@ -39,4 +40,17 @@ public void integration() {
3940

4041
}
4142

43+
@Test
44+
public void convertToString() {
45+
final int n = 10000;
46+
final StringBuilder expected = new StringBuilder("List(");
47+
for (int i = 0; i < n; i++) {
48+
expected.append(i);
49+
if (i < n - 1) {
50+
expected.append(',');
51+
}
52+
}
53+
expected.append(')');
54+
assertEquals(expected.toString(), List.range(0, n).toString());
55+
}
4256
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Test;
44

5+
import static org.junit.Assert.assertEquals;
56
import static org.junit.Assert.assertFalse;
67
import static org.junit.Assert.assertTrue;
78

@@ -20,4 +21,17 @@ public void objectMethods() {
2021

2122
}
2223

24+
@Test
25+
public void convertToString() {
26+
final int n = 10000;
27+
final StringBuilder expected = new StringBuilder("Seq(");
28+
for (int i = 0; i < n; i++) {
29+
expected.append(i);
30+
if (i < n - 1) {
31+
expected.append(',');
32+
}
33+
}
34+
expected.append(')');
35+
assertEquals(expected.toString(), Seq.seq(Array.range(0, 10000).array()).toString());
36+
}
2337
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package fj.data;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
/**
8+
* Created by Zheka Kozlov on 27.05.2015.
9+
*/
10+
public class StreamTest {
11+
12+
@Test
13+
public void infiniteStream() {
14+
Stream<Integer> s = Stream.forever(Enumerator.intEnumerator, 0).bind(Stream::single);
15+
assertEquals(List.range(0, 5), s.take(5).toList());
16+
}
17+
}

0 commit comments

Comments
 (0)