annotate src/main/java/fj/Effect.java @ 0:fe80c1edf1be

add getLoop
author tatsuki
date Fri, 20 Mar 2015 21:04:03 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
1 package fj;
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
2
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
3 import fj.function.Effect0;
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
4 import fj.function.Effect1;
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
5 import fj.function.Effect2;
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
6 import fj.function.Effect3;
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
7 import fj.function.Effect4;
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
8 import fj.function.Effect5;
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
9 import fj.function.Effect6;
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
10 import fj.function.Effect7;
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
11 import fj.function.Effect8;
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
12
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
13 import static fj.Unit.unit;
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
14
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
15 /**
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
16 * Represents a side-effect.
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
17 *
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
18 * @version %build.number%
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
19 */
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
20 public class Effect {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
21
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
22 private Effect() {}
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
23
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
24 public static P1<Unit> f(Effect0 e) {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
25 return P.lazy(u -> unit());
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
26 }
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
27
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
28 /**
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
29 * Returns a function for the given effect.
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
30 *
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
31 * @return The function using the given effect.
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
32 */
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
33 public static final <A> F<A, Unit> f(Effect1<A> e1) {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
34 return a -> {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
35 e1.f(a);
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
36 return unit();
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
37 };
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
38 }
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
39
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
40 public static <A, B> F2<A, B, Unit> f(Effect2<A, B> e) {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
41 return (a, b) -> {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
42 e.f(a, b);
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
43 return unit();
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
44 };
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
45 }
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
46
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
47 public static <A, B, C> F3<A, B, C, Unit> f(Effect3<A, B, C> e) {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
48 return (a, b, c) -> {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
49 e.f(a, b, c);
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
50 return unit();
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
51 };
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
52 }
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
53
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
54 public static <A, B, C, D> F4<A, B, C, D, Unit> f(Effect4<A, B, C, D> e) {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
55 return (a, b, c, d) -> {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
56 e.f(a, b, c, d);
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
57 return unit();
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
58 };
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
59 }
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
60
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
61 public static <A, B, C, D, E> F5<A, B, C, D, E, Unit> f(Effect5<A, B, C, D, E> z) {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
62 return (a, b, c, d, e) -> {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
63 z.f(a, b, c, d, e);
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
64 return unit();
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
65 };
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
66 }
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
67
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
68 public static <A, B, C, D, E, $F> F6<A, B, C, D, E, $F, Unit> f(Effect6<A, B, C, D, E, $F> z) {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
69 return (a, b, c, d, e, f) -> {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
70 z.f(a, b, c, d, e, f);
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
71 return unit();
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
72 };
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
73 }
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
74
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
75 public static <A, B, C, D, E, $F, G> F7<A, B, C, D, E, $F, G, Unit> f(Effect7<A, B, C, D, E, $F, G> z) {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
76 return (a, b, c, d, e, f, g) -> {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
77 z.f(a, b, c, d, e, f, g);
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
78 return unit();
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
79 };
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
80 }
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
81
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
82 public static <A, B, C, D, E, $F, G, H> F8<A, B, C, D, E, $F, G, H, Unit> f(Effect8<A, B, C, D, E, $F, G, H> z) {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
83 return (a, b, c, d, e, f, g, h) -> {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
84 z.f(a, b, c, d, e, f, g, h);
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
85 return unit();
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
86 };
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
87 }
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
88
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
89 /**
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
90 * A contra-variant functor on effect.
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
91 *
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
92 * @param f The function to map over the effect.
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
93 * @return An effect after a contra-variant map.
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
94 */
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
95 public final <A, B> Effect1<B> comap(Effect1<A> e1, final F<B, A> f) {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
96 return new Effect1<B>() {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
97 public void f(final B b) {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
98 e1.f(f.f(b));
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
99 }
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
100 };
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
101 }
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
102
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
103 public static <A> Effect1<A> lazy(final F<A, Unit> f) {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
104 return new Effect1<A>() {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
105 public void f(final A a) {
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
106 f.f(a);
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
107 }
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
108 };
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
109 }
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
110
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
111 // public static <A> void f(Effect1<A> )
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
112
fe80c1edf1be add getLoop
tatsuki
parents:
diff changeset
113 }