Skip to content

Commit 4a77b0f

Browse files
committed
Merge branch 'realfj-4.0' into fj4.0
Conflicts: .gitignore
2 parents 3b69b24 + d9314d7 commit 4a77b0f

File tree

2 files changed

+18
-62
lines changed

2 files changed

+18
-62
lines changed

core/src/main/java/fj/P1.java

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @version %build.number%
1212
*/
13-
public abstract class P1<A> {
13+
public interface P1<A> {
1414
/**
1515
* Access the first element of the product.
1616
*
@@ -24,12 +24,8 @@ public abstract class P1<A> {
2424
* @param f The function to map with.
2525
* @return A product with the given function applied.
2626
*/
27-
public final <X> P1<X> map(final F<A, X> f) {
28-
return new P1<X>() {
29-
public X _1() {
30-
return f.f(P1.this._1());
31-
}
32-
};
27+
public default <X> P1<X> map(final F<A, X> f) {
28+
return () -> f.f(_1());
3329
}
3430

3531
/**
@@ -38,11 +34,7 @@ public X _1() {
3834
* @return A function that returns the first element of a product.
3935
*/
4036
public static <A> F<P1<A>, A> __1() {
41-
return new F<P1<A>, A>() {
42-
public A f(final P1<A> p) {
43-
return p._1();
44-
}
45-
};
37+
return P1::_1;
4638
}
4739

4840
/**
@@ -52,11 +44,7 @@ public A f(final P1<A> p) {
5244
* @return A function promoted to operate on P1s.
5345
*/
5446
public static <A, B> F<P1<A>, P1<B>> fmap(final F<A, B> f) {
55-
return new F<P1<A>, P1<B>>() {
56-
public P1<B> f(final P1<A> a) {
57-
return a.map(f);
58-
}
59-
};
47+
return a -> a.map(f);
6048
}
6149

6250
/**
@@ -67,11 +55,7 @@ public P1<B> f(final P1<A> a) {
6755
* @return The result of applying the given function to the value of given product-1.
6856
*/
6957
public static <A, B> P1<B> bind(final P1<A> a, final F<A, P1<B>> f) {
70-
return new P1<B>() {
71-
public B _1() {
72-
return f.f(a._1())._1();
73-
}
74-
};
58+
return () -> f.f(a._1())._1();
7559
}
7660

7761
/**
@@ -81,15 +65,7 @@ public B _1() {
8165
* @return A function whose result is wrapped in a P1.
8266
*/
8367
public static <A, B> F<A, P1<B>> curry(final F<A, B> f) {
84-
return new F<A, P1<B>>() {
85-
public P1<B> f(final A a) {
86-
return new P1<B>() {
87-
public B _1() {
88-
return f.f(a);
89-
}
90-
};
91-
}
92-
};
68+
return a -> () -> f.f(a);
9369
}
9470

9571
/**
@@ -100,11 +76,7 @@ public B _1() {
10076
* @return A new P1 after applying the given P1 function to the first argument.
10177
*/
10278
public static <A, B> P1<B> apply(final P1<A> ca, final P1<F<A, B>> cf) {
103-
return bind(cf, new F<F<A, B>, P1<B>>() {
104-
public P1<B> f(final F<A, B> f) {
105-
return fmap(f).f(ca);
106-
}
107-
});
79+
return bind(cf, f -> fmap(f).f(ca));
10880
}
10981

11082
/**
@@ -126,7 +98,7 @@ public static <A, B, C> P1<C> bind(final P1<A> ca, final P1<B> cb, final F<A, F<
12698
* @return A new P1 that is the join of the given P1.
12799
*/
128100
public static <A> P1<A> join(final P1<P1<A>> a) {
129-
return bind(a, Function.<P1<A>>identity());
101+
return bind(a, Function.identity());
130102
}
131103

132104
/**
@@ -136,11 +108,7 @@ public static <A> P1<A> join(final P1<P1<A>> a) {
136108
* @return A function of arity-2 promoted to map over P1s.
137109
*/
138110
public static <A, B, C> F<P1<A>, F<P1<B>, P1<C>>> liftM2(final F<A, F<B, C>> f) {
139-
return Function.curry(new F2<P1<A>, P1<B>, P1<C>>() {
140-
public P1<C> f(final P1<A> pa, final P1<B> pb) {
141-
return bind(pa, pb, f);
142-
}
143-
});
111+
return pa -> pb -> bind(pa, pb, f);
144112
}
145113

146114
/**
@@ -150,7 +118,7 @@ public P1<C> f(final P1<A> pa, final P1<B> pb) {
150118
* @return A single P1 for the given List.
151119
*/
152120
public static <A> P1<List<A>> sequence(final List<P1<A>> as) {
153-
return as.foldRight(liftM2(List.<A>cons()), P.p(List.<A>nil()));
121+
return as.foldRight(liftM2(List.cons()), () -> List.nil());
154122
}
155123

156124
/**
@@ -159,11 +127,7 @@ public static <A> P1<List<A>> sequence(final List<P1<A>> as) {
159127
* @return A function from a List of P1s to a single P1 of a List.
160128
*/
161129
public static <A> F<List<P1<A>>, P1<List<A>>> sequenceList() {
162-
return new F<List<P1<A>>, P1<List<A>>>() {
163-
public P1<List<A>> f(final List<P1<A>> as) {
164-
return sequence(as);
165-
}
166-
};
130+
return as -> sequence(as);
167131
}
168132

169133
/**
@@ -173,7 +137,7 @@ public P1<List<A>> f(final List<P1<A>> as) {
173137
* @return A single P1 for the given stream.
174138
*/
175139
public static <A> P1<Stream<A>> sequence(final Stream<P1<A>> as) {
176-
return as.foldRight(liftM2(Stream.<A>cons()), P.p(Stream.<A>nil()));
140+
return as.foldRight(liftM2(Stream.cons()), () -> Stream.nil());
177141
}
178142

179143
/**
@@ -183,19 +147,15 @@ public static <A> P1<Stream<A>> sequence(final Stream<P1<A>> as) {
183147
* @return A single P1 for the given array.
184148
*/
185149
public static <A> P1<Array<A>> sequence(final Array<P1<A>> as) {
186-
return new P1<Array<A>>() {
187-
public Array<A> _1() {
188-
return as.map(P1.<A>__1());
189-
}
190-
};
150+
return () -> as.map(P1::_1);
191151
}
192152

193153
/**
194154
* Provides a memoising P1 that remembers its value.
195155
*
196156
* @return A P1 that calls this P1 once and remembers the value for subsequent calls.
197157
*/
198-
public final P1<A> memo() {
158+
public default P1<A> memo() {
199159
final P1<A> self = this;
200160
return new P1<A>() {
201161
private final Object latch = new Object();
@@ -220,11 +180,7 @@ public A _1() {
220180
*
221181
* @return A constant function that always uses this value.
222182
*/
223-
public final <B> F<B, A> constant() {
224-
return new F<B, A>() {
225-
public A f(final B b) {
226-
return _1();
227-
}
228-
};
183+
public default <B> F<B, A> constant() {
184+
return b -> _1();
229185
}
230186
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* is the identity morphism from B to B.
88
*/
99
@SuppressWarnings({"UnusedDeclaration"})
10-
public final class $<A, B> extends P1<B> {
10+
public final class $<A, B> implements P1<B> {
1111

1212
private final B b;
1313

0 commit comments

Comments
 (0)