Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
65 changes: 39 additions & 26 deletions core/src/main/java/fj/P2.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,37 +344,50 @@ public static <A, B, C> F2<A, B, C> untuple(final F<P2<A, B>, C> f) {
return (a, b) -> f.f(P.p(a, b));
}

/**
* Polyomorphic lens targeted on _1.
*/
public static <A, B, C> PLens<P2<A, B>, P2<C, B>, A, C> _1pLens() {
return pLens(__1(), a -> p2 -> P.p(a, p2._2()));
}

/**
* Monomorphic lens targeted on _1.
*/
public static <A, B, C> Lens<P2<A, B>, A> _1Lens() {
return new Lens<>(_1pLens());
}
@Override
public String toString() {
return Show.p2Show(Show.<A>anyShow(), Show.<B>anyShow()).showS(this);
}

/**
* Polyomorphic lens targeted on _2.
*/
public static <A, B, C> PLens<P2<A, B>, P2<A, C>, B, C> _2pLens() {
return pLens(__2(), b -> p2 -> P.p(p2._1(), b));
}
* Optic factory methods for a P2

/**
* Monomorphic lens targeted on _1.
*/
public static <A, B, C> Lens<P2<A, B>, B> _2Lens() {
return new Lens<>(_2pLens());
}
public static final class Optic {

@Override
public String toString() {
return Show.p2Show(Show.<A>anyShow(), Show.<B>anyShow()).showS(this);
}
private Optic() {
throw new UnsupportedOperationException();
}

/**
* Polyomorphic lens targeted on _1.
*/
public static <A, B, C> PLens<P2<A, B>, P2<C, B>, A, C> _1p() {
return pLens(__1(), a -> p2 -> P.p(a, p2._2()));
}

/**
* Monomorphic lens targeted on _1.
*/
public static <A, B, C> Lens<P2<A, B>, A> _1() {
return new Lens<>(_1p());
}

/**
* Polyomorphic lens targeted on _2.
*/
public static <A, B, C> PLens<P2<A, B>, P2<A, C>, B, C> _2p() {
return pLens(__2(), b -> p2 -> P.p(p2._1(), b));
}

/**
* Monomorphic lens targeted on _1.
*/
public static <A, B, C> Lens<P2<A, B>, B> _2() {
return new Lens<>(_2p());
}

}

}
219 changes: 116 additions & 103 deletions core/src/main/java/fj/data/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -655,81 +655,6 @@ public <B> V2<List<B>> traverseV2(final F<A, V2<B>> f) {
v(List.<B> nil(), List.<B> nil()));
}

/**
* polymorphic traversal
*/
public static <A, B> PTraversal<List<A>, List<B>, A, B> _pTraversal() {
return new PTraversal<List<A>, List<B>, A, B>() {

@Override
public <C> F<List<A>, F<C, List<B>>> modifyFunctionF(F<A, F<C, B>> f) {
return l -> l.traverseF(f);
}

@Override
public <L> F<List<A>, Either<L, List<B>>> modifyEitherF(F<A, Either<L, B>> f) {
return l -> l.traverseEither(f);
}

@Override
public F<List<A>, IO<List<B>>> modifyIOF(F<A, IO<B>> f) {
return l -> l.traverseIO(f);
}

@Override
public F<List<A>, Trampoline<List<B>>> modifyTrampolineF(F<A, Trampoline<B>> f) {
return l -> l.traverseTrampoline(f);
}

@Override
public F<List<A>, Promise<List<B>>> modifyPromiseF(F<A, Promise<B>> f) {
return l -> l.traversePromise(f);
}

@Override
public F<List<A>, List<List<B>>> modifyListF(F<A, List<B>> f) {
return l -> l.traverseList(f);
}

@Override
public F<List<A>, Option<List<B>>> modifyOptionF(F<A, Option<B>> f) {
return l -> l.traverseOption(f);
}

@Override
public F<List<A>, Stream<List<B>>> modifyStreamF(F<A, Stream<B>> f) {
return l -> l.traverseStream(f);
}

@Override
public F<List<A>, P1<List<B>>> modifyP1F(F<A, P1<B>> f) {
return l -> l.traverseP1(f);
}

@Override
public <E> F<List<A>, Validation<E, List<B>>> modifyValidationF(F<A, Validation<E, B>> f) {
return l -> l.traverseValidation(f);
}

@Override
public F<List<A>, V2<List<B>>> modifyV2F(F<A, V2<B>> f) {
return l -> l.traverseV2(f);
}

@Override
public <M> F<List<A>, M> foldMap(Monoid<M> monoid, F<A, M> f) {
return l -> monoid.sumLeft(l.map(f));
}
};
}

/**
* monomorphic traversal
*/
public static <A> Traversal<List<A>, A> _traversal() {
return new Traversal<>(_pTraversal());
}

/**
* Performs function application within a list (applicative functor pattern).
*
Expand Down Expand Up @@ -1176,12 +1101,6 @@ public final List<A> nub(final Ord<A> o) {
return sort(o).group(o.equal()).map(List.<A>head_());
}

/**
* Optional targeted on Cons head.
*/
public static <A> Optional<List<A>, A> _head() {
return optional(l -> l.toOption(), a -> l -> l.<List<A>> list(l, constant(cons_(a))));
}

/**
* First-class head function.
Expand All @@ -1192,14 +1111,6 @@ public static <A> F<List<A>, A> head_() {
return list -> list.head();
}

/**
* Optional targeted on Cons tail.
*/
public static <A> Optional<List<A>, List<A>> _tail() {
return optional(l -> l.<Option<List<A>>> list(none(), h -> tail -> some(tail)),
tail -> l -> l.list(l, h -> constant(cons(h, tail))));
}

/**
* First-class tail function.
*
Expand Down Expand Up @@ -1597,13 +1508,6 @@ public static <A> List<A> nil() {
return (Nil<A>) Nil.INSTANCE;
}

/**
* Nil prism
*/
public static <A> Prism<List<A>, Unit> _nil() {
return prism(l -> l.isEmpty() ? some(unit()) : none(), constant(nil()));
}

/**
* Returns a function that prepends (cons) an element to a list to produce a new list.
*
Expand All @@ -1613,13 +1517,6 @@ public static <A> F<A, F<List<A>, List<A>>> cons() {
return a -> tail -> cons(a, tail);
}

/**
* Cons prism
*/
public static <A> Prism<List<A>, P2<A, List<A>>> _cons() {
return prism(l -> l.<Option<P2<A, List<A>>>> list(none(), h -> tail -> some(P.p(h, tail))), c -> cons(c._1(), c._2()));
}

public static <A> F2<A, List<A>, List<A>> cons_() {
return (a, listA) -> cons(a, listA);
}
Expand Down Expand Up @@ -2105,4 +2002,120 @@ public int hashCode() {
public boolean isSingle() {
return isNotEmpty() && tail().isEmpty();
}

/**
* Optic factory methods for a List
*/
public static final class Optic {

private Optic() {
throw new UnsupportedOperationException();
}

/**
* Polymorphic traversal
*/
public static <A, B> PTraversal<List<A>, List<B>, A, B> pTraversal() {
return new PTraversal<List<A>, List<B>, A, B>() {

@Override
public <C> F<List<A>, F<C, List<B>>> modifyFunctionF(F<A, F<C, B>> f) {
return l -> l.traverseF(f);
}

@Override
public <L> F<List<A>, Either<L, List<B>>> modifyEitherF(F<A, Either<L, B>> f) {
return l -> l.traverseEither(f);
}

@Override
public F<List<A>, IO<List<B>>> modifyIOF(F<A, IO<B>> f) {
return l -> l.traverseIO(f);
}

@Override
public F<List<A>, Trampoline<List<B>>> modifyTrampolineF(F<A, Trampoline<B>> f) {
return l -> l.traverseTrampoline(f);
}

@Override
public F<List<A>, Promise<List<B>>> modifyPromiseF(F<A, Promise<B>> f) {
return l -> l.traversePromise(f);
}

@Override
public F<List<A>, List<List<B>>> modifyListF(F<A, List<B>> f) {
return l -> l.traverseList(f);
}

@Override
public F<List<A>, Option<List<B>>> modifyOptionF(F<A, Option<B>> f) {
return l -> l.traverseOption(f);
}

@Override
public F<List<A>, Stream<List<B>>> modifyStreamF(F<A, Stream<B>> f) {
return l -> l.traverseStream(f);
}

@Override
public F<List<A>, P1<List<B>>> modifyP1F(F<A, P1<B>> f) {
return l -> l.traverseP1(f);
}

@Override
public <E> F<List<A>, Validation<E, List<B>>> modifyValidationF(F<A, Validation<E, B>> f) {
return l -> l.traverseValidation(f);
}

@Override
public F<List<A>, V2<List<B>>> modifyV2F(F<A, V2<B>> f) {
return l -> l.traverseV2(f);
}

@Override
public <M> F<List<A>, M> foldMap(Monoid<M> monoid, F<A, M> f) {
return l -> monoid.sumLeft(l.map(f));
}
};
}

/**
* Monomorphic traversal
*/
public static <A> Traversal<List<A>, A> traversal() {
return new Traversal<>(pTraversal());
}

/**
* Optional targeted on Cons head.
*/
public static <A> Optional<List<A>, A> head() {
return optional(l -> l.toOption(), a -> l -> l.<List<A>>list(l, constant(cons_(a))));
}

/**
* Optional targeted on Cons tail.
*/
public static <A> Optional<List<A>, List<A>> tail() {
return optional(l -> l.<Option<List<A>>> list(none(), h -> tail -> some(tail)),
tail -> l -> l.list(l, h -> constant(List.cons(h, tail))));
}

/**
* Nil prism
*/
public static <A> Prism<List<A>, Unit> nil() {
return prism((List<A> l) -> l.isEmpty() ? some(unit()) : none(), constant(List.nil()));
}

/**
* Cons prism
*/
public static <A> Prism<List<A>, P2<A, List<A>>> cons() {
return prism(l -> l.<Option<P2<A, List<A>>>> list(none(), h -> tail -> some(P.p(h, tail))), c -> List.cons(c._1(), c._2()));
}

}

}
4 changes: 2 additions & 2 deletions core/src/main/java/fj/data/optic/PIso.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
*
* <pre>
* S T S T
* | |
* | | | |
* | | | |
* get | | reverseGet reverse.reverseGet | | reverse.get
* | | | |
* f | | g
* | f | | g |
* A --------> B A <-------- B
* </pre>
*
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/fj/data/optic/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
*
* Optic data types adapted from the <a href="https://github.com/julien-truffaut/Monocle">Scala Monocle library</a>
* and inspired by the
* <a href="https://github.com/ekmett/lens">Haskell Lens library</a>. See the Monocle Github
* page for an overview of the package.
*
* @version %build.number%
* @see <a href="https://github.com/julien-truffaut/Monocle">Monocle</a>
*/
package fj.data.optic;
Loading