Skip to content

Commit c34d3b4

Browse files
author
Mark Perry
committed
#35: Fixed memo function in for artiy 2..8 in Px (e.g. P3)
1 parent 830e22a commit c34d3b4

File tree

9 files changed

+346
-53
lines changed

9 files changed

+346
-53
lines changed

core/src/main/java/fj/P.java

Lines changed: 196 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,202 @@ public A _1() {
3737
};
3838
}
3939

40-
/**
40+
41+
public static <A> P1<A> lazy(final P1<A> pa) {
42+
return pa;
43+
}
44+
45+
public static <A, B> P2<A, B> lazy(final P1<A> pa, final P1<B> pb) {
46+
return new P2<A, B>() {
47+
@Override
48+
public A _1() {
49+
return pa._1();
50+
}
51+
@Override
52+
public B _2() {
53+
return pb._1();
54+
}
55+
};
56+
}
57+
58+
public static <A, B, C> P3<A, B, C> lazy(final P1<A> pa, final P1<B> pb, final P1<C> pc) {
59+
return new P3<A, B, C>() {
60+
@Override
61+
public A _1() {
62+
return pa._1();
63+
}
64+
@Override
65+
public B _2() {
66+
return pb._1();
67+
}
68+
@Override
69+
public C _3() {
70+
return pc._1();
71+
}
72+
};
73+
}
74+
75+
public static <A, B, C, D> P4<A, B, C, D> lazy(final P1<A> pa, final P1<B> pb, final P1<C> pc, final P1<D> pd) {
76+
return new P4<A, B, C, D>() {
77+
@Override
78+
public A _1() {
79+
return pa._1();
80+
}
81+
@Override
82+
public B _2() {
83+
return pb._1();
84+
}
85+
@Override
86+
public C _3() {
87+
return pc._1();
88+
}
89+
90+
@Override
91+
public D _4() {
92+
return pd._1();
93+
}
94+
};
95+
}
96+
97+
public static <A, B, C, D, E> P5<A, B, C, D, E> lazy(final P1<A> pa, final P1<B> pb, final P1<C> pc, final P1<D> pd, P1<E> pe) {
98+
return new P5<A, B, C, D, E>() {
99+
@Override
100+
public A _1() {
101+
return pa._1();
102+
}
103+
@Override
104+
public B _2() {
105+
return pb._1();
106+
}
107+
@Override
108+
public C _3() {
109+
return pc._1();
110+
}
111+
112+
@Override
113+
public D _4() {
114+
return pd._1();
115+
}
116+
117+
@Override
118+
public E _5() {
119+
return pe._1();
120+
}
121+
};
122+
}
123+
124+
public static <A, B, C, D, E, F> P6<A, B, C, D, E, F> lazy(final P1<A> pa, final P1<B> pb, final P1<C> pc, final P1<D> pd, P1<E> pe, P1<F> pf) {
125+
return new P6<A, B, C, D, E, F>() {
126+
@Override
127+
public A _1() {
128+
return pa._1();
129+
}
130+
@Override
131+
public B _2() {
132+
return pb._1();
133+
}
134+
@Override
135+
public C _3() {
136+
return pc._1();
137+
}
138+
139+
@Override
140+
public D _4() {
141+
return pd._1();
142+
}
143+
144+
@Override
145+
public E _5() {
146+
return pe._1();
147+
}
148+
149+
@Override
150+
public F _6() {
151+
return pf._1();
152+
}
153+
};
154+
}
155+
156+
public static <A, B, C, D, E, F, G> P7<A, B, C, D, E, F, G> lazy(final P1<A> pa, final P1<B> pb, final P1<C> pc, final P1<D> pd, P1<E> pe, P1<F> pf, P1<G> pg) {
157+
return new P7<A, B, C, D, E, F, G>() {
158+
@Override
159+
public A _1() {
160+
return pa._1();
161+
}
162+
@Override
163+
public B _2() {
164+
return pb._1();
165+
}
166+
@Override
167+
public C _3() {
168+
return pc._1();
169+
}
170+
171+
@Override
172+
public D _4() {
173+
return pd._1();
174+
}
175+
176+
@Override
177+
public E _5() {
178+
return pe._1();
179+
}
180+
181+
@Override
182+
public F _6() {
183+
return pf._1();
184+
}
185+
186+
@Override
187+
public G _7() {
188+
return pg._1();
189+
}
190+
};
191+
}
192+
193+
public static <A, B, C, D, E, F, G, H> P8<A, B, C, D, E, F, G, H> lazy(final P1<A> pa, final P1<B> pb, final P1<C> pc, final P1<D> pd, P1<E> pe, P1<F> pf, P1<G> pg, P1<H> ph) {
194+
return new P8<A, B, C, D, E, F, G, H>() {
195+
@Override
196+
public A _1() {
197+
return pa._1();
198+
}
199+
@Override
200+
public B _2() {
201+
return pb._1();
202+
}
203+
@Override
204+
public C _3() {
205+
return pc._1();
206+
}
207+
208+
@Override
209+
public D _4() {
210+
return pd._1();
211+
}
212+
213+
@Override
214+
public E _5() {
215+
return pe._1();
216+
}
217+
218+
@Override
219+
public F _6() {
220+
return pf._1();
221+
}
222+
223+
@Override
224+
public G _7() {
225+
return pg._1();
226+
}
227+
228+
@Override
229+
public H _8() {
230+
return ph._1();
231+
}
232+
};
233+
}
234+
235+
/**
41236
* A function that puts an element in a product-2.
42237
*
43238
* @return A function that puts an element in a product-2.

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -180,25 +180,26 @@ public final P1<B> _2_() {
180180
return F1Functions.lazy(P2.<A, B>__2()).f(this);
181181
}
182182

183-
/**
184-
* Provides a memoising P2 that remembers its values.
185-
*
186-
* @return A P2 that calls this P2 once for any given element and remembers the value for subsequent calls.
187-
*/
188-
public final P2<A, B> memo() {
189-
return new P2<A, B>() {
190-
private final P1<A> a = P1Functions.memo(_1_());
191-
private final P1<B> b = P1Functions.memo(_2_());
192-
193-
public A _1() {
194-
return a._1();
195-
}
196-
197-
public B _2() {
198-
return b._1();
199-
}
200-
};
201-
}
183+
/**
184+
* Provides a memoising P2 that remembers its values.
185+
*
186+
* @return A P2 that calls this P2 once for any given element and remembers the value for subsequent calls.
187+
*/
188+
public final P2<A, B> memo() {
189+
P2<A, B> self = this;
190+
return new P2<A, B>() {
191+
private final P1<A> a = P1Functions.memo(() -> self._1());
192+
private final P1<B> b = P1Functions.memo(() -> self._2());
193+
194+
public A _1() {
195+
return a._1();
196+
}
197+
198+
public B _2() {
199+
return b._1();
200+
}
201+
};
202+
}
202203

203204
/**
204205
* A first-class version of the split function.

core/src/main/java/fj/P3.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ public final P1<C> _3_() {
126126
* @return A P3 that calls this P3 once for any given element and remembers the value for subsequent calls.
127127
*/
128128
public final P3<A, B, C> memo() {
129+
P3<A, B, C> self = this;
129130
return new P3<A, B, C>() {
130-
private final P1<A> a = P1Functions.memo(_1_());
131-
private final P1<B> b = P1Functions.memo(_2_());
132-
private final P1<C> c = P1Functions.memo(_3_());
131+
private final P1<A> a = P1Functions.memo(() -> self._1());
132+
private final P1<B> b = P1Functions.memo(() -> self._2());
133+
private final P1<C> c = P1Functions.memo(() -> self._3());
133134

134135
public A _1() {
135136
return a._1();

core/src/main/java/fj/P4.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,12 @@ public final P1<D> _4_() {
180180
* @return A P4 that calls this P4 once for any given element and remembers the value for subsequent calls.
181181
*/
182182
public final P4<A, B, C, D> memo() {
183+
P4<A, B, C, D> self = this;
183184
return new P4<A, B, C, D>() {
184-
private final P1<A> a = P1Functions.memo(_1_());
185-
private final P1<B> b = P1Functions.memo(_2_());
186-
private final P1<C> c = P1Functions.memo(_3_());
187-
private final P1<D> d = P1Functions.memo(_4_());
185+
private final P1<A> a = P1Functions.memo(() -> self._1());
186+
private final P1<B> b = P1Functions.memo(() -> self._2());
187+
private final P1<C> c = P1Functions.memo(() -> self._3());
188+
private final P1<D> d = P1Functions.memo(() -> self._4());
188189

189190
public A _1() {
190191
return a._1();

core/src/main/java/fj/P5.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,13 @@ public final P1<E> _5_() {
242242
* @return A P5 that calls this P5 once for any given element and remembers the value for subsequent calls.
243243
*/
244244
public final P5<A, B, C, D, E> memo() {
245+
P5<A, B, C, D, E> self = this;
245246
return new P5<A, B, C, D, E>() {
246-
private final P1<A> a = P1Functions.memo(_1_());
247-
private final P1<B> b = P1Functions.memo(_2_());
248-
private final P1<C> c = P1Functions.memo(_3_());
249-
private final P1<D> d = P1Functions.memo(_4_());
250-
private final P1<E> e = P1Functions.memo(_5_());
247+
private final P1<A> a = P1Functions.memo(() -> self._1());
248+
private final P1<B> b = P1Functions.memo(() -> self._2());
249+
private final P1<C> c = P1Functions.memo(() -> self._3());
250+
private final P1<D> d = P1Functions.memo(() -> self._4());
251+
private final P1<E> e = P1Functions.memo(() -> self._5());
251252

252253
public A _1() {
253254
return a._1();

core/src/main/java/fj/P6.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,14 @@ public final P1<F> _6_() {
313313
* @return A P6 that calls this P6 once for any given element and remembers the value for subsequent calls.
314314
*/
315315
public final P6<A, B, C, D, E, F> memo() {
316+
P6<A, B, C, D, E, F> self = this;
316317
return new P6<A, B, C, D, E, F>() {
317-
private final P1<A> a = P1Functions.memo(_1_());
318-
private final P1<B> b = P1Functions.memo(_2_());
319-
private final P1<C> c = P1Functions.memo(_3_());
320-
private final P1<D> d = P1Functions.memo(_4_());
321-
private final P1<E> e = P1Functions.memo(_5_());
322-
private final P1<F> f = P1Functions.memo(_6_());
318+
private final P1<A> a = P1Functions.memo(() -> self._1());
319+
private final P1<B> b = P1Functions.memo(() -> self._2());
320+
private final P1<C> c = P1Functions.memo(() -> self._3());
321+
private final P1<D> d = P1Functions.memo(() -> self._4());
322+
private final P1<E> e = P1Functions.memo(() -> self._5());
323+
private final P1<F> f = P1Functions.memo(() -> self._6());
323324

324325
public A _1() {
325326
return a._1();

core/src/main/java/fj/P7.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,15 @@ public final P1<G> _7_() {
391391
* @return A P7 that calls this P7 once for any given element and remembers the value for subsequent calls.
392392
*/
393393
public final P7<A, B, C, D, E, F, G> memo() {
394+
P7<A, B, C, D, E, F, G> self = this;
394395
return new P7<A, B, C, D, E, F, G>() {
395-
private final P1<A> a = P1Functions.memo(_1_());
396-
private final P1<B> b = P1Functions.memo(_2_());
397-
private final P1<C> c = P1Functions.memo(_3_());
398-
private final P1<D> d = P1Functions.memo(_4_());
399-
private final P1<E> e = P1Functions.memo(_5_());
400-
private final P1<F> f = P1Functions.memo(_6_());
401-
private final P1<G> g = P1Functions.memo(_7_());
396+
private final P1<A> a = P1Functions.memo(() -> self._1());
397+
private final P1<B> b = P1Functions.memo(() -> self._2());
398+
private final P1<C> c = P1Functions.memo(() -> self._3());
399+
private final P1<D> d = P1Functions.memo(() -> self._4());
400+
private final P1<E> e = P1Functions.memo(() -> self._5());
401+
private final P1<F> f = P1Functions.memo(() -> self._6());
402+
private final P1<G> g = P1Functions.memo(() -> self._7());
402403

403404
public A _1() {
404405
return a._1();

core/src/main/java/fj/P8.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -478,15 +478,16 @@ public final P1<H> _8_() {
478478
* @return A P8 that calls this P8 once for any given element and remembers the value for subsequent calls.
479479
*/
480480
public final P8<A, B, C, D, E, F, G, H> memo() {
481+
P8<A, B, C, D, E, F, G, H> self = this;
481482
return new P8<A, B, C, D, E, F, G, H>() {
482-
private final P1<A> a = P1Functions.memo(_1_());
483-
private final P1<B> b = P1Functions.memo(_2_());
484-
private final P1<C> c = P1Functions.memo(_3_());
485-
private final P1<D> d = P1Functions.memo(_4_());
486-
private final P1<E> e = P1Functions.memo(_5_());
487-
private final P1<F> f = P1Functions.memo(_6_());
488-
private final P1<G> g = P1Functions.memo(_7_());
489-
private final P1<H> h = P1Functions.memo(_8_());
483+
private final P1<A> a = P1Functions.memo(() -> self._1());
484+
private final P1<B> b = P1Functions.memo(() -> self._2());
485+
private final P1<C> c = P1Functions.memo(() -> self._3());
486+
private final P1<D> d = P1Functions.memo(() -> self._4());
487+
private final P1<E> e = P1Functions.memo(() -> self._5());
488+
private final P1<F> f = P1Functions.memo(() -> self._6());
489+
private final P1<G> g = P1Functions.memo(() -> self._7());
490+
private final P1<H> h = P1Functions.memo(() -> self._8());
490491

491492
public A _1() {
492493
return a._1();

0 commit comments

Comments
 (0)