Skip to content

Commit d7d02f3

Browse files
committed
Added function 2 wrapper
1 parent 0ca6e3e commit d7d02f3

File tree

1 file changed

+255
-1
lines changed

1 file changed

+255
-1
lines changed

core/src/main/java/fj/F2W.java

Lines changed: 255 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,261 @@
11
package fj;
22

3+
import fj.control.parallel.Promise;
4+
import fj.data.*;
5+
6+
import static fj.P.p;
7+
import static fj.data.IterableW.wrap;
8+
import static fj.data.Set.iterableSet;
9+
import static fj.data.Tree.node;
10+
import static fj.data.TreeZipper.treeZipper;
11+
import static fj.data.Zipper.zipper;
12+
313
/**
414
* Created by MarkPerry on 22/01/2015.
515
*/
6-
public abstract class F2W implements F2 {
16+
public abstract class F2W<A, B, C> implements F2<A, B, C> {
17+
18+
/**
19+
* Partial application.
20+
*
21+
* @param a The <code>A</code> to which to apply this function.
22+
* @return The function partially applied to the given argument.
23+
*/
24+
public F1W<B, C> f(final A a) {
25+
return F1W.lift(F2Functions.f(this, a));
26+
}
27+
28+
/**
29+
* Curries this wrapped function to a wrapped function of arity-1 that returns another wrapped function.
30+
*
31+
* @return a wrapped function of arity-1 that returns another wrapped function.
32+
*/
33+
public F1W<A, F<B, C>> curry() {
34+
return F1W.lift(F2Functions.curry(this));
35+
}
36+
37+
/**
38+
* Flips the arguments of this function.
39+
*
40+
* @return A new function with the arguments of this function flipped.
41+
*/
42+
public F2W<B, A, C> flip() {
43+
return lift(F2Functions.flip(this));
44+
}
45+
46+
/**
47+
* Uncurries this function to a function on tuples.
48+
*
49+
* @return A new function that calls this function with the elements of a given tuple.
50+
*/
51+
public F1W<P2<A, B>, C> tuple() {
52+
return F1W.lift(F2Functions.tuple(this));
53+
}
54+
55+
/**
56+
* Promotes this function to a function on Arrays.
57+
*
58+
* @return This function promoted to transform Arrays.
59+
*/
60+
public F2W<Array<A>, Array<B>, Array<C>> arrayM() {
61+
return lift(F2Functions.arrayM(this));
62+
}
63+
64+
/**
65+
* Promotes this function to a function on Promises.
66+
*
67+
* @return This function promoted to transform Promises.
68+
*/
69+
public F2W<Promise<A>, Promise<B>, Promise<C>> promiseM() {
70+
return lift(F2Functions.promiseM(this));
71+
}
72+
73+
/**
74+
* Promotes this function to a function on Iterables.
75+
*
76+
* @return This function promoted to transform Iterables.
77+
*/
78+
public F2W<Iterable<A>, Iterable<B>, IterableW<C>> iterableM() {
79+
return lift(F2Functions.iterableM(this));
80+
}
81+
82+
/**
83+
* Promotes this function to a function on Lists.
84+
*
85+
* @return This function promoted to transform Lists.
86+
*/
87+
public F2W<List<A>, List<B>, List<C>> listM() {
88+
return lift(F2Functions.listM(this));
89+
}
90+
91+
/**
92+
* Promotes this function to a function on non-empty lists.
93+
*
94+
* @return This function promoted to transform non-empty lists.
95+
*/
96+
public F2W<NonEmptyList<A>, NonEmptyList<B>, NonEmptyList<C>> nelM() {
97+
return lift(F2Functions.nelM(this));
98+
}
99+
100+
/**
101+
* Promotes this function to a function on Options.
102+
*
103+
* @return This function promoted to transform Options.
104+
*/
105+
public F2W<Option<A>, Option<B>, Option<C>> optionM() {
106+
return lift(F2Functions.optionM(this));
107+
}
108+
109+
/**
110+
* Promotes this function to a function on Sets.
111+
*
112+
* @param o An ordering for the result of the promoted function.
113+
* @return This function promoted to transform Sets.
114+
*/
115+
public F2W<Set<A>, Set<B>, Set<C>> setM(final Ord<C> o) {
116+
return lift(F2Functions.setM(this, o));
117+
}
118+
119+
/**
120+
* Promotes this function to a function on Streams.
121+
*
122+
* @return This function promoted to transform Streams.
123+
*/
124+
public F2W<Stream<A>, Stream<B>, Stream<C>> streamM() {
125+
return lift(F2Functions.streamM(this));
126+
}
127+
128+
/**
129+
* Promotes this function to a function on Trees.
130+
*
131+
* @return This function promoted to transform Trees.
132+
*/
133+
public F2W<Tree<A>, Tree<B>, Tree<C>> treeM() {
134+
return lift(F2Functions.treeM(this));
135+
}
136+
137+
/**
138+
* Promotes this function to zip two arrays, applying the function lock-step over both Arrays.
139+
*
140+
* @return A function that zips two arrays with this function.
141+
*/
142+
public F2W<Array<A>, Array<B>, Array<C>> zipArrayM() {
143+
return lift(F2Functions.zipArrayM(this));
144+
}
145+
146+
/**
147+
* Promotes this function to zip two iterables, applying the function lock-step over both iterables.
148+
*
149+
* @return A function that zips two iterables with this function.
150+
*/
151+
public F2W<Iterable<A>, Iterable<B>, Iterable<C>> zipIterableM() {
152+
return lift(F2Functions.zipIterableM(this));
153+
}
154+
155+
/**
156+
* Promotes this function to zip two lists, applying the function lock-step over both lists.
157+
*
158+
* @return A function that zips two lists with this function.
159+
*/
160+
public F2W<List<A>, List<B>, List<C>> zipListM() {
161+
return lift(F2Functions.zipListM(this));
162+
}
163+
164+
165+
/**
166+
* Promotes this function to zip two streams, applying the function lock-step over both streams.
167+
*
168+
* @return A function that zips two streams with this function.
169+
*/
170+
public F2W<Stream<A>, Stream<B>, Stream<C>> zipStreamM() {
171+
return lift(F2Functions.zipStreamM(this));
172+
}
173+
174+
/**
175+
* Promotes this function to zip two non-empty lists, applying the function lock-step over both lists.
176+
*
177+
* @return A function that zips two non-empty lists with this function.
178+
*/
179+
public F2W<NonEmptyList<A>, NonEmptyList<B>, NonEmptyList<C>> zipNelM() {
180+
return lift(F2Functions.zipNelM(this));
181+
}
182+
183+
/**
184+
* Promotes this function to zip two sets, applying the function lock-step over both sets.
185+
*
186+
* @param o An ordering for the resulting set.
187+
* @return A function that zips two sets with this function.
188+
*/
189+
public F2W<Set<A>, Set<B>, Set<C>> zipSetM(final Ord<C> o) {
190+
return lift(F2Functions.zipSetM(this, o));
191+
}
192+
193+
/**
194+
* Promotes this function to zip two trees, applying the function lock-step over both trees.
195+
* The structure of the resulting tree is the structural intersection of the two trees.
196+
*
197+
* @return A function that zips two trees with this function.
198+
*/
199+
public F2W<Tree<A>, Tree<B>, Tree<C>> zipTreeM() {
200+
return lift(F2Functions.zipTreeM(this));
201+
}
202+
203+
/**
204+
* Promotes this function to zip two zippers, applying the function lock-step over both zippers in both directions.
205+
* The structure of the resulting zipper is the structural intersection of the two zippers.
206+
*
207+
* @return A function that zips two zippers with this function.
208+
*/
209+
public F2W<Zipper<A>, Zipper<B>, Zipper<C>> zipZipperM() {
210+
return lift(F2Functions.zipZipperM(this));
211+
}
212+
213+
/**
214+
* Promotes this function to zip two TreeZippers, applying the function lock-step over both zippers in all directions.
215+
* The structure of the resulting TreeZipper is the structural intersection of the two TreeZippers.
216+
*
217+
* @return A function that zips two TreeZippers with this function.
218+
*/
219+
public F2W<TreeZipper<A>, TreeZipper<B>, TreeZipper<C>> zipTreeZipperM() {
220+
return lift(F2Functions.zipTreeZipperM(this));
221+
}
222+
223+
public <Z> F2W<Z, B, C> contramapFirst(F<Z, A> f) {
224+
return lift(F2Functions.contramapFirst(this, f));
225+
}
226+
227+
public <Z> F2W<A, Z, C> contramapSecond(F<Z, B> f) {
228+
return lift(F2Functions.contramapSecond(this, f));
229+
}
230+
231+
public <X, Y> F2W<X, Y, C> contramap(F<X, A> f, F<Y, B> g) {
232+
return lift(F2Functions.contramap(this, f, g));
233+
}
234+
235+
public <Z> F2W<A, B, Z> map(F<C, Z> f) {
236+
return lift(F2Functions.map(this, f));
237+
}
238+
239+
240+
public static class F2WFunc<A, B, C> extends F2W<A, B, C> {
241+
final F2<A, B, C> func;
242+
public F2WFunc(F2<A, B, C> f) {
243+
func = f;
244+
}
245+
246+
@Override
247+
public C f(A a, B b) {
248+
return func.f(a, b);
249+
}
250+
}
251+
252+
/**
253+
* Lifts the function into the fully featured function wrapper
254+
*/
255+
public static <A, B, C> F2W<A, B, C> lift(final F2<A, B, C> f) {
256+
return new F2WFunc<>(f);
257+
}
258+
259+
260+
7261
}

0 commit comments

Comments
 (0)