Skip to content

Commit 159c4bc

Browse files
committed
Made small improvements to P1
1 parent de8aa1d commit 159c4bc

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,25 @@ public static <A> F<P1<A>, A> __1() {
3939
/**
4040
* Promote any function to a transformation between P1s.
4141
*
42+
* @deprecated As of release 4.5, use {@link #map_}
4243
* @param f A function to promote to a transformation between P1s.
4344
* @return A function promoted to operate on P1s.
4445
*/
4546
public static <A, B> F<P1<A>, P1<B>> fmap(final F<A, B> f) {
46-
return a -> a.map(f);
47+
return map_(f);
4748
}
4849

49-
/**
50+
/**
51+
* Promote any function to a transformation between P1s.
52+
*
53+
* @param f A function to promote to a transformation between P1s.
54+
* @return A function promoted to operate on P1s.
55+
*/
56+
public static <A, B> F<P1<A>, P1<B>> map_(final F<A, B> f) {
57+
return a -> a.map(f);
58+
}
59+
60+
/**
5061
* Binds the given function to the value in a product-1 with a final join.
5162
*
5263
* @param f A function to apply to the value in a product-1.
@@ -89,6 +100,13 @@ public <B, C> P1<C> bind(final P1<B> cb, final F<A, F<B, C>> f) {
89100
return cb.apply(fmap(f).f(this));
90101
}
91102

103+
/**
104+
* Binds the given function to the values in the given P1s with a final join.
105+
*/
106+
public <B, C> P1<C> bind(final P1<B> cb, final F2<A, B, C> f) {
107+
return bind(cb, F2W.lift(f).curry());
108+
}
109+
92110
/**
93111
* Joins a P1 of a P1 with a bind operation.
94112
*
@@ -109,6 +127,10 @@ public static <A, B, C> F<P1<A>, F<P1<B>, P1<C>>> liftM2(final F<A, F<B, C>> f)
109127
return Function.curry((pa, pb) -> pa.bind(pb, f));
110128
}
111129

130+
public <B, C> P1<C> liftM2(P1<B> pb, F2<A, B, C> f) {
131+
return P.lazy(() -> f.f(_1(), pb._1()));
132+
}
133+
112134
/**
113135
* Turns a List of P1s into a single P1 of a List.
114136
*
@@ -138,6 +160,13 @@ public static <A> P1<Stream<A>> sequence(final Stream<P1<A>> as) {
138160
return as.foldRight(liftM2(Stream.<A>cons()), P.p(Stream.<A>nil()));
139161
}
140162

163+
/**
164+
* Turns an optional P1 into a lazy option.
165+
*/
166+
public static <A> P1<Option<A>> sequence(final Option<P1<A>> o) {
167+
return P.lazy(() -> o.map(p -> p._1()));
168+
}
169+
141170
/**
142171
* Turns an array of P1s into a single P1 of an array.
143172
*
@@ -204,7 +233,7 @@ public <B> Stream<P1<B>> traverseStream(final F<A, Stream<B>> f){
204233
* @param f The function to map with.
205234
* @return A product with the given function applied.
206235
*/
207-
public <X> P1<X> map(final F<A, X> f) {
236+
public <B> P1<B> map(final F<A, B> f) {
208237
final P1<A> self = this;
209238
return P.lazy(() -> f.f(self._1()));
210239
}
@@ -234,6 +263,10 @@ static <A> P1<A> memo(F<Unit, A> f) {
234263
return P.lazy(f).memo();
235264
}
236265

266+
static <A> P1<A> memo(F0<A> f) {
267+
return P.lazy(f).memo();
268+
}
269+
237270
static class Memo<A> extends P1<A> {
238271
private final P1<A> self;
239272
private volatile boolean initialized;

0 commit comments

Comments
 (0)