Skip to content

Commit 659f728

Browse files
committed
Converted F2 methods to static methods on F2Functions
1 parent bf1f9e6 commit 659f728

File tree

16 files changed

+419
-352
lines changed

16 files changed

+419
-352
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package fj;
2+
3+
/**
4+
* Created by MarkPerry on 6/04/2014.
5+
*/
6+
public class F1Functions {
7+
}

core/src/main/java/fj/F2.java

Lines changed: 0 additions & 330 deletions
Original file line numberDiff line numberDiff line change
@@ -35,334 +35,4 @@ public interface F2<A, B, C> {
3535
*/
3636
public C f(A a, B b);
3737

38-
39-
/**
40-
* Partial application.
41-
*
42-
* @param a The <code>A</code> to which to apply this function.
43-
* @return The function partially applied to the given argument.
44-
*/
45-
default public F<B, C> f(final A a) {
46-
return new F<B, C>() {
47-
public C f(final B b) {
48-
return F2.this.f(a, b);
49-
}
50-
};
51-
}
52-
53-
/**
54-
* Curries this wrapped function to a wrapped function of arity-1 that returns another wrapped function.
55-
*
56-
* @return a wrapped function of arity-1 that returns another wrapped function.
57-
*/
58-
default public F<A, F<B, C>> curry() {
59-
return new F<A, F<B, C>>() {
60-
public F<B, C> f(final A a) {
61-
return new F<B, C>() {
62-
public C f(final B b) {
63-
return F2.this.f(a, b);
64-
}
65-
};
66-
}
67-
};
68-
}
69-
70-
/**
71-
* Flips the arguments of this function.
72-
*
73-
* @return A new function with the arguments of this function flipped.
74-
*/
75-
default public F2<B, A, C> flip() {
76-
return new F2<B, A, C>() {
77-
public C f(final B b, final A a) {
78-
return F2.this.f(a, b);
79-
}
80-
};
81-
}
82-
83-
/**
84-
* Uncurries this function to a function on tuples.
85-
*
86-
* @return A new function that calls this function with the elements of a given tuple.
87-
*/
88-
default public F<P2<A, B>, C> tuple() {
89-
return new F<P2<A, B>, C>() {
90-
public C f(final P2<A, B> p) {
91-
return F2.this.f(p._1(), p._2());
92-
}
93-
};
94-
}
95-
96-
/**
97-
* Promotes this function to a function on Arrays.
98-
*
99-
* @return This function promoted to transform Arrays.
100-
*/
101-
default public F2<Array<A>, Array<B>, Array<C>> arrayM() {
102-
return new F2<Array<A>, Array<B>, Array<C>>() {
103-
public Array<C> f(final Array<A> a, final Array<B> b) {
104-
return a.bind(b, F2.this.curry());
105-
}
106-
};
107-
}
108-
109-
/**
110-
* Promotes this function to a function on Promises.
111-
*
112-
* @return This function promoted to transform Promises.
113-
*/
114-
default public F2<Promise<A>, Promise<B>, Promise<C>> promiseM() {
115-
return new F2<Promise<A>, Promise<B>, Promise<C>>() {
116-
public Promise<C> f(final Promise<A> a, final Promise<B> b) {
117-
return a.bind(b, F2.this.curry());
118-
}
119-
};
120-
}
121-
122-
/**
123-
* Promotes this function to a function on Iterables.
124-
*
125-
* @return This function promoted to transform Iterables.
126-
*/
127-
default public F2<Iterable<A>, Iterable<B>, IterableW<C>> iterableM() {
128-
return new F2<Iterable<A>, Iterable<B>, IterableW<C>>() {
129-
public IterableW<C> f(final Iterable<A> a, final Iterable<B> b) {
130-
return IterableW.liftM2(F2.this.curry()).f(a).f(b);
131-
}
132-
};
133-
}
134-
135-
/**
136-
* Promotes this function to a function on Lists.
137-
*
138-
* @return This function promoted to transform Lists.
139-
*/
140-
default public F2<List<A>, List<B>, List<C>> listM() {
141-
return new F2<List<A>, List<B>, List<C>>() {
142-
public List<C> f(final List<A> a, final List<B> b) {
143-
return List.liftM2(F2.this.curry()).f(a).f(b);
144-
}
145-
};
146-
}
147-
148-
/**
149-
* Promotes this function to a function on non-empty lists.
150-
*
151-
* @return This function promoted to transform non-empty lists.
152-
*/
153-
default public F2<NonEmptyList<A>, NonEmptyList<B>, NonEmptyList<C>> nelM() {
154-
return new F2<NonEmptyList<A>, NonEmptyList<B>, NonEmptyList<C>>() {
155-
public NonEmptyList<C> f(final NonEmptyList<A> as, final NonEmptyList<B> bs) {
156-
return NonEmptyList.fromList(as.toList().bind(bs.toList(), F2.this)).some();
157-
}
158-
};
159-
}
160-
161-
/**
162-
* Promotes this function to a function on Options.
163-
*
164-
* @return This function promoted to transform Options.
165-
*/
166-
default public F2<Option<A>, Option<B>, Option<C>> optionM() {
167-
return new F2<Option<A>, Option<B>, Option<C>>() {
168-
public Option<C> f(final Option<A> a, final Option<B> b) {
169-
return Option.liftM2(F2.this.curry()).f(a).f(b);
170-
}
171-
};
172-
}
173-
174-
/**
175-
* Promotes this function to a function on Sets.
176-
*
177-
* @param o An ordering for the result of the promoted function.
178-
* @return This function promoted to transform Sets.
179-
*/
180-
default public F2<Set<A>, Set<B>, Set<C>> setM(final Ord<C> o) {
181-
return new F2<Set<A>, Set<B>, Set<C>>() {
182-
public Set<C> f(final Set<A> as, final Set<B> bs) {
183-
Set<C> cs = Set.empty(o);
184-
for (final A a : as)
185-
for (final B b : bs)
186-
cs = cs.insert(F2.this.f(a, b));
187-
return cs;
188-
}
189-
};
190-
}
191-
192-
/**
193-
* Promotes this function to a function on Streams.
194-
*
195-
* @return This function promoted to transform Streams.
196-
*/
197-
default public F2<Stream<A>, Stream<B>, Stream<C>> streamM() {
198-
return new F2<Stream<A>, Stream<B>, Stream<C>>() {
199-
public Stream<C> f(final Stream<A> as, final Stream<B> bs) {
200-
return as.bind(bs, F2.this);
201-
}
202-
};
203-
}
204-
205-
/**
206-
* Promotes this function to a function on Trees.
207-
*
208-
* @return This function promoted to transform Trees.
209-
*/
210-
default public F2<Tree<A>, Tree<B>, Tree<C>> treeM() {
211-
return new F2<Tree<A>, Tree<B>, Tree<C>>() {
212-
public Tree<C> f(final Tree<A> as, final Tree<B> bs) {
213-
final F2<Tree<A>, Tree<B>, Tree<C>> self = this;
214-
return node(F2.this.f(as.root(), bs.root()), new P1<Stream<Tree<C>>>() {
215-
public Stream<Tree<C>> _1() {
216-
return self.streamM().f(as.subForest()._1(), bs.subForest()._1());
217-
}
218-
});
219-
}
220-
};
221-
}
222-
223-
/**
224-
* Promotes this function to zip two arrays, applying the function lock-step over both Arrays.
225-
*
226-
* @return A function that zips two arrays with this function.
227-
*/
228-
default public F2<Array<A>, Array<B>, Array<C>> zipArrayM() {
229-
return new F2<Array<A>, Array<B>, Array<C>>() {
230-
public Array<C> f(final Array<A> as, final Array<B> bs) {
231-
return as.zipWith(bs, F2.this);
232-
}
233-
};
234-
}
235-
236-
/**
237-
* Promotes this function to zip two iterables, applying the function lock-step over both iterables.
238-
*
239-
* @return A function that zips two iterables with this function.
240-
*/
241-
default public F2<Iterable<A>, Iterable<B>, Iterable<C>> zipIterableM() {
242-
return new F2<Iterable<A>, Iterable<B>, Iterable<C>>() {
243-
public Iterable<C> f(final Iterable<A> as, final Iterable<B> bs) {
244-
return wrap(as).zipWith(bs, F2.this);
245-
}
246-
};
247-
}
248-
249-
/**
250-
* Promotes this function to zip two lists, applying the function lock-step over both lists.
251-
*
252-
* @return A function that zips two lists with this function.
253-
*/
254-
default public F2<List<A>, List<B>, List<C>> zipListM() {
255-
return new F2<List<A>, List<B>, List<C>>() {
256-
public List<C> f(final List<A> as, final List<B> bs) {
257-
return as.zipWith(bs, F2.this);
258-
}
259-
};
260-
}
261-
262-
263-
/**
264-
* Promotes this function to zip two streams, applying the function lock-step over both streams.
265-
*
266-
* @return A function that zips two streams with this function.
267-
*/
268-
default public F2<Stream<A>, Stream<B>, Stream<C>> zipStreamM() {
269-
return new F2<Stream<A>, Stream<B>, Stream<C>>() {
270-
public Stream<C> f(final Stream<A> as, final Stream<B> bs) {
271-
return as.zipWith(bs, F2.this);
272-
}
273-
};
274-
}
275-
276-
/**
277-
* Promotes this function to zip two non-empty lists, applying the function lock-step over both lists.
278-
*
279-
* @return A function that zips two non-empty lists with this function.
280-
*/
281-
default public F2<NonEmptyList<A>, NonEmptyList<B>, NonEmptyList<C>> zipNelM() {
282-
return new F2<NonEmptyList<A>, NonEmptyList<B>, NonEmptyList<C>>() {
283-
public NonEmptyList<C> f(final NonEmptyList<A> as, final NonEmptyList<B> bs) {
284-
return NonEmptyList.fromList(as.toList().zipWith(bs.toList(), F2.this)).some();
285-
}
286-
};
287-
}
288-
289-
/**
290-
* Promotes this function to zip two sets, applying the function lock-step over both sets.
291-
*
292-
* @param o An ordering for the resulting set.
293-
* @return A function that zips two sets with this function.
294-
*/
295-
default public F2<Set<A>, Set<B>, Set<C>> zipSetM(final Ord<C> o) {
296-
return new F2<Set<A>, Set<B>, Set<C>>() {
297-
public Set<C> f(final Set<A> as, final Set<B> bs) {
298-
return iterableSet(o, as.toStream().zipWith(bs.toStream(), F2.this));
299-
}
300-
};
301-
}
302-
303-
/**
304-
* Promotes this function to zip two trees, applying the function lock-step over both trees.
305-
* The structure of the resulting tree is the structural intersection of the two trees.
306-
*
307-
* @return A function that zips two trees with this function.
308-
*/
309-
default public F2<Tree<A>, Tree<B>, Tree<C>> zipTreeM() {
310-
return new F2<Tree<A>, Tree<B>, Tree<C>>() {
311-
public Tree<C> f(final Tree<A> ta, final Tree<B> tb) {
312-
final F2<Tree<A>, Tree<B>, Tree<C>> self = this;
313-
return node(F2.this.f(ta.root(), tb.root()), new P1<Stream<Tree<C>>>() {
314-
public Stream<Tree<C>> _1() {
315-
return self.zipStreamM().f(ta.subForest()._1(), tb.subForest()._1());
316-
}
317-
});
318-
}
319-
};
320-
}
321-
322-
/**
323-
* Promotes this function to zip two zippers, applying the function lock-step over both zippers in both directions.
324-
* The structure of the resulting zipper is the structural intersection of the two zippers.
325-
*
326-
* @return A function that zips two zippers with this function.
327-
*/
328-
default public F2<Zipper<A>, Zipper<B>, Zipper<C>> zipZipperM() {
329-
return new F2<Zipper<A>, Zipper<B>, Zipper<C>>() {
330-
@SuppressWarnings({"unchecked"})
331-
public Zipper<C> f(final Zipper<A> ta, final Zipper<B> tb) {
332-
final F2<Stream<A>, Stream<B>, Stream<C>> sf = F2.this.zipStreamM();
333-
return zipper(sf.f(ta.lefts(), tb.lefts()), F2.this.f(ta.focus(), tb.focus()), sf.f(ta.rights(), tb.rights()));
334-
}
335-
};
336-
}
337-
338-
/**
339-
* Promotes this function to zip two TreeZippers, applying the function lock-step over both zippers in all directions.
340-
* The structure of the resulting TreeZipper is the structural intersection of the two TreeZippers.
341-
*
342-
* @return A function that zips two TreeZippers with this function.
343-
*/
344-
default public F2<TreeZipper<A>, TreeZipper<B>, TreeZipper<C>> zipTreeZipperM() {
345-
return new F2<TreeZipper<A>, TreeZipper<B>, TreeZipper<C>>() {
346-
@SuppressWarnings({"unchecked"})
347-
public TreeZipper<C> f(final TreeZipper<A> ta, final TreeZipper<B> tb) {
348-
final F2<Stream<Tree<A>>, Stream<Tree<B>>, Stream<Tree<C>>> sf = F2.this.treeM().zipStreamM();
349-
final
350-
F2<Stream<P3<Stream<Tree<A>>, A, Stream<Tree<A>>>>,
351-
Stream<P3<Stream<Tree<B>>, B, Stream<Tree<B>>>>,
352-
Stream<P3<Stream<Tree<C>>, C, Stream<Tree<C>>>>>
353-
pf =
354-
new F2<P3<Stream<Tree<A>>, A, Stream<Tree<A>>>,
355-
P3<Stream<Tree<B>>, B, Stream<Tree<B>>>,
356-
P3<Stream<Tree<C>>, C, Stream<Tree<C>>>>() {
357-
public P3<Stream<Tree<C>>, C, Stream<Tree<C>>> f(final P3<Stream<Tree<A>>, A, Stream<Tree<A>>> pa,
358-
final P3<Stream<Tree<B>>, B, Stream<Tree<B>>> pb) {
359-
return p(F2.this.treeM().zipStreamM().f(pa._1(), pb._1()), F2.this.f(pa._2(), pb._2()),
360-
F2.this.treeM().zipStreamM().f(pa._3(), pb._3()));
361-
}
362-
}.zipStreamM();
363-
return treeZipper(F2.this.treeM().f(ta.p()._1(), tb.p()._1()), sf.f(ta.lefts(), tb.lefts()),
364-
sf.f(ta.rights(), tb.rights()), pf.f(ta.p()._4(), tb.p()._4()));
365-
}
366-
};
367-
}
36838
}

0 commit comments

Comments
 (0)