Skip to content

Commit 3d67791

Browse files
committed
Comverted P1 to lambdas for fj root
1 parent 0b0df0f commit 3d67791

File tree

10 files changed

+24
-94
lines changed

10 files changed

+24
-94
lines changed

core/src/main/java/fj/Bottom.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ public static Error error(final String s) {
3838
* @return A thunk that throws an error using the given message when evaluated.
3939
*/
4040
public static <A> P1<A> error_(final String s) {
41-
return new P1<A>() {
42-
@Override public A _1() {
41+
return P.lazy(() -> {
4342
throw new Error(s);
44-
}
45-
};
43+
});
4644
}
4745

4846
/**

core/src/main/java/fj/F2Functions.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,7 @@ static public <A, B, C> F2<Tree<A>, Tree<B>, Tree<C>> treeM(final F2<A, B, C> f)
142142
return new F2<Tree<A>, Tree<B>, Tree<C>>() {
143143
public Tree<C> f(final Tree<A> as, final Tree<B> bs) {
144144
final F2<Tree<A>, Tree<B>, Tree<C>> self = this;
145-
return node(f.f(as.root(), bs.root()), new P1<Stream<Tree<C>>>() {
146-
public Stream<Tree<C>> _1() {
147-
return streamM(self).f(as.subForest()._1(), bs.subForest()._1());
148-
}
149-
});
145+
return node(f.f(as.root(), bs.root()), P.lazy(() -> streamM(self).f(as.subForest()._1(), bs.subForest()._1())));
150146
}
151147
};
152148
}
@@ -217,11 +213,7 @@ static public <A, B, C> F2<Tree<A>, Tree<B>, Tree<C>> zipTreeM(final F2<A, B, C>
217213
return new F2<Tree<A>, Tree<B>, Tree<C>>() {
218214
public Tree<C> f(final Tree<A> ta, final Tree<B> tb) {
219215
final F2<Tree<A>, Tree<B>, Tree<C>> self = this;
220-
return node(f.f(ta.root(), tb.root()), new P1<Stream<Tree<C>>>() {
221-
public Stream<Tree<C>> _1() {
222-
return zipStreamM(self).f(ta.subForest()._1(), tb.subForest()._1());
223-
}
224-
});
216+
return node(f.f(ta.root(), tb.root()), P.lazy(() -> zipStreamM(self).f(ta.subForest()._1(), tb.subForest()._1())));
225217
}
226218
};
227219
}

core/src/main/java/fj/P2.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,7 @@ public final <C, X> Either<X, P2<A, C>> traverseEither(final F<B, Either<X, C>>
187187
public final <C> Stream<C> sequenceW(final Stream<F<P2<A, B>, C>> fs) {
188188
return fs.isEmpty()
189189
? Stream.<C>nil()
190-
: Stream.cons(fs.head().f(this), new P1<Stream<C>>() {
191-
public Stream<C> _1() {
192-
return sequenceW(fs.tail()._1());
193-
}
194-
});
190+
: Stream.cons(fs.head().f(this), P.lazy(() -> sequenceW(fs.tail()._1())));
195191
}
196192

197193
/**

core/src/main/java/fj/Primitive.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ private Primitive() {
113113
/**
114114
* A function that converts characters to integers.
115115
*/
116-
public static final F<Character, Integer> Character_Integer = new F<Character, Integer>() {
117-
public Integer f(final Character c) {
118-
return (int) (char) c;
119-
}
120-
};
116+
public static final F<Character, Integer> Character_Integer = c -> (int) (char) c;
121117

122118
/**
123119
* A function that converts characters to longs.
@@ -141,11 +137,7 @@ public Integer f(final Character c) {
141137
/**
142138
* A function that converts doubles to bytes.
143139
*/
144-
public static final F<Double, Byte> Double_Byte = new F<Double, Byte>() {
145-
public Byte f(final Double d) {
146-
return (byte) (double) d;
147-
}
148-
};
140+
public static final F<Double, Byte> Double_Byte = d -> (byte) (double) d;
149141

150142
/**
151143
* A function that converts doubles to characters.

core/src/main/java/fj/Semigroup.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,7 @@ public static <A> Semigroup<Array<A>> arraySemigroup() {
305305
* @return A semigroup for unary products.
306306
*/
307307
public static <A> Semigroup<P1<A>> p1Semigroup(final Semigroup<A> sa) {
308-
return semigroup((a1, a2) -> new P1<A>() {
309-
public A _1() {
310-
return sa.sum(a1._1(), a2._1());
311-
}
312-
});
308+
return semigroup((a1, a2) -> P.lazy(() -> sa.sum(a1._1(), a2._1())));
313309
}
314310

315311
/**

core/src/main/java/fj/control/parallel/Callables.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package fj.control.parallel;
22

3-
import fj.F;
4-
import fj.F0;
5-
import fj.F2;
6-
import fj.Function;
3+
import fj.*;
4+
75
import static fj.Function.curry;
8-
import fj.P1;
6+
97
import fj.data.Either;
108
import static fj.data.Either.left;
119
import static fj.data.Either.right;
@@ -196,15 +194,13 @@ public static <A> F<Callable<A>, P1<Option<A>>> option() {
196194
* @return Either the value in the given Callable, or the Exception with which the Callable fails.
197195
*/
198196
public static <A> P1<Either<Exception, A>> either(final Callable<A> a) {
199-
return new P1<Either<Exception, A>>() {
200-
public Either<Exception, A> _1() {
197+
return P.lazy(() -> {
201198
try {
202-
return right(a.call());
199+
return right(a.call());
203200
} catch (Exception e) {
204-
return left(e);
201+
return left(e);
205202
}
206-
}
207-
};
203+
});
208204
}
209205

210206
/**

core/src/main/java/fj/control/parallel/Promise.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@ public static <A> F<P1<A>, Promise<A>> promise(final Strategy<Unit> s) {
9797
* @return A promise of a new Callable that will return the result of calling the given Callable.
9898
*/
9999
public static <A> Promise<Callable<A>> promise(final Strategy<Unit> s, final Callable<A> a) {
100-
return promise(s, new P1<Callable<A>>() {
101-
public Callable<A> _1() {
102-
return normalise(a);
103-
}
104-
});
100+
return promise(s, P.lazy(() -> normalise(a)));
105101
}
106102

107103
/**
@@ -368,11 +364,7 @@ public boolean isFulfilled() {
368364
* @return A new promise of the result of applying the given function to this promise.
369365
*/
370366
public <B> Promise<B> cobind(final F<Promise<A>, B> f) {
371-
return promise(s, new P1<B>() {
372-
public B _1() {
373-
return f.f(Promise.this);
374-
}
375-
});
367+
return promise(s, P.lazy(() -> f.f(Promise.this)));
376368
}
377369

378370
/**
@@ -394,11 +386,7 @@ public Promise<Promise<A>> cojoin() {
394386
public <B> Stream<B> sequenceW(final Stream<F<Promise<A>, B>> fs) {
395387
return fs.isEmpty()
396388
? Stream.<B>nil()
397-
: Stream.cons(fs.head().f(this), new P1<Stream<B>>() {
398-
public Stream<B> _1() {
399-
return sequenceW(fs.tail()._1());
400-
}
401-
});
389+
: Stream.cons(fs.head().f(this), P.lazy(() -> sequenceW(fs.tail()._1())));
402390
}
403391

404392
}

core/src/main/java/fj/data/vector/V4.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,6 @@ public static <A> F<V4<A>, A> __3() {
289289
* @return a function that gets the fourth element of a given vector.
290290
*/
291291
public static <A> F<V4<A>, A> __4() {
292-
return new F<V4<A>, A>() {
293-
public A f(final V4<A> v) {
294-
return v._4();
295-
}
296-
};
292+
return v -> v._4();
297293
}
298294
}

core/src/main/java/fj/data/vector/V5.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,7 @@ public V5<V2<A>> vzip(final V5<A> bs) {
261261
* @return a function that transforms a vector-5 to a stream of its elements.
262262
*/
263263
public static <A> F<V5<A>, Stream<A>> toStream_() {
264-
return new F<V5<A>, Stream<A>>() {
265-
public Stream<A> f(final V5<A> v) {
266-
return v.toStream();
267-
}
268-
};
264+
return v -> v.toStream();
269265
}
270266

271267
/**
@@ -274,11 +270,7 @@ public Stream<A> f(final V5<A> v) {
274270
* @return a function that transforms a vector-5 to the equivalent product-5.
275271
*/
276272
public static <A> F<V5<A>, P5<A, A, A, A, A>> p_() {
277-
return new F<V5<A>, P5<A, A, A, A, A>>() {
278-
public P5<A, A, A, A, A> f(final V5<A> v) {
279-
return v.p();
280-
}
281-
};
273+
return v -> v.p();
282274
}
283275

284276
/**
@@ -305,11 +297,7 @@ public static <A> F<V5<A>, A> __2() {
305297
* @return a function that gets the third element of a given vector.
306298
*/
307299
public static <A> F<V5<A>, A> __3() {
308-
return new F<V5<A>, A>() {
309-
public A f(final V5<A> v) {
310-
return v._3();
311-
}
312-
};
300+
return v -> v._3();
313301
}
314302

315303
/**

core/src/test/java/fj/P1Test.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,8 @@ public void bug105() throws Exception {
3131

3232
@Test
3333
public void bug122() throws Exception {
34-
final P1<Integer> p1a = new P1<Integer>() {
35-
36-
@Override
37-
public Integer _1() {
38-
return 1;
39-
}
40-
};
41-
final P1<Integer> p1b = new P1<Integer>() {
42-
43-
@Override
44-
public Integer _1() {
45-
return 1;
46-
}
47-
};
34+
final P1<Integer> p1a = P.lazy(() -> 1);
35+
final P1<Integer> p1b = P.lazy(() -> 1);
4836

4937
org.junit.Assert.assertTrue(p1a + " and " + p1b + " should be equal by Object.equals", p1a.equals(p1b));
5038
}

0 commit comments

Comments
 (0)