Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
de6694b
Adding `traverse` to option type
amarpotghan Jan 12, 2015
d92161a
Refactoring map2 to change the order of params
amarpotghan Jan 12, 2015
221e26f
moving list traversal to List, removed from the Option
amarpotghan Jan 13, 2015
c21ba2b
test names starts with lowercase
amarpotghan Jan 13, 2015
d0eb40b
conforming naming to as @mperry suggested
amarpotghan Jan 13, 2015
50e9122
replacing explicit recursion using foldr
amarpotghan Jan 13, 2015
f3cab03
Adding travereseEitherRight to List type
amarpotghan Jan 13, 2015
a4d7c05
Renaming `traverseEitherRight` to `traverseEither`
amarpotghan Jan 13, 2015
6a683e5
Merge remote-tracking branch 'upstream/master'
amarpotghan Jan 14, 2015
a436a4e
Adding `traverseIO` and `traverseList` for `Either` type
amarpotghan Jan 16, 2015
31fbbbe
Adding `traverse` for `P2` type
amarpotghan Jan 16, 2015
db09a08
Adding `traverse` for `Validation` type
amarpotghan Jan 16, 2015
8d48312
Resolving conflicts
amarpotghan Jan 16, 2015
7cf84dd
removing ipr file.
amarpotghan Jan 16, 2015
35aae2e
removing `List` traversal from IOFunctions. It's already moved to `List`
amarpotghan Jan 16, 2015
b4b27be
Adding traversables for `P1`
amarpotghan Jan 19, 2015
546dc74
correcting copy paste mistakes :)
amarpotghan Jan 20, 2015
dc64154
Merge remote-tracking branch 'upstream/master'
amarpotghan Jan 21, 2015
19245d5
adding translators for right and left traversals in Either
amarpotghan Jan 21, 2015
3879302
inlining map2x
amarpotghan Jan 21, 2015
3f724a3
correcting javadoc
amarpotghan Jan 22, 2015
a5a557b
correcting javadoc
amarpotghan Jan 22, 2015
8343962
correcting javadoc
amarpotghan Jan 22, 2015
b304afa
correcting javadoc
amarpotghan Jan 22, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 66 additions & 62 deletions core/src/main/java/fj/P1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import java.lang.ref.SoftReference;

import fj.data.Array;
import fj.data.List;
import fj.data.Stream;
import fj.data.Validation;
import fj.data.*;
import fj.function.Try0;

public abstract class P1<A> {
Expand All @@ -23,11 +20,7 @@ public abstract class P1<A> {
* @return A function that returns the first element of a product.
*/
public static <A> F<P1<A>, A> __1() {
return new F<P1<A>, A>() {
public A f(final P1<A> p) {
return p._1();
}
};
return p -> p._1();
}

/**
Expand All @@ -37,11 +30,7 @@ public A f(final P1<A> p) {
* @return A function promoted to operate on P1s.
*/
public static <A, B> F<P1<A>, P1<B>> fmap(final F<A, B> f) {
return new F<P1<A>, P1<B>>() {
public P1<B> f(final P1<A> a) {
return a.map(f);
}
};
return a -> a.map(f);
}

/**
Expand All @@ -52,11 +41,7 @@ public P1<B> f(final P1<A> a) {
*/
public <B> P1<B> bind(final F<A, P1<B>> f) {
P1<A> self = this;
return new P1<B>() {
public B _1() {
return f.f(self._1())._1();
}
};
return P.lazy(u -> f.f(self._1())._1());
}

/**
Expand All @@ -66,15 +51,7 @@ public B _1() {
* @return A function whose result is wrapped in a P1.
*/
public static <A, B> F<A, P1<B>> curry(final F<A, B> f) {
return new F<A, P1<B>>() {
public P1<B> f(final A a) {
return new P1<B>() {
public B _1() {
return f.f(a);
}
};
}
};
return a -> P.lazy(u -> f.f(a));
}

/**
Expand All @@ -85,11 +62,7 @@ public B _1() {
*/
public <B> P1<B> apply(final P1<F<A, B>> cf) {
P1<A> self = this;
return cf.bind(new F<F<A, B>, P1<B>>() {
public P1<B> f(final F<A, B> f) {
return fmap(f).f(self);
}
});
return cf.bind(f -> fmap(f).f(self));
}

/**
Expand Down Expand Up @@ -120,11 +93,7 @@ public static <A> P1<A> join(final P1<P1<A>> a) {
* @return A function of arity-2 promoted to map over P1s.
*/
public static <A, B, C> F<P1<A>, F<P1<B>, P1<C>>> liftM2(final F<A, F<B, C>> f) {
return Function.curry(new F2<P1<A>, P1<B>, P1<C>>() {
public P1<C> f(final P1<A> pa, final P1<B> pb) {
return pa.bind(pb, f);
}
});
return Function.curry((pa, pb) -> pa.bind(pb, f));
}

/**
Expand All @@ -143,11 +112,7 @@ public static <A> P1<List<A>> sequence(final List<P1<A>> as) {
* @return A function from a List of P1s to a single P1 of a List.
*/
public static <A> F<List<P1<A>>, P1<List<A>>> sequenceList() {
return new F<List<P1<A>>, P1<List<A>>>() {
public P1<List<A>> f(final List<P1<A>> as) {
return sequence(as);
}
};
return as -> sequence(as);
}

/**
Expand All @@ -167,11 +132,57 @@ public static <A> P1<Stream<A>> sequence(final Stream<P1<A>> as) {
* @return A single P1 for the given array.
*/
public static <A> P1<Array<A>> sequence(final Array<P1<A>> as) {
return new P1<Array<A>>() {
public Array<A> _1() {
return as.map(P1.<A>__1());
}
};
return P.lazy(u -> as.map(P1.<A>__1()));
}

/**
* Traversable instance of P1 for List
*
* @param f The function that takes A and produces a List<B> (non-deterministic result)
* @return A List of P1<B>
*/
public <B> List<P1<B>> traverseList(final F<A, List<B>> f){
return f.f(_1()).map(b -> P.p(b));
}

/**
* Traversable instance of P1 for Either
*
* @param f The function produces Either
* @return An Either of P1<B>
*/
public <B, X> Either<X, P1<B>> traverseEither(final F<A, Either<X, B>> f){
return f.f(_1()).right().map(b -> P.p(b));
}

/**
* Traversable instance of P1 for Option
*
* @param f The function that produces Option
* @return An Option of P1<B>
*/
public <B> Option<P1<B>> traverseOption(final F<A, Option<B>> f){
return f.f(_1()).map(b -> P.p(b));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have copy and pasted and forgotten to change the signature to include option here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops..

/**
* Traversable instance of P1 for Validation
*
* @param f The function might produces Validation
* @return An Validation of P1<B>
*/
public <B, E> Validation<E, P1<B>> traverseValidation(final F<A, Validation<E, B>> f){
return f.f(_1()).map(b -> P.p(b));
}

/**
* Traversable instance of P1 for Stream
*
* @param f The function that produces Stream
* @return An Stream of P1<B>
*/
public <B> Stream<P1<B>> traverseStream(final F<A, Stream<B>> f){
return f.f(_1()).map(b -> P.p(b));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you have forgotten to update this for a stream.

/**
Expand All @@ -182,11 +193,7 @@ public Array<A> _1() {
*/
public <X> P1<X> map(final F<A, X> f) {
final P1<A> self = this;
return new P1<X>() {
public X _1() {
return f.f(self._1());
}
};
return P.lazy(u -> f.f(self._1()));
}

/**
Expand All @@ -195,12 +202,12 @@ public X _1() {
* @return A P1 that calls this P1 once and remembers the value for subsequent calls.
*/
public P1<A> memo() {
final P1<A> self = this;
final P1<A> self = this;
return new P1<A>() {
private final Object latch = new Object();
@SuppressWarnings({"InstanceVariableMayNotBeInitialized"})
private volatile SoftReference<A> v;

public A _1() {
A a = v != null ? v.get() : null;
if (a == null)
Expand All @@ -225,22 +232,19 @@ static <A> P1<A> memo(F<Unit, A> f) {
*/
public <B> F<B, A> constant() {

return new F<B, A>() {
public A f(final B b) {
return P1.this._1();
}
};
return b -> P1.this._1();
}

@Override
public String toString() {
return Show.p1Show(Show.<A>anyShow()).showS(this);
}

@SuppressWarnings("unchecked")
@Override
public boolean equals(Object other) {
return !Equal.equalsValidationCheck(this, other) ? false :
Equal.p1Equal(Equal.<A>anyEqual()).eq(this, (P1<A>) other);
return Equal.equalsValidationCheck(this, other)
&& Equal.p1Equal(Equal.<A>anyEqual()).eq(this, (P1<A>) other);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for point this out. In the future, please do this as part of a different change request. I did this change in lots of places, so this change should be applied for most of those classes implementing equals.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure @mperry . I thought that is too minor to have a separate PR. Affirmative to the point of tracking things separately.

}

@Override
Expand Down
72 changes: 30 additions & 42 deletions core/src/main/java/fj/P2.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package fj;

import static fj.Function.*;
import fj.data.List;
import fj.data.Stream;

import fj.data.*;

/**
* A product-2.
Expand Down Expand Up @@ -156,6 +156,26 @@ public final <C> List<C> sequenceW(final List<F<P2<A, B>, C>> fs) {
return cs.toList();
}

public final <C> List<P2<A, C>> traverseList(final F<B, List<C>> f) {
return f.f(_2()).map(x -> P.p(_1(), x));
}

public final <C> Stream<P2<A, C>> traverseStream(final F<B, Stream<C>> f) {
return f.f(_2()).map(x -> P.p(_1(), x));
}

public final <C> IO<P2<A, C>> traverseIO(final F<B, IO<C>> f) {
return IOFunctions.map(f.f(_2()), x -> P.p(_1(), x));
}

public final <C> Option<P2<A, C>> traverseOption(final F<B, Option<C>> f) {
return f.f(_2()).map(x -> P.p(_1(), x));
}

public final <C, X> Either<X, P2<A, C>> traverseEither(final F<B, Either<X, C>> f) {
return f.f(_2()).right().map(x -> P.p(_1(), x));
}

/**
* Applies a stream of comonadic functions to this product, returning a stream of values.
*
Expand Down Expand Up @@ -219,11 +239,7 @@ public B _2() {
* @return A function that splits a given product between the two given functions and combines their output.
*/
public static <A, B, C, D> F<P2<A, B>, P2<C, D>> split_(final F<A, C> f, final F<B, D> g) {
return new F<P2<A, B>, P2<C, D>>() {
public P2<C, D> f(final P2<A, B> p) {
return p.split(f, g);
}
};
return p -> p.split(f, g);
}

/**
Expand All @@ -233,11 +249,7 @@ public P2<C, D> f(final P2<A, B> p) {
* @return The given function, promoted to map the first element of products.
*/
public static <A, B, X> F<P2<A, B>, P2<X, B>> map1_(final F<A, X> f) {
return new F<P2<A, B>, P2<X, B>>() {
public P2<X, B> f(final P2<A, B> p) {
return p.map1(f);
}
};
return p -> p.map1(f);
}

/**
Expand All @@ -247,11 +259,7 @@ public P2<X, B> f(final P2<A, B> p) {
* @return The given function, promoted to map the second element of products.
*/
public static <A, B, X> F<P2<A, B>, P2<A, X>> map2_(final F<B, X> f) {
return new F<P2<A, B>, P2<A, X>>() {
public P2<A, X> f(final P2<A, B> p) {
return p.map2(f);
}
};
return p -> p.map2(f);
}

/**
Expand Down Expand Up @@ -283,11 +291,7 @@ public static <A, B> P2<B, B> map(final F<A, B> f, final P2<A, A> p) {
* @return A curried form of {@link #swap()}.
*/
public static <A, B> F<P2<A, B>, P2<B, A>> swap_() {
return new F<P2<A, B>, P2<B, A>>() {
public P2<B, A> f(final P2<A, B> p) {
return p.swap();
}
};
return p -> p.swap();
}

/**
Expand All @@ -296,11 +300,7 @@ public P2<B, A> f(final P2<A, B> p) {
* @return A function that returns the first element of a product.
*/
public static <A, B> F<P2<A, B>, A> __1() {
return new F<P2<A, B>, A>() {
public A f(final P2<A, B> p) {
return p._1();
}
};
return p -> p._1();
}

/**
Expand All @@ -309,11 +309,7 @@ public A f(final P2<A, B> p) {
* @return A function that returns the second element of a product.
*/
public static <A, B> F<P2<A, B>, B> __2() {
return new F<P2<A, B>, B>() {
public B f(final P2<A, B> p) {
return p._2();
}
};
return p -> p._2();
}

/**
Expand All @@ -323,11 +319,7 @@ public B f(final P2<A, B> p) {
* @return The function, transformed to operate on on a product-2
*/
public static <A, B, C> F<P2<A, B>, C> tuple(final F<A, F<B, C>> f) {
return new F<P2<A, B>, C>() {
public C f(final P2<A, B> p) {
return f.f(p._1()).f(p._2());
}
};
return p -> f.f(p._1()).f(p._2());
}

/**
Expand All @@ -347,11 +339,7 @@ public static <A, B, C> F<P2<A, B>, C> tuple(final F2<A, B, C> f) {
* @return The function, transformed to an uncurried function of arity-2.
*/
public static <A, B, C> F2<A, B, C> untuple(final F<P2<A, B>, C> f) {
return new F2<A, B, C>() {
public C f(final A a, final B b) {
return f.f(P.p(a, b));
}
};
return (a, b) -> f.f(P.p(a, b));
}

@Override
Expand Down
Loading