Skip to content

Commit f41234e

Browse files
committed
Moved optic instances to nested class. Added optic example
1 parent a513e40 commit f41234e

File tree

7 files changed

+258
-138
lines changed

7 files changed

+258
-138
lines changed

core/src/main/java/fj/P2.java

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -344,37 +344,50 @@ public static <A, B, C> F2<A, B, C> untuple(final F<P2<A, B>, C> f) {
344344
return (a, b) -> f.f(P.p(a, b));
345345
}
346346

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

354-
/**
355-
* Monomorphic lens targeted on _1.
356-
*/
357-
public static <A, B, C> Lens<P2<A, B>, A> _1Lens() {
358-
return new Lens<>(_1pLens());
359-
}
348+
@Override
349+
public String toString() {
350+
return Show.p2Show(Show.<A>anyShow(), Show.<B>anyShow()).showS(this);
351+
}
360352

361353
/**
362-
* Polyomorphic lens targeted on _2.
363-
*/
364-
public static <A, B, C> PLens<P2<A, B>, P2<A, C>, B, C> _2pLens() {
365-
return pLens(__2(), b -> p2 -> P.p(p2._1(), b));
366-
}
354+
* Optic factory methods for a P2
367355
368-
/**
369-
* Monomorphic lens targeted on _1.
370356
*/
371-
public static <A, B, C> Lens<P2<A, B>, B> _2Lens() {
372-
return new Lens<>(_2pLens());
373-
}
357+
public static final class Optic {
374358

375-
@Override
376-
public String toString() {
377-
return Show.p2Show(Show.<A>anyShow(), Show.<B>anyShow()).showS(this);
378-
}
359+
private Optic() {
360+
throw new UnsupportedOperationException();
361+
}
362+
363+
/**
364+
* Polyomorphic lens targeted on _1.
365+
*/
366+
public static <A, B, C> PLens<P2<A, B>, P2<C, B>, A, C> _1p() {
367+
return pLens(__1(), a -> p2 -> P.p(a, p2._2()));
368+
}
369+
370+
/**
371+
* Monomorphic lens targeted on _1.
372+
*/
373+
public static <A, B, C> Lens<P2<A, B>, A> _1() {
374+
return new Lens<>(_1p());
375+
}
376+
377+
/**
378+
* Polyomorphic lens targeted on _2.
379+
*/
380+
public static <A, B, C> PLens<P2<A, B>, P2<A, C>, B, C> _2p() {
381+
return pLens(__2(), b -> p2 -> P.p(p2._1(), b));
382+
}
383+
384+
/**
385+
* Monomorphic lens targeted on _1.
386+
*/
387+
public static <A, B, C> Lens<P2<A, B>, B> _2() {
388+
return new Lens<>(_2p());
389+
}
390+
391+
}
379392

380393
}

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

Lines changed: 116 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -655,81 +655,6 @@ public <B> V2<List<B>> traverseV2(final F<A, V2<B>> f) {
655655
v(List.<B> nil(), List.<B> nil()));
656656
}
657657

658-
/**
659-
* polymorphic traversal
660-
*/
661-
public static <A, B> PTraversal<List<A>, List<B>, A, B> _pTraversal() {
662-
return new PTraversal<List<A>, List<B>, A, B>() {
663-
664-
@Override
665-
public <C> F<List<A>, F<C, List<B>>> modifyFunctionF(F<A, F<C, B>> f) {
666-
return l -> l.traverseF(f);
667-
}
668-
669-
@Override
670-
public <L> F<List<A>, Either<L, List<B>>> modifyEitherF(F<A, Either<L, B>> f) {
671-
return l -> l.traverseEither(f);
672-
}
673-
674-
@Override
675-
public F<List<A>, IO<List<B>>> modifyIOF(F<A, IO<B>> f) {
676-
return l -> l.traverseIO(f);
677-
}
678-
679-
@Override
680-
public F<List<A>, Trampoline<List<B>>> modifyTrampolineF(F<A, Trampoline<B>> f) {
681-
return l -> l.traverseTrampoline(f);
682-
}
683-
684-
@Override
685-
public F<List<A>, Promise<List<B>>> modifyPromiseF(F<A, Promise<B>> f) {
686-
return l -> l.traversePromise(f);
687-
}
688-
689-
@Override
690-
public F<List<A>, List<List<B>>> modifyListF(F<A, List<B>> f) {
691-
return l -> l.traverseList(f);
692-
}
693-
694-
@Override
695-
public F<List<A>, Option<List<B>>> modifyOptionF(F<A, Option<B>> f) {
696-
return l -> l.traverseOption(f);
697-
}
698-
699-
@Override
700-
public F<List<A>, Stream<List<B>>> modifyStreamF(F<A, Stream<B>> f) {
701-
return l -> l.traverseStream(f);
702-
}
703-
704-
@Override
705-
public F<List<A>, P1<List<B>>> modifyP1F(F<A, P1<B>> f) {
706-
return l -> l.traverseP1(f);
707-
}
708-
709-
@Override
710-
public <E> F<List<A>, Validation<E, List<B>>> modifyValidationF(F<A, Validation<E, B>> f) {
711-
return l -> l.traverseValidation(f);
712-
}
713-
714-
@Override
715-
public F<List<A>, V2<List<B>>> modifyV2F(F<A, V2<B>> f) {
716-
return l -> l.traverseV2(f);
717-
}
718-
719-
@Override
720-
public <M> F<List<A>, M> foldMap(Monoid<M> monoid, F<A, M> f) {
721-
return l -> monoid.sumLeft(l.map(f));
722-
}
723-
};
724-
}
725-
726-
/**
727-
* monomorphic traversal
728-
*/
729-
public static <A> Traversal<List<A>, A> _traversal() {
730-
return new Traversal<>(_pTraversal());
731-
}
732-
733658
/**
734659
* Performs function application within a list (applicative functor pattern).
735660
*
@@ -1176,12 +1101,6 @@ public final List<A> nub(final Ord<A> o) {
11761101
return sort(o).group(o.equal()).map(List.<A>head_());
11771102
}
11781103

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

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

1195-
/**
1196-
* Optional targeted on Cons tail.
1197-
*/
1198-
public static <A> Optional<List<A>, List<A>> _tail() {
1199-
return optional(l -> l.<Option<List<A>>> list(none(), h -> tail -> some(tail)),
1200-
tail -> l -> l.list(l, h -> constant(cons(h, tail))));
1201-
}
1202-
12031114
/**
12041115
* First-class tail function.
12051116
*
@@ -1597,13 +1508,6 @@ public static <A> List<A> nil() {
15971508
return (Nil<A>) Nil.INSTANCE;
15981509
}
15991510

1600-
/**
1601-
* Nil prism
1602-
*/
1603-
public static <A> Prism<List<A>, Unit> _nil() {
1604-
return prism(l -> l.isEmpty() ? some(unit()) : none(), constant(nil()));
1605-
}
1606-
16071511
/**
16081512
* Returns a function that prepends (cons) an element to a list to produce a new list.
16091513
*
@@ -1613,13 +1517,6 @@ public static <A> F<A, F<List<A>, List<A>>> cons() {
16131517
return a -> tail -> cons(a, tail);
16141518
}
16151519

1616-
/**
1617-
* Cons prism
1618-
*/
1619-
public static <A> Prism<List<A>, P2<A, List<A>>> _cons() {
1620-
return prism(l -> l.<Option<P2<A, List<A>>>> list(none(), h -> tail -> some(P.p(h, tail))), c -> cons(c._1(), c._2()));
1621-
}
1622-
16231520
public static <A> F2<A, List<A>, List<A>> cons_() {
16241521
return (a, listA) -> cons(a, listA);
16251522
}
@@ -2105,4 +2002,120 @@ public int hashCode() {
21052002
public boolean isSingle() {
21062003
return isNotEmpty() && tail().isEmpty();
21072004
}
2005+
2006+
/**
2007+
* Optic factory methods for a List
2008+
*/
2009+
public static final class Optic {
2010+
2011+
private Optic() {
2012+
throw new UnsupportedOperationException();
2013+
}
2014+
2015+
/**
2016+
* Polymorphic traversal
2017+
*/
2018+
public static <A, B> PTraversal<List<A>, List<B>, A, B> pTraversal() {
2019+
return new PTraversal<List<A>, List<B>, A, B>() {
2020+
2021+
@Override
2022+
public <C> F<List<A>, F<C, List<B>>> modifyFunctionF(F<A, F<C, B>> f) {
2023+
return l -> l.traverseF(f);
2024+
}
2025+
2026+
@Override
2027+
public <L> F<List<A>, Either<L, List<B>>> modifyEitherF(F<A, Either<L, B>> f) {
2028+
return l -> l.traverseEither(f);
2029+
}
2030+
2031+
@Override
2032+
public F<List<A>, IO<List<B>>> modifyIOF(F<A, IO<B>> f) {
2033+
return l -> l.traverseIO(f);
2034+
}
2035+
2036+
@Override
2037+
public F<List<A>, Trampoline<List<B>>> modifyTrampolineF(F<A, Trampoline<B>> f) {
2038+
return l -> l.traverseTrampoline(f);
2039+
}
2040+
2041+
@Override
2042+
public F<List<A>, Promise<List<B>>> modifyPromiseF(F<A, Promise<B>> f) {
2043+
return l -> l.traversePromise(f);
2044+
}
2045+
2046+
@Override
2047+
public F<List<A>, List<List<B>>> modifyListF(F<A, List<B>> f) {
2048+
return l -> l.traverseList(f);
2049+
}
2050+
2051+
@Override
2052+
public F<List<A>, Option<List<B>>> modifyOptionF(F<A, Option<B>> f) {
2053+
return l -> l.traverseOption(f);
2054+
}
2055+
2056+
@Override
2057+
public F<List<A>, Stream<List<B>>> modifyStreamF(F<A, Stream<B>> f) {
2058+
return l -> l.traverseStream(f);
2059+
}
2060+
2061+
@Override
2062+
public F<List<A>, P1<List<B>>> modifyP1F(F<A, P1<B>> f) {
2063+
return l -> l.traverseP1(f);
2064+
}
2065+
2066+
@Override
2067+
public <E> F<List<A>, Validation<E, List<B>>> modifyValidationF(F<A, Validation<E, B>> f) {
2068+
return l -> l.traverseValidation(f);
2069+
}
2070+
2071+
@Override
2072+
public F<List<A>, V2<List<B>>> modifyV2F(F<A, V2<B>> f) {
2073+
return l -> l.traverseV2(f);
2074+
}
2075+
2076+
@Override
2077+
public <M> F<List<A>, M> foldMap(Monoid<M> monoid, F<A, M> f) {
2078+
return l -> monoid.sumLeft(l.map(f));
2079+
}
2080+
};
2081+
}
2082+
2083+
/**
2084+
* Monomorphic traversal
2085+
*/
2086+
public static <A> Traversal<List<A>, A> traversal() {
2087+
return new Traversal<>(pTraversal());
2088+
}
2089+
2090+
/**
2091+
* Optional targeted on Cons head.
2092+
*/
2093+
public static <A> Optional<List<A>, A> head() {
2094+
return optional(l -> l.toOption(), a -> l -> l.<List<A>>list(l, constant(cons_(a))));
2095+
}
2096+
2097+
/**
2098+
* Optional targeted on Cons tail.
2099+
*/
2100+
public static <A> Optional<List<A>, List<A>> tail() {
2101+
return optional(l -> l.<Option<List<A>>> list(none(), h -> tail -> some(tail)),
2102+
tail -> l -> l.list(l, h -> constant(List.cons(h, tail))));
2103+
}
2104+
2105+
/**
2106+
* Nil prism
2107+
*/
2108+
public static <A> Prism<List<A>, Unit> nil() {
2109+
return prism((List<A> l) -> l.isEmpty() ? some(unit()) : none(), constant(List.nil()));
2110+
}
2111+
2112+
/**
2113+
* Cons prism
2114+
*/
2115+
public static <A> Prism<List<A>, P2<A, List<A>>> cons() {
2116+
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()));
2117+
}
2118+
2119+
}
2120+
21082121
}

core/src/main/java/fj/data/optic/PIso.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
*
3434
* <pre>
3535
* S T S T
36-
* | |
36+
* | | | |
3737
* | | | |
3838
* get | | reverseGet reverse.reverseGet | | reverse.get
3939
* | | | |
40-
* f | | g
40+
* | f | | g |
4141
* A --------> B A <-------- B
4242
* </pre>
4343
*
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
*
3+
* Optic data types adapted from the <a href="https://github.com/julien-truffaut/Monocle">Scala Monocle library</a>
4+
* and inspired by the
5+
* <a href="https://github.com/ekmett/lens">Haskell Lens library</a>. See the Monocle Github
6+
* page for an overview of the package.
7+
*
8+
* @version %build.number%
9+
* @see <a href="https://github.com/julien-truffaut/Monocle">Monocle</a>
10+
*/
11+
package fj.data.optic;

0 commit comments

Comments
 (0)