Skip to content

Commit eb92358

Browse files
committed
Refactoring Control and Data module
1 parent 0d3a1c5 commit eb92358

19 files changed

+413
-1610
lines changed

core/src/main/java/fj/control/Trampoline.java

Lines changed: 21 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -50,57 +50,21 @@ public <R> R fold(final F<Normal<A>, R> n,
5050
// The monadic bind constructs a new Codense whose subcomputation is still `sub`, and Kleisli-composes the
5151
// continuations.
5252
public <B> Trampoline<B> bind(final F<A, Trampoline<B>> f) {
53-
return codense(sub, new F<Object, Trampoline<B>>() {
54-
public Trampoline<B> f(final Object o) {
55-
return suspend(new P1<Trampoline<B>>() {
56-
public Trampoline<B> _1() {
57-
return cont.f(o).bind(f);
58-
}
59-
});
53+
return codense(sub, o -> suspend(new P1<Trampoline<B>>() {
54+
public Trampoline<B> _1() {
55+
return cont.f(o).bind(f);
6056
}
61-
});
57+
}));
6258
}
6359

6460
// The resumption of a Codense is the resumption of its subcomputation. If that computation is done, its result
6561
// gets shifted into the continuation.
6662
public Either<P1<Trampoline<A>>, A> resume() {
67-
return left(sub.resume().either(new F<P1<Trampoline<Object>>, P1<Trampoline<A>>>() {
68-
public P1<Trampoline<A>> f(final P1<Trampoline<Object>> p) {
69-
return p.map(new F<Trampoline<Object>, Trampoline<A>>() {
70-
public Trampoline<A> f(final Trampoline<Object> ot) {
71-
return ot.fold(new F<Normal<Object>, Trampoline<A>>() {
72-
public Trampoline<A> f(final Normal<Object> o) {
73-
return o.foldNormal(new F<Object, Trampoline<A>>() {
74-
public Trampoline<A> f(final Object o) {
75-
return cont.f(o);
76-
}
77-
}, new F<P1<Trampoline<Object>>, Trampoline<A>>() {
78-
public Trampoline<A> f(final P1<Trampoline<Object>> t) {
79-
return t._1().bind(cont);
80-
}
81-
}
82-
);
83-
}
84-
}, new F<Codense<Object>, Trampoline<A>>() {
85-
public Trampoline<A> f(final Codense<Object> c) {
86-
return codense(c.sub, new F<Object, Trampoline<A>>() {
87-
public Trampoline<A> f(final Object o) {
88-
return c.cont.f(o).bind(cont);
89-
}
90-
});
91-
}
92-
}
93-
);
94-
}
95-
});
96-
}
97-
}, new F<Object, P1<Trampoline<A>>>() {
98-
public P1<Trampoline<A>> f(final Object o) {
99-
return new P1<Trampoline<A>>() {
100-
public Trampoline<A> _1() {
101-
return cont.f(o);
102-
}
103-
};
63+
return left(sub.resume().either(p -> p.map(ot -> ot.fold(o1 -> o1.foldNormal(o2 -> cont.f(o2), t -> t._1().bind(cont)
64+
), c -> codense(c.sub, o -> c.cont.f(o).bind(cont))
65+
)), o3 -> new P1<Trampoline<A>>() {
66+
public Trampoline<A> _1() {
67+
return cont.f(o3);
10468
}
10569
}
10670
));
@@ -159,11 +123,7 @@ protected static <A, B> Codense<B> codense(final Normal<A> a, final F<A, Trampol
159123
* @return The first-class version of `pure`.
160124
*/
161125
public static <A> F<A, Trampoline<A>> pure() {
162-
return new F<A, Trampoline<A>>() {
163-
public Trampoline<A> f(final A a) {
164-
return pure(a);
165-
}
166-
};
126+
return a -> pure(a);
167127
}
168128

169129
/**
@@ -190,11 +150,7 @@ public static <A> Trampoline<A> suspend(final P1<Trampoline<A>> a) {
190150
* @return The first-class version of `suspend`.
191151
*/
192152
public static <A> F<P1<Trampoline<A>>, Trampoline<A>> suspend_() {
193-
return new F<P1<Trampoline<A>>, Trampoline<A>>() {
194-
public Trampoline<A> f(final P1<Trampoline<A>> trampolineP1) {
195-
return suspend(trampolineP1);
196-
}
197-
};
153+
return trampolineP1 -> suspend(trampolineP1);
198154
}
199155

200156
protected abstract <R> R fold(final F<Normal<A>, R> n, final F<Codense<A>, R> gs);
@@ -221,41 +177,21 @@ public final <B> Trampoline<B> map(final F<A, B> f) {
221177
* @return The first-class version of `bind`.
222178
*/
223179
public static <A, B> F<F<A, Trampoline<B>>, F<Trampoline<A>, Trampoline<B>>> bind_() {
224-
return new F<F<A, Trampoline<B>>, F<Trampoline<A>, Trampoline<B>>>() {
225-
public F<Trampoline<A>, Trampoline<B>> f(final F<A, Trampoline<B>> f) {
226-
return new F<Trampoline<A>, Trampoline<B>>() {
227-
public Trampoline<B> f(final Trampoline<A> a) {
228-
return a.bind(f);
229-
}
230-
};
231-
}
232-
};
180+
return f -> a -> a.bind(f);
233181
}
234182

235183
/**
236184
* @return The first-class version of `map`.
237185
*/
238186
public static <A, B> F<F<A, B>, F<Trampoline<A>, Trampoline<B>>> map_() {
239-
return new F<F<A, B>, F<Trampoline<A>, Trampoline<B>>>() {
240-
public F<Trampoline<A>, Trampoline<B>> f(final F<A, B> f) {
241-
return new F<Trampoline<A>, Trampoline<B>>() {
242-
public Trampoline<B> f(final Trampoline<A> a) {
243-
return a.map(f);
244-
}
245-
};
246-
}
247-
};
187+
return f -> a -> a.map(f);
248188
}
249189

250190
/**
251191
* @return The first-class version of `resume`.
252192
*/
253193
public static <A> F<Trampoline<A>, Either<P1<Trampoline<A>>, A>> resume_() {
254-
return new F<Trampoline<A>, Either<P1<Trampoline<A>>, A>>() {
255-
public Either<P1<Trampoline<A>>, A> f(final Trampoline<A> aTrampoline) {
256-
return aTrampoline.resume();
257-
}
258-
};
194+
return aTrampoline -> aTrampoline.resume();
259195
}
260196

261197
/**
@@ -291,11 +227,7 @@ public A run() {
291227
* @return A new Trampoline after applying the given function through this Trampoline.
292228
*/
293229
public final <B> Trampoline<B> apply(final Trampoline<F<A, B>> lf) {
294-
return lf.bind(new F<F<A, B>, Trampoline<B>>() {
295-
public Trampoline<B> f(final F<A, B> f) {
296-
return map(f);
297-
}
298-
});
230+
return lf.bind(f -> map(f));
299231
}
300232

301233
/**
@@ -317,11 +249,7 @@ public final <B, C> Trampoline<C> bind(final Trampoline<B> lb, final F<A, F<B, C
317249
* @return The given function, promoted to operate on Trampolines.
318250
*/
319251
public static <A, B, C> F<Trampoline<A>, F<Trampoline<B>, Trampoline<C>>> liftM2(final F<A, F<B, C>> f) {
320-
return curry(new F2<Trampoline<A>, Trampoline<B>, Trampoline<C>>() {
321-
public Trampoline<C> f(final Trampoline<A> as, final Trampoline<B> bs) {
322-
return as.bind(bs, f);
323-
}
324-
});
252+
return curry((as, bs) -> as.bind(bs, f));
325253
}
326254

327255
/**
@@ -337,22 +265,14 @@ public <B, C> Trampoline<C> zipWith(final Trampoline<B> b, final F2<A, B, C> f)
337265
final Either<P1<Trampoline<B>>, B> eb = b.resume();
338266
for (final P1<Trampoline<A>> x : ea.left()) {
339267
for (final P1<Trampoline<B>> y : eb.left()) {
340-
return suspend(x.bind(y, F2Functions.curry(new F2<Trampoline<A>, Trampoline<B>, Trampoline<C>>() {
341-
public Trampoline<C> f(final Trampoline<A> ta, final Trampoline<B> tb) {
342-
return suspend(new P1<Trampoline<C>>() {
343-
public Trampoline<C> _1() {
344-
return ta.zipWith(tb, f);
345-
}
346-
});
268+
return suspend(x.bind(y, F2Functions.curry((ta, tb) -> suspend(new P1<Trampoline<C>>() {
269+
public Trampoline<C> _1() {
270+
return ta.zipWith(tb, f);
347271
}
348-
})));
272+
}))));
349273
}
350274
for (final B y : eb.right()) {
351-
return suspend(x.map(new F<Trampoline<A>, Trampoline<C>>() {
352-
public Trampoline<C> f(final Trampoline<A> ta) {
353-
return ta.map(F2Functions.f(F2Functions.flip(f), y));
354-
}
355-
}));
275+
return suspend(x.map(ta -> ta.map(F2Functions.f(F2Functions.flip(f), y))));
356276
}
357277
}
358278
for (final A x : ea.right()) {

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,7 @@ protected void work() {
7979

8080
private Actor(final Strategy<Unit> s, final F<A, P1<Unit>> e) {
8181
this.s = s;
82-
f = new F<A, P1<Unit>>() {
83-
public P1<Unit> f(final A a) {
84-
return s.par(e.f(a));
85-
}
86-
};
82+
f = a -> s.par(e.f(a));
8783
}
8884

8985
/**
@@ -127,11 +123,7 @@ public P1<Unit> act(final A a) {
127123
* @return A new actor which passes its messages through the given function, to this actor.
128124
*/
129125
public <B> Actor<B> comap(final F<B, A> f) {
130-
return actor(s, new F<B, P1<Unit>>() {
131-
public P1<Unit> f(final B b) {
132-
return act(f.f(b));
133-
}
134-
});
126+
return actor(s, (B b) -> act(f.f(b)));
135127
}
136128

137129
/**
@@ -140,11 +132,7 @@ public P1<Unit> f(final B b) {
140132
* @return A new actor, equivalent to this actor, that acts on promises.
141133
*/
142134
public Actor<Promise<A>> promise() {
143-
return actor(s, new Effect1<Promise<A>>() {
144-
public void f(final Promise<A> b) {
145-
b.to(Actor.this);
146-
}
147-
});
135+
return actor(s, (Promise<A> b) -> b.to(Actor.this));
148136
}
149137

150138
}

0 commit comments

Comments
 (0)