Skip to content

Commit 5bd8677

Browse files
committed
Changed Effect to Effect1 and renamed e to f
1 parent c65c183 commit 5bd8677

29 files changed

Lines changed: 145 additions & 116 deletions

core/src/main/java/fj/Effect.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
package fj;
22

3+
import fj.function.Effect1;
4+
35
import static fj.Unit.unit;
46

57
/**
68
* Represents a side-effect.
79
*
810
* @version %build.number%
911
*/
10-
public abstract class Effect<A> {
11-
public abstract void e(A a);
12+
public class Effect {
1213

14+
private Effect() {}
1315

1416
/**
1517
* Returns a function for the given effect.
1618
*
1719
* @return The function using the given effect.
1820
*/
19-
public final F<A, Unit> e() {
21+
public static final <A> F<A, Unit> f(Effect1<A> e1) {
2022
return new F<A, Unit>() {
2123
public Unit f(final A a) {
22-
e(a);
24+
e1.f(a);
2325
return unit();
2426
}
2527
};
@@ -31,19 +33,22 @@ public Unit f(final A a) {
3133
* @param f The function to map over the effect.
3234
* @return An effect after a contra-variant map.
3335
*/
34-
public final <B> Effect<B> comap(final F<B, A> f) {
35-
return new Effect<B>() {
36-
public void e(final B b) {
37-
Effect.this.e(f.f(b));
36+
public final <A, B> Effect1<B> comap(Effect1<A> e1, final F<B, A> f) {
37+
return new Effect1<B>() {
38+
public void f(final B b) {
39+
e1.f(f.f(b));
3840
}
3941
};
4042
}
4143

42-
public static <A> Effect<A> f(final F<A, Unit> f) {
43-
return new Effect<A>() {
44-
public void e(final A a) {
44+
public static <A> Effect1<A> f(final F<A, Unit> f) {
45+
return new Effect1<A>() {
46+
public void f(final A a) {
4547
f.f(a);
4648
}
4749
};
4850
}
51+
52+
// public static <A> void f(Effect1<A> )
53+
4954
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import fj.F;
88
import fj.Unit;
99
import fj.P1;
10+
import fj.function.Effect1;
1011

1112
/**
1213
* Light weight actors for Java. Concurrency is controlled by a parallel Strategy.
@@ -31,8 +32,8 @@ public final class Actor<A> {
3132
* With respect to an enqueueing actor or thread, this actor will process messages in the same order
3233
* as they are sent.
3334
*/
34-
public static <T> Actor<T> queueActor(final Strategy<Unit> s, final Effect<T> ea) {
35-
return actor(Strategy.<Unit>seqStrategy(), new Effect<T>() {
35+
public static <T> Actor<T> queueActor(final Strategy<Unit> s, final Effect1<T> ea) {
36+
return actor(Strategy.<Unit>seqStrategy(), new Effect1<T>() {
3637

3738
// Lock to ensure the actor only acts on one message at a time
3839
AtomicBoolean suspended = new AtomicBoolean(true);
@@ -48,7 +49,7 @@ public static <T> Actor<T> queueActor(final Strategy<Unit> s, final Effect<T> ea
4849
T a = mbox.poll();
4950
// if there is one, process it
5051
if (a != null) {
51-
ea.e(a);
52+
ea.f(a);
5253
// try again, in case there are more messages
5354
s.par(this);
5455
} else {
@@ -62,7 +63,7 @@ public static <T> Actor<T> queueActor(final Strategy<Unit> s, final Effect<T> ea
6263
};
6364

6465
// Effect's body -- queues up a message and tries to unsuspend the actor
65-
@Override public void e(T a) {
66+
@Override public void f(T a) {
6667
mbox.offer(a);
6768
work();
6869
}
@@ -92,8 +93,8 @@ public P1<Unit> f(final A a) {
9293
* @param e The side-effect to apply to messages passed to the Actor.
9394
* @return A new actor that uses the given parallelization strategy and has the given side-effect.
9495
*/
95-
public static <A> Actor<A> actor(final Strategy<Unit> s, final Effect<A> e) {
96-
return new Actor<A>(s, P1.curry(e.e()));
96+
public static <A> Actor<A> actor(final Strategy<Unit> s, final Effect1<A> e) {
97+
return new Actor<A>(s, P1.curry(Effect.f(e)));
9798
}
9899

99100
/**
@@ -139,8 +140,8 @@ public P1<Unit> f(final B b) {
139140
* @return A new actor, equivalent to this actor, that acts on promises.
140141
*/
141142
public Actor<Promise<A>> promise() {
142-
return actor(s, new Effect<Promise<A>>() {
143-
public void e(final Promise<A> b) {
143+
return actor(s, new Effect1<Promise<A>>() {
144+
public void f(final Promise<A> b) {
144145
b.to(Actor.this);
145146
}
146147
});

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import fj.data.Tree;
1616
import fj.data.TreeZipper;
1717
import fj.data.Zipper;
18+
import fj.function.Effect1;
19+
1820
import static fj.data.Option.some;
1921
import static fj.data.Stream.iterableStream;
2022

@@ -105,7 +107,7 @@ public <A, B, C> F2<A, B, Promise<C>> promise(final F2<A, B, C> f) {
105107
* @param e The effect that the actor should have on its messages.
106108
* @return A concurrent actor that does not guarantee ordering of its messages.
107109
*/
108-
public <A> Actor<A> effect(final Effect<A> e) {
110+
public <A> Actor<A> effect(final Effect1<A> e) {
109111
return Actor.actor(strategy, e);
110112
}
111113

@@ -115,9 +117,9 @@ public <A> Actor<A> effect(final Effect<A> e) {
115117
*
116118
* @return A function that takes an effect and returns a concurrent effect.
117119
*/
118-
public <A> F<Effect<A>, Actor<A>> effect() {
119-
return new F<Effect<A>, Actor<A>>() {
120-
public Actor<A> f(final Effect<A> effect) {
120+
public <A> F<Effect1<A>, Actor<A>> effect() {
121+
return new F<Effect1<A>, Actor<A>>() {
122+
public Actor<A> f(final Effect1<A> effect) {
121123
return effect(effect);
122124
}
123125
};
@@ -129,7 +131,7 @@ public Actor<A> f(final Effect<A> effect) {
129131
* @param e The effect that the actor should have on its messages.
130132
* @return A concurrent actor that is guaranteed to process its messages in order.
131133
*/
132-
public <A> Actor<A> actor(final Effect<A> e) {
134+
public <A> Actor<A> actor(final Effect1<A> e) {
133135
return Actor.queueActor(strategy, e);
134136
}
135137

@@ -138,9 +140,9 @@ public <A> Actor<A> actor(final Effect<A> e) {
138140
*
139141
* @return A function that takes an effect and returns an actor that processes messages in some order.
140142
*/
141-
public <A> F<Effect<A>, Actor<A>> actor() {
142-
return new F<Effect<A>, Actor<A>>() {
143-
public Actor<A> f(final Effect<A> effect) {
143+
public <A> F<Effect1<A>, Actor<A>> actor() {
144+
return new F<Effect1<A>, Actor<A>>() {
145+
public Actor<A> f(final Effect1<A> effect) {
144146
return actor(effect);
145147
}
146148
};

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static fj.data.Option.none;
2020
import static fj.data.Option.some;
2121
import fj.data.Stream;
22+
import fj.function.Effect1;
2223

2324
import java.util.LinkedList;
2425
import java.util.Queue;
@@ -50,8 +51,8 @@ private Promise(final Strategy<Unit> s, final Actor<P2<Either<P1<A>, Actor<A>>,
5051

5152
private static <A> Promise<A> mkPromise(final Strategy<Unit> s) {
5253
final Actor<P2<Either<P1<A>, Actor<A>>, Promise<A>>> q =
53-
queueActor(s, new Effect<P2<Either<P1<A>, Actor<A>>, Promise<A>>>() {
54-
public void e(final P2<Either<P1<A>, Actor<A>>, Promise<A>> p) {
54+
queueActor(s, new Effect1<P2<Either<P1<A>, Actor<A>>, Promise<A>>>() {
55+
public void f(final P2<Either<P1<A>, Actor<A>>, Promise<A>> p) {
5556
final Promise<A> snd = p._2();
5657
final Queue<Actor<A>> as = snd.waiting;
5758
if (p._1().isLeft()) {
@@ -194,8 +195,8 @@ public static <A> Promise<A> join(final Strategy<Unit> s, final P1<Promise<A>> p
194195
*/
195196
public <B> Promise<B> bind(final F<A, Promise<B>> f) {
196197
final Promise<B> r = mkPromise(s);
197-
final Actor<B> ab = actor(s, new Effect<B>() {
198-
public void e(final B b) {
198+
final Actor<B> ab = actor(s, new Effect1<B>() {
199+
public void f(final B b) {
199200
r.actor.act(P.p(Either.<P1<B>, Actor<B>>left(P.p(b)), r));
200201
}
201202
});

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import fj.data.Java;
1212
import fj.data.List;
1313
import fj.data.Array;
14+
import fj.function.Effect1;
1415

1516
import java.util.concurrent.Callable;
1617
import java.util.concurrent.CompletionService;
@@ -388,9 +389,9 @@ public A _1() {
388389
*
389390
* @return An effect, which, given a Future, waits for it to obtain a value, discarding the value.
390391
*/
391-
public static <A> Effect<Future<A>> discard() {
392-
return new Effect<Future<A>>() {
393-
public void e(final Future<A> a) {
392+
public static <A> Effect1<Future<A>> discard() {
393+
return new Effect1<Future<A>>() {
394+
public void f(final Future<A> a) {
394395
Strategy.<A>obtain().f(a)._1();
395396
}
396397
};
@@ -505,7 +506,7 @@ public Strategy<A> comap(final F<P1<A>, P1<A>> f) {
505506
* @param e The effect that should handle errors.
506507
* @return A strategy that captures any runtime errors with a side-effect.
507508
*/
508-
public Strategy<A> errorStrategy(final Effect<Error> e) {
509+
public Strategy<A> errorStrategy(final Effect1<Error> e) {
509510
return errorStrategy(this, e);
510511
}
511512

@@ -517,7 +518,7 @@ public Strategy<A> errorStrategy(final Effect<Error> e) {
517518
* @param e The effect that should handle errors.
518519
* @return A strategy that captures any runtime errors with a side-effect.
519520
*/
520-
public static <A> Strategy<A> errorStrategy(final Strategy<A> s, final Effect<Error> e) {
521+
public static <A> Strategy<A> errorStrategy(final Strategy<A> s, final Effect1<Error> e) {
521522
return s.comap(new F<P1<A>, P1<A>>() {
522523
public P1<A> f(final P1<A> a) {
523524
return new P1<A>() {
@@ -526,7 +527,7 @@ public A _1() {
526527
return a._1();
527528
} catch (Throwable t) {
528529
final Error error = new Error(t);
529-
e.e(error);
530+
e.f(error);
530531
throw error;
531532
}
532533
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import fj.P1;
88
import fj.P2;
99
import fj.Unit;
10+
import fj.function.Effect1;
1011

1112
import static fj.Function.*;
1213
import static fj.P.p;
@@ -229,9 +230,9 @@ public Unit foreach(final F<A, Unit> f) {
229230
* @param f The side-effect to perform for the given element.
230231
*/
231232
@SuppressWarnings("unchecked")
232-
public void foreach(final Effect<A> f) {
233+
public void foreach(final Effect1<A> f) {
233234
for (final Object x : a) {
234-
f.e((A) x);
235+
f.f((A) x);
235236
}
236237
}
237238

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

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import fj.Effect;
44
import fj.F;
55
import fj.P1;
6+
import fj.function.Effect1;
7+
68
import static fj.data.List.asString;
79
import static fj.data.List.fromString;
810

@@ -167,8 +169,8 @@ public Either<A, B> f(final Array<B> bs) {
167169
public static final F<Array<Character>, String> Array_String = new F<Array<Character>, String>() {
168170
public String f(final Array<Character> cs) {
169171
final StringBuilder sb = new StringBuilder();
170-
cs.foreach(new Effect<Character>() {
171-
public void e(final Character c) {
172+
cs.foreach(new Effect1<Character>() {
173+
public void f(final Character c) {
172174
sb.append(c);
173175
}
174176
});
@@ -182,8 +184,8 @@ public void e(final Character c) {
182184
public static final F<Array<Character>, StringBuffer> Array_StringBuffer = new F<Array<Character>, StringBuffer>() {
183185
public StringBuffer f(final Array<Character> cs) {
184186
final StringBuffer sb = new StringBuffer();
185-
cs.foreach(new Effect<Character>() {
186-
public void e(final Character c) {
187+
cs.foreach(new Effect1<Character>() {
188+
public void f(final Character c) {
187189
sb.append(c);
188190
}
189191
});
@@ -198,11 +200,7 @@ public void e(final Character c) {
198200
new F<Array<Character>, StringBuilder>() {
199201
public StringBuilder f(final Array<Character> cs) {
200202
final StringBuilder sb = new StringBuilder();
201-
cs.foreach(new Effect<Character>() {
202-
public void e(final Character c) {
203-
sb.append(c);
204-
}
205-
});
203+
cs.foreach((Character c) -> sb.append(c));
206204
return sb;
207205
}
208206
};
@@ -273,11 +271,7 @@ public Either<A, B> f(final Stream<B> bs) {
273271
public static final F<Stream<Character>, String> Stream_String = new F<Stream<Character>, String>() {
274272
public String f(final Stream<Character> cs) {
275273
final StringBuilder sb = new StringBuilder();
276-
cs.foreach(new Effect<Character>() {
277-
public void e(final Character c) {
278-
sb.append(c);
279-
}
280-
});
274+
cs.foreach((Character c) -> sb.append(c));
281275
return sb.toString();
282276
}
283277
};
@@ -289,11 +283,7 @@ public void e(final Character c) {
289283
new F<Stream<Character>, StringBuffer>() {
290284
public StringBuffer f(final Stream<Character> cs) {
291285
final StringBuffer sb = new StringBuffer();
292-
cs.foreach(new Effect<Character>() {
293-
public void e(final Character c) {
294-
sb.append(c);
295-
}
296-
});
286+
cs.foreach((Character c) -> sb.append(c));
297287
return sb;
298288
}
299289
};
@@ -305,11 +295,7 @@ public void e(final Character c) {
305295
new F<Stream<Character>, StringBuilder>() {
306296
public StringBuilder f(final Stream<Character> cs) {
307297
final StringBuilder sb = new StringBuilder();
308-
cs.foreach(new Effect<Character>() {
309-
public void e(final Character c) {
310-
sb.append(c);
311-
}
312-
});
298+
cs.foreach((Character c) -> sb.append(c));
313299
return sb;
314300
}
315301
};

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import fj.Function;
1111
import fj.P1;
1212
import fj.Unit;
13+
import fj.function.Effect1;
14+
1315
import static fj.Unit.unit;
1416
import static fj.data.Array.mkArray;
1517
import static fj.data.List.single;
@@ -230,9 +232,9 @@ public Unit foreach(final F<A, Unit> f) {
230232
*
231233
* @param f The side-effect to execute.
232234
*/
233-
public void foreach(final Effect<A> f) {
235+
public void foreach(final Effect1<A> f) {
234236
if (isLeft())
235-
f.e(value());
237+
f.f(value());
236238
}
237239

238240
/**
@@ -463,9 +465,9 @@ public Unit foreach(final F<B, Unit> f) {
463465
*
464466
* @param f The side-effect to execute.
465467
*/
466-
public void foreach(final Effect<B> f) {
468+
public void foreach(final Effect1<B> f) {
467469
if (isRight())
468-
f.e(value());
470+
f.f(value());
469471
}
470472

471473
/**

0 commit comments

Comments
 (0)