From f056c287947d7aad4704207281e936c7a259cdc7 Mon Sep 17 00:00:00 2001 From: John McClean Date: Wed, 21 Jun 2017 23:10:15 +0100 Subject: [PATCH 01/30] pCollecitons fix #271 --- .../java/cyclops/collections/clojure/ClojureListX.java | 10 ++++++++-- .../main/java/cyclops/collections/dexx/DexxListX.java | 9 +++++++-- .../streams/observables/AsyncSequentialTest.java | 7 +++++++ cyclops-rxjava2/build.gradle | 2 +- .../AsyncRSExtensionOperatorsTest.java | 5 +++-- .../syncflux/SyncRSExtensionOperatorsTest.java | 7 ++++--- .../streams/observables/AsyncSequentialTest.java | 7 +++++++ .../java/cyclops/collections/scala/ScalaListX.java | 10 +++++++--- .../main/java/cyclops/collections/vavr/VavrListX.java | 7 +++++-- 9 files changed, 49 insertions(+), 15 deletions(-) diff --git a/cyclops-clojure/src/main/java/cyclops/collections/clojure/ClojureListX.java b/cyclops-clojure/src/main/java/cyclops/collections/clojure/ClojureListX.java index 1f6eaab7..caf58699 100644 --- a/cyclops-clojure/src/main/java/cyclops/collections/clojure/ClojureListX.java +++ b/cyclops-clojure/src/main/java/cyclops/collections/clojure/ClojureListX.java @@ -7,6 +7,7 @@ import java.util.stream.Stream; import com.aol.cyclops2.data.collections.extensions.CollectionX; +import com.aol.cyclops2.data.collections.extensions.lazy.immutable.FoldToList; import com.aol.cyclops2.data.collections.extensions.lazy.immutable.LazyLinkedListX; import com.aol.cyclops2.types.Unwrapable; import com.aol.cyclops2.types.foldable.Evaluation; @@ -28,6 +29,8 @@ @AllArgsConstructor(access = AccessLevel.PRIVATE) public class ClojureListX extends AbstractListimplements PStack, Unwrapable { + static final FoldToList gen = (it, i)-> ClojureListX.from(from(it,i)); + public static LinkedListX listX(ReactiveSeq stream){ return fromStream(stream); } @@ -35,6 +38,9 @@ public static LinkedListX copyFromCollection(CollectionX vec) { return fromPStack(new ClojureListX(from(vec.iterator(),0)),toPStack()); } + public static LazyLinkedListX from(IPersistentList q) { + return fromPStack(new ClojureListX<>(q), toPStack()); + } private static IPersistentList from(final Iterator i, int depth) { if(!i.hasNext()) @@ -55,7 +61,7 @@ public R unwrap() { public static LazyLinkedListX fromStream(Stream stream) { Reducer> s = toPStack(); return new LazyLinkedListX(null, - ReactiveSeq.fromStream(stream), s, Evaluation.LAZY); + ReactiveSeq.fromStream(stream), s,gen, Evaluation.LAZY); } /** @@ -177,7 +183,7 @@ public static LazyLinkedListX PStack(List q) { } private static LazyLinkedListX fromPStack(PStack s, Reducer> pStackReducer) { - return new LazyLinkedListX(s,null, pStackReducer, Evaluation.LAZY); + return new LazyLinkedListX(s,null, pStackReducer, gen,Evaluation.LAZY); } @SafeVarargs diff --git a/cyclops-dexx/src/main/java/cyclops/collections/dexx/DexxListX.java b/cyclops-dexx/src/main/java/cyclops/collections/dexx/DexxListX.java index 3d4ffc97..b06bf5d6 100644 --- a/cyclops-dexx/src/main/java/cyclops/collections/dexx/DexxListX.java +++ b/cyclops-dexx/src/main/java/cyclops/collections/dexx/DexxListX.java @@ -7,6 +7,7 @@ import java.util.stream.Stream; import com.aol.cyclops2.data.collections.extensions.CollectionX; +import com.aol.cyclops2.data.collections.extensions.lazy.immutable.FoldToList; import com.aol.cyclops2.data.collections.extensions.lazy.immutable.LazyLinkedListX; import com.aol.cyclops2.types.Unwrapable; import com.aol.cyclops2.types.foldable.Evaluation; @@ -35,6 +36,10 @@ public class DexxListX extends AbstractListimplements PStack, Unwrapabl public static LinkedListX listX(ReactiveSeq stream){ return fromStream(stream); } + static final FoldToList gen = (it, i)-> DexxListX.from(from(it,i)); + public static LazyLinkedListX from(List q) { + return fromPStack(new DexxListX<>(q), toPStack()); + } @Override public R unwrap() { return (R)list; @@ -60,7 +65,7 @@ private static List from(final Iterator i, int depth) { */ public static LazyLinkedListX fromStream(Stream stream) { Reducer> r = toPStack(); - return new LazyLinkedListX(null, ReactiveSeq.fromStream(stream), r, Evaluation.LAZY); + return new LazyLinkedListX(null, ReactiveSeq.fromStream(stream), r, gen,Evaluation.LAZY); } /** @@ -189,7 +194,7 @@ public static LazyLinkedListX PStack(List q) { toPStack()); } private static LazyLinkedListX fromPStack(PStack s, Reducer> pStackReducer) { - return new LazyLinkedListX(s,null,pStackReducer, Evaluation.LAZY); + return new LazyLinkedListX(s,null,pStackReducer,gen, Evaluation.LAZY); } diff --git a/cyclops-rx/src/test/java/cyclops/streams/observables/AsyncSequentialTest.java b/cyclops-rx/src/test/java/cyclops/streams/observables/AsyncSequentialTest.java index 8ea6b13a..1aa121e9 100644 --- a/cyclops-rx/src/test/java/cyclops/streams/observables/AsyncSequentialTest.java +++ b/cyclops-rx/src/test/java/cyclops/streams/observables/AsyncSequentialTest.java @@ -23,6 +23,7 @@ import java.util.concurrent.ForkJoinPool; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; +import java.util.stream.Stream; import static java.util.Arrays.asList; import static org.hamcrest.CoreMatchers.equalTo; @@ -58,7 +59,13 @@ protected ReactiveSeq of(U... array){ public void testCycle() { } + @Test + public void limitReplay() { + final ReactiveSeq t = of(1).map(i -> i).flatMap(i -> Stream.of(i)); + assertThat(t.limit(1).toList(), equalTo(ListX.of(1))); + t.limit(1).toList(); + } @Test public void subscribe3ErrorOnComplete() throws InterruptedException { List result = new ArrayList<>(); diff --git a/cyclops-rxjava2/build.gradle b/cyclops-rxjava2/build.gradle index cc1d57f8..0def6ef5 100644 --- a/cyclops-rxjava2/build.gradle +++ b/cyclops-rxjava2/build.gradle @@ -66,7 +66,7 @@ modifyPom { inceptionYear '2017' groupId 'com.aol.cyclops' - artifactId 'cyclops-rx' + artifactId 'cyclops-rxjava2' version "$version" scm { diff --git a/cyclops-rxjava2/src/test/java/cyclops/streams/flowables/asyncreactivestreams/AsyncRSExtensionOperatorsTest.java b/cyclops-rxjava2/src/test/java/cyclops/streams/flowables/asyncreactivestreams/AsyncRSExtensionOperatorsTest.java index 348219a6..2b9d9d43 100644 --- a/cyclops-rxjava2/src/test/java/cyclops/streams/flowables/asyncreactivestreams/AsyncRSExtensionOperatorsTest.java +++ b/cyclops-rxjava2/src/test/java/cyclops/streams/flowables/asyncreactivestreams/AsyncRSExtensionOperatorsTest.java @@ -187,11 +187,12 @@ public void singleOptonal2(){ public void limitTime(){ List result = of(1,2,3,4,5,6) .peek(i->sleep(i*100)) - .limit(1000,TimeUnit.MILLISECONDS) + .limit(1000, TimeUnit.MILLISECONDS) .toList(); - assertThat(result,equalTo(Arrays.asList(1,2,3))); + assertThat(result,contains(1,2,3)); + assertThat(result.size(),lessThan(5)); } @Test public void limitTimeEmpty(){ diff --git a/cyclops-rxjava2/src/test/java/cyclops/streams/flowables/syncflux/SyncRSExtensionOperatorsTest.java b/cyclops-rxjava2/src/test/java/cyclops/streams/flowables/syncflux/SyncRSExtensionOperatorsTest.java index 381e328f..307362f3 100644 --- a/cyclops-rxjava2/src/test/java/cyclops/streams/flowables/syncflux/SyncRSExtensionOperatorsTest.java +++ b/cyclops-rxjava2/src/test/java/cyclops/streams/flowables/syncflux/SyncRSExtensionOperatorsTest.java @@ -182,9 +182,10 @@ public void limitTime(){ .peek(i->sleep(i*100)) .limit(1000,TimeUnit.MILLISECONDS) .toList(); - - - assertThat(result,equalTo(Arrays.asList(1,2,3))); + + + assertThat(result,contains(1,2,3)); + assertThat(result.size(),lessThan(5)); } @Test public void limitTimeEmpty(){ diff --git a/cyclops-rxjava2/src/test/java/cyclops/streams/observables/AsyncSequentialTest.java b/cyclops-rxjava2/src/test/java/cyclops/streams/observables/AsyncSequentialTest.java index a793f6f3..b4d5fb71 100644 --- a/cyclops-rxjava2/src/test/java/cyclops/streams/observables/AsyncSequentialTest.java +++ b/cyclops-rxjava2/src/test/java/cyclops/streams/observables/AsyncSequentialTest.java @@ -23,6 +23,7 @@ import java.util.concurrent.ForkJoinPool; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; +import java.util.stream.Stream; import static java.util.Arrays.asList; import static org.hamcrest.CoreMatchers.equalTo; @@ -58,7 +59,13 @@ protected ReactiveSeq of(U... array){ public void testCycle() { } + @Test + public void limitReplay() { + final ReactiveSeq t = of(1).map(i -> i).flatMap(i -> Stream.of(i)); + assertThat(t.limit(1).toList(), equalTo(ListX.of(1))); + t.limit(1).toList(); + } @Test public void subscribe3ErrorOnComplete() throws InterruptedException { List result = new ArrayList<>(); diff --git a/cyclops-scala/src/main/java/cyclops/collections/scala/ScalaListX.java b/cyclops-scala/src/main/java/cyclops/collections/scala/ScalaListX.java index f60a424f..b17bc633 100644 --- a/cyclops-scala/src/main/java/cyclops/collections/scala/ScalaListX.java +++ b/cyclops-scala/src/main/java/cyclops/collections/scala/ScalaListX.java @@ -12,6 +12,7 @@ import com.aol.cyclops.scala.collections.HasScalaCollection; import com.aol.cyclops2.data.collections.extensions.CollectionX; +import com.aol.cyclops2.data.collections.extensions.lazy.immutable.FoldToList; import com.aol.cyclops2.data.collections.extensions.lazy.immutable.LazyLinkedListX; import com.aol.cyclops2.types.Unwrapable; import com.aol.cyclops2.types.foldable.Evaluation; @@ -39,6 +40,7 @@ @AllArgsConstructor(access = AccessLevel.PRIVATE) public class ScalaListX extends AbstractListimplements PStack, HasScalaCollection, Unwrapable { + static final FoldToList gen = (it, i)-> ScalaListX.from(from(it,i)); public static LinkedListX listX(ReactiveSeq stream){ return fromStream(stream); } @@ -46,7 +48,9 @@ public static LinkedListX listX(ReactiveSeq stream){ public R unwrap() { return (R)list; } - + public static LazyLinkedListX from(List q) { + return fromPStack(new ScalaListX<>(q), toPStack()); + } public LazyLinkedListX plusLoop(int max, IntFunction value) { List toUse = list; @@ -73,7 +77,7 @@ public LazyLinkedListX plusLoop(Supplier> supplier) { * @return LazyLinkedListX */ public static LazyLinkedListX fromStream(Stream stream) { - return new LazyLinkedListX(null,ReactiveSeq.fromStream(stream), toPStack(), Evaluation.LAZY); + return new LazyLinkedListX(null,ReactiveSeq.fromStream(stream), toPStack(), gen, Evaluation.LAZY); } /** @@ -171,7 +175,7 @@ public static LazyLinkedListX lazyList(List vector){ } private static LazyLinkedListX fromPStack(PStack s, Reducer> pStackReducer) { - return new LazyLinkedListX(s,null, pStackReducer,Evaluation.LAZY); + return new LazyLinkedListX(s,null, pStackReducer,gen, Evaluation.LAZY); } diff --git a/cyclops-vavr/src/main/java/cyclops/collections/vavr/VavrListX.java b/cyclops-vavr/src/main/java/cyclops/collections/vavr/VavrListX.java index c48a5c22..25708b9a 100644 --- a/cyclops-vavr/src/main/java/cyclops/collections/vavr/VavrListX.java +++ b/cyclops-vavr/src/main/java/cyclops/collections/vavr/VavrListX.java @@ -10,6 +10,7 @@ import java.util.stream.Stream; import com.aol.cyclops2.data.collections.extensions.CollectionX; +import com.aol.cyclops2.data.collections.extensions.lazy.immutable.FoldToList; import com.aol.cyclops2.data.collections.extensions.lazy.immutable.LazyLinkedListX; import com.aol.cyclops2.types.Unwrapable; import com.aol.cyclops2.types.foldable.Evaluation; @@ -36,12 +37,14 @@ public R unwrap() { return (R)list; } + static final FoldToList gen = (it,i)-> VavrListX.from(from(it,i)); public static LinkedListX copyFromCollection(CollectionX vec) { List list = from(vec.iterator(),0); return from(list); } + private static List from(final Iterator i, int depth) { if(!i.hasNext()) @@ -57,7 +60,7 @@ private static List from(final Iterator i, int depth) { */ public static LazyLinkedListX fromStream(Stream stream) { Reducer> p = toPStack(); - return new LazyLinkedListX(null, ReactiveSeq.fromStream(stream),p, Evaluation.LAZY); + return new LazyLinkedListX(null, ReactiveSeq.fromStream(stream),p, gen,Evaluation.LAZY); } /** @@ -152,7 +155,7 @@ public static LazyLinkedListX empty(){ return fromPStack(new VavrListX(List.empty()), toPStack()); } private static LazyLinkedListX fromPStack(PStack s, Reducer> pStackReducer) { - return new LazyLinkedListX(s,null, pStackReducer, Evaluation.LAZY); + return new LazyLinkedListX(s,null, pStackReducer, gen, Evaluation.LAZY); } public static LazyLinkedListX singleton(T t){ return fromPStack(new VavrListX(List.of(t)), toPStack()); From 4f0a047c2ac6a81ec0eec04fc37e9f63948d6bc0 Mon Sep 17 00:00:00 2001 From: John McClean Date: Tue, 4 Jul 2017 18:09:49 +0100 Subject: [PATCH 02/30] 2.0.0-FINAL migration --- .../collections/clojure/ClojureListX.java | 2 + .../cyclops/functionaljava/hkt/ListKind.java | 25 ++++---- .../functionaljava/hkt/NonEmptyListKind.java | 27 ++++---- .../functionaljava/hkt/OptionKind.java | 25 ++++---- .../functionaljava/hkt/StreamKind.java | 25 ++++---- .../companion/functionaljava/Lists.java | 34 +++++----- .../functionaljava/NonEmptyLists.java | 20 +++--- .../companion/functionaljava/Options.java | 40 ++++++------ .../companion/functionaljava/Streams.java | 34 +++++----- .../hkt/typeclesses/instances/ListsTest.java | 16 ++--- .../instances/NonEmptyListsTest.java | 8 +-- .../hkt/typeclesses/instances/OptionTest.java | 16 ++--- .../cyclops/guava/hkt/FluentIterableKind.java | 21 +++---- .../aol/cyclops/guava/hkt/OptionalKind.java | 19 +++--- .../companion/guava/FluentIterables.java | 42 ++++++------- .../cyclops/companion/guava/Optionals.java | 40 ++++++------ .../instances/FluentIterablesTest.java | 28 +++------ .../typeclasses/instances/OptionalsTest.java | 16 ++--- cyclops-reactor/readme.md | 8 +-- .../com/aol/cyclops/reactor/hkt/FluxKind.java | 21 +++---- .../com/aol/cyclops/reactor/hkt/MonoKind.java | 19 +++--- .../java/cyclops/companion/reactor/Fluxs.java | 44 ++++++------- .../java/cyclops/companion/reactor/Monos.java | 40 ++++++------ .../hkt/typeclasses/instances/FluxTest.java | 16 ++--- .../hkt/typeclasses/instances/MonosTest.java | 30 +++++---- cyclops-rx/readme.md | 4 +- .../cyclops/rx/adapter/ObservableAdapter.java | 22 +++---- .../aol/cyclops/rx/hkt/ObservableKind.java | 12 ++-- .../cyclops/companion/rx/Observables.java | 62 ++++++++----------- .../main/java/cyclops/monads/RxWitness.java | 6 +- .../test/java/com/aol/cyclops/rx/RxTest.java | 7 +-- .../typeclasses/instances/ObservableTest.java | 34 +++++----- cyclops-rxjava2/readme.md | 12 ++-- .../rx2/adapter/ObservableAdapter.java | 22 +++---- .../com/aol/cyclops/rx2/hkt/FlowableKind.java | 21 +++---- .../com/aol/cyclops/rx2/hkt/MaybeKind.java | 19 +++--- .../aol/cyclops/rx2/hkt/ObservableKind.java | 21 +++---- .../com/aol/cyclops/rx2/hkt/SingleKind.java | 10 +-- .../java/cyclops/companion/rx2/Flowables.java | 44 ++++++------- .../java/cyclops/companion/rx2/Maybes.java | 40 ++++++------ .../cyclops/companion/rx2/Observables.java | 52 ++++++++-------- .../java/cyclops/companion/rx2/Singles.java | 40 ++++++------ .../main/java/cyclops/monads/Rx2Witness.java | 6 +- .../test/java/com/aol/cyclops/rx/RxTest.java | 4 +- .../typeclasses/instances/FlowableTest.java | 28 +++------ .../hkt/typeclasses/instances/MaybesTest.java | 29 +++++---- .../typeclasses/instances/ObservableTest.java | 28 +++------ .../typeclasses/instances/SinglesTest.java | 30 +++++---- cyclops-vavr/readme.md | 4 +- .../com/aol/cyclops/vavr/hkt/ArrayKind.java | 23 +++---- .../com/aol/cyclops/vavr/hkt/FutureKind.java | 19 +++--- .../com/aol/cyclops/vavr/hkt/LazyKind.java | 21 +++---- .../com/aol/cyclops/vavr/hkt/ListKind.java | 21 +++---- .../com/aol/cyclops/vavr/hkt/OptionKind.java | 18 +++--- .../com/aol/cyclops/vavr/hkt/QueueKind.java | 23 +++---- .../com/aol/cyclops/vavr/hkt/StreamKind.java | 21 +++---- .../com/aol/cyclops/vavr/hkt/VectorKind.java | 22 +++---- .../java/cyclops/companion/vavr/Arrays.java | 34 +++++----- .../java/cyclops/companion/vavr/Futures.java | 40 ++++++------ .../java/cyclops/companion/vavr/Lazys.java | 42 +++++++------ .../java/cyclops/companion/vavr/Lists.java | 42 ++++++------- .../java/cyclops/companion/vavr/Options.java | 40 ++++++------ .../java/cyclops/companion/vavr/Queues.java | 34 +++++----- .../java/cyclops/companion/vavr/Streams.java | 34 +++++----- .../java/cyclops/companion/vavr/Vectors.java | 34 +++++----- .../main/java/cyclops/monads/VavrWitness.java | 9 +++ .../hkt/typeclesses/instances/ArraysTest.java | 16 ++--- .../typeclesses/instances/FuturesTest.java | 16 ++--- .../instances/LazyInstancesTest.java | 48 +++++++------- .../hkt/typeclesses/instances/ListTest.java | 28 +++------ .../typeclesses/instances/OptionsTest.java | 16 ++--- .../hkt/typeclesses/instances/QueuesTest.java | 16 ++--- .../hkt/typeclesses/instances/StreamTest.java | 36 +++++------ .../typeclesses/instances/VectorsTest.java | 16 ++--- gradle.properties | 4 +- 75 files changed, 880 insertions(+), 966 deletions(-) diff --git a/cyclops-clojure/src/main/java/cyclops/collections/clojure/ClojureListX.java b/cyclops-clojure/src/main/java/cyclops/collections/clojure/ClojureListX.java index caf58699..aabed254 100644 --- a/cyclops-clojure/src/main/java/cyclops/collections/clojure/ClojureListX.java +++ b/cyclops-clojure/src/main/java/cyclops/collections/clojure/ClojureListX.java @@ -16,6 +16,7 @@ import cyclops.function.Reducer; import cyclops.stream.ReactiveSeq; import org.jooq.lambda.tuple.Tuple2; +import org.pcollections.ConsPStack; import org.pcollections.PStack; @@ -41,6 +42,7 @@ public static LinkedListX copyFromCollection(CollectionX vec) { public static LazyLinkedListX from(IPersistentList q) { return fromPStack(new ClojureListX<>(q), toPStack()); } + private static IPersistentList from(final Iterator i, int depth) { if(!i.hasNext()) diff --git a/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/ListKind.java b/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/ListKind.java index fdb0c076..cba49189 100644 --- a/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/ListKind.java +++ b/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/ListKind.java @@ -2,9 +2,12 @@ import java.util.Collection; import java.util.Iterator; +import java.util.function.Function; import com.aol.cyclops2.hkt.Higher; +import cyclops.monads.FJWitness; +import cyclops.monads.FJWitness.list; import fj.Equal; import fj.F; import fj.F0; @@ -33,21 +36,17 @@ /** * Simulates Higher Kinded Types for List's * - * ListKind is a List and a Higher Kinded Type (ListKind.µ,T) + * ListKind is a List and a Higher Kinded Type (list,T) * * @author johnmcclean * * @param Data type stored within the List */ @AllArgsConstructor(access = AccessLevel.PRIVATE) -public class ListKind implements Higher { - /** - * Witness type - * - * @author johnmcclean - * - */ - public static class µ { +public class ListKind implements Higher { + + public ListKind fold(Function,? extends List> op){ + return widen(op.apply(boxed)); } public static ListKind list(final T... values) { @@ -73,9 +72,9 @@ public static ListKind widen(final List list) { * @param list HTK encoded type containing a List to widen * @return HKT encoded type with a widened List */ - public static Higher> widen2(Higher> list){ + public static Higher> widen2(Higher> list){ //a functor could be used (if C2 is a functor / one exists for C2 type) instead of casting - //cast seems safer as Higher must be a ListKind + //cast seems safer as Higher must be a ListKind return (Higher)list; } /** @@ -84,7 +83,7 @@ public static ListKind widen(final List list) { * @param list HKT encoded list into a ListKind * @return ListKind */ - public static ListKind narrowK(final Higher list) { + public static ListKind narrowK(final Higher list) { return (ListKind)list; } /** @@ -93,7 +92,7 @@ public static ListKind narrowK(final Higher list) { * @param list Type Constructor to convert back into narrowed type * @return ListX from Higher Kinded Type */ - public static List narrow(final Higher list) { + public static List narrow(final Higher list) { return ((ListKind)list).narrow(); } diff --git a/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/NonEmptyListKind.java b/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/NonEmptyListKind.java index b88e92d6..150a39f8 100644 --- a/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/NonEmptyListKind.java +++ b/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/NonEmptyListKind.java @@ -4,38 +4,37 @@ import java.util.Iterator; import java.util.Spliterator; import java.util.function.Consumer; +import java.util.function.Function; import com.aol.cyclops2.hkt.Higher; +import cyclops.monads.FJWitness; +import cyclops.monads.FJWitness.nonEmptyList; import fj.F; import fj.F2; import fj.Ord; import fj.P2; import fj.data.List; import fj.data.NonEmptyList; +import fj.data.Stream; import lombok.AccessLevel; import lombok.AllArgsConstructor; /** * Simulates Higher Kinded Types for NonEmptyList's * - * NonEmptyListKind is a NonEmptyList and a Higher Kinded Type (ListKind.µ,T) + * NonEmptyListKind is a NonEmptyList and a Higher Kinded Type (list,T) * * @author johnmcclean * * @param Data type stored within the NonEmptyList */ @AllArgsConstructor(access = AccessLevel.PRIVATE) -public class NonEmptyListKind implements Higher { - /** - * Witness type - * - * @author johnmcclean - * - */ - public static class µ { +public class NonEmptyListKind implements Higher { + + public NonEmptyListKind fold(Function,? extends NonEmptyList> op){ + return widen(op.apply(boxed)); } - public static NonEmptyListKind of(T first, T...v){ return widen(NonEmptyList.fromList(List.list(v).cons(first)).some()); @@ -61,9 +60,9 @@ public static NonEmptyListKind widen(final NonEmptyList list) { * @param list HTK encoded type containing a NonEmptyList to widen * @return HKT encoded type with a widened NonEmptyList */ - public static Higher> widen2(Higher> list){ + public static Higher> widen2(Higher> list){ //a functor could be used (if C2 is a functor / one exists for C2 type) instead of casting - //cast seems safer as Higher must be a NonEmptyListKind + //cast seems safer as Higher must be a NonEmptyListKind return (Higher)list; } /** @@ -72,7 +71,7 @@ public static NonEmptyListKind widen(final NonEmptyList list) { * @param list HKT encoded list into a NonEmptyListKind * @return NonEmptyListKind */ - public static NonEmptyListKind narrowK(final Higher list) { + public static NonEmptyListKind narrowK(final Higher list) { return (NonEmptyListKind)list; } /** @@ -81,7 +80,7 @@ public static NonEmptyListKind narrowK(final Higher NonEmptyList narrow(final Higher list) { + public static NonEmptyList narrow(final Higher list) { return ((NonEmptyListKind)list).narrow(); } diff --git a/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/OptionKind.java b/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/OptionKind.java index 6b7e7917..40ea4bbe 100644 --- a/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/OptionKind.java +++ b/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/OptionKind.java @@ -11,6 +11,8 @@ import cyclops.conversion.functionaljava.FromCyclopsReact; import com.aol.cyclops2.hkt.Higher; import cyclops.control.Maybe; +import cyclops.monads.FJWitness; +import cyclops.monads.FJWitness.option; import fj.F; import fj.F0; import fj.F2; @@ -40,24 +42,19 @@ /** * Simulates Higher Kinded Types for Option's * - * OptionKind is a Option and a Higher Kinded Type (OptionKind.µ,T) + * OptionKind is a Option and a Higher Kinded Type (option,T) * * @author johnmcclean * * @param Data type stored within the Option */ @AllArgsConstructor(access = AccessLevel.PRIVATE) -public final class OptionKind implements Higher, Iterable { - private final Option boxed; - - /** - * Witness type - * - * @author johnmcclean - * - */ - public static class µ { +public final class OptionKind implements Higher, Iterable { + private final Option boxed; + public OptionKind fold(Function,? extends Option> op){ + return widen(op.apply(boxed)); } + /** * @return An HKT encoded empty Option */ @@ -96,7 +93,7 @@ public static OptionKind widen(final Maybe option) { * @param future HKT encoded list into a OptionKind * @return OptionKind */ - public static OptionKind narrowK(final Higher future) { + public static OptionKind narrowK(final Higher future) { return (OptionKind)future; } /** @@ -105,8 +102,8 @@ public static OptionKind narrowK(final Higher future) { * @param Option Type Constructor to convert back into narrowed type * @return Option from Higher Kinded Type */ - public static Option narrow(final Higher Option) { - //has to be an OptionKind as only OptionKind can implement Higher + public static Option narrow(final Higher Option) { + //has to be an OptionKind as only OptionKind can implement Higher return ((OptionKind)Option).boxed; } diff --git a/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/StreamKind.java b/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/StreamKind.java index 2db3e7d8..0ade4f48 100644 --- a/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/StreamKind.java +++ b/cyclops-functionaljava/src/main/java/com/aol/cyclops/functionaljava/hkt/StreamKind.java @@ -2,9 +2,12 @@ import java.util.Collection; import java.util.Iterator; +import java.util.function.Function; import com.aol.cyclops2.hkt.Higher; +import cyclops.monads.FJWitness; +import cyclops.monads.FJWitness.stream; import fj.Equal; import fj.F; import fj.F0; @@ -34,15 +37,8 @@ * @param Data type stored within the Stream */ @AllArgsConstructor(access = AccessLevel.PRIVATE) -public class StreamKind implements Higher { - /** - * Witness type - * - * @author johnmcclean - * - */ - public static class µ { - } +public class StreamKind implements Higher { + public static StreamKind stream(final T... values) { return widen(Stream.stream(values)); @@ -67,9 +63,9 @@ public static StreamKind widen(final Stream stream) { * @param stream HTK encoded type containing a Stream to widen * @return HKT encoded type with a widened Stream */ - public static Higher> widen2(Higher> stream){ + public static Higher> widen2(Higher> stream){ //a functor could be used (if C2 is a functor / one exists for C2 type) instead of casting - //cast seems safer as Higher must be a StreamKind + //cast seems safer as Higher must be a StreamKind return (Higher)stream; } /** @@ -78,7 +74,7 @@ public static StreamKind widen(final Stream stream) { * @param stream HKT encoded stream into a StreamKind * @return StreamKind */ - public static StreamKind narrowK(final Higher stream) { + public static StreamKind narrowK(final Higher stream) { return (StreamKind)stream; } /** @@ -87,11 +83,14 @@ public static StreamKind narrowK(final Higher stream) { * @param stream Type Constructor to convert back into narrowed type * @return StreamX from Higher Kinded Type */ - public static Stream narrow(final Higher stream) { + public static Stream narrow(final Higher stream) { return ((StreamKind)stream).narrow(); } + public StreamKind fold(Function,? extends Stream> op){ + return widen(op.apply(boxed)); + } private final Stream boxed; diff --git a/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/Lists.java b/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/Lists.java index 13ade87a..9852e181 100644 --- a/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/Lists.java +++ b/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/Lists.java @@ -333,7 +333,7 @@ public static class Instances { * * @return A functor for Lists */ - public static Functor functor(){ + public static Functor functor(){ BiFunction,Function,ListKind> map = Instances::map; return General.functor(map); } @@ -352,8 +352,8 @@ public static class Instances { * * @return A factory for Lists */ - public static Pure unit(){ - return General.unit(Instances::of); + public static Pure unit(){ + return General.unit(Instances::of); } /** * @@ -392,7 +392,7 @@ public static class Instances { * * @return A zipper for Lists */ - public static Applicative zippingApplicative(){ + public static Applicative zippingApplicative(){ BiFunction>,ListKind,ListKind> ap = Instances::ap; return General.applicative(functor(), unit(), ap); } @@ -422,9 +422,9 @@ public static class Instances { * * @return Type class with monad functions for Lists */ - public static Monad monad(){ + public static Monad monad(){ - BiFunction,Function>,Higher> flatMap = Instances::flatMap; + BiFunction,Function>,Higher> flatMap = Instances::flatMap; return General.monad(zippingApplicative(), flatMap); } /** @@ -444,7 +444,7 @@ public static class Instances { * * @return A filterable monad (with default value) */ - public static MonadZero monadZero(){ + public static MonadZero monadZero(){ return General.monadZero(monad(), ListKind.widen(List.list())); } @@ -460,9 +460,9 @@ public static class Instances { * * @return Type class for combining Lists by concatenation */ - public static MonadPlus monadPlus(){ + public static MonadPlus monadPlus(){ Monoid> m = Monoid.of(ListKind.widen(List.list()), Instances::concat); - Monoid> m2= (Monoid)m; + Monoid> m2= (Monoid)m; return General.monadPlus(monadZero(),m2); } /** @@ -481,15 +481,15 @@ public static class Instances { * @param m Monoid to use for combining Lists * @return Type class for combining Lists */ - public static MonadPlus monadPlus(Monoid> m){ - Monoid> m2= (Monoid)m; + public static MonadPlus monadPlus(Monoid> m){ + Monoid> m2= (Monoid)m; return General.monadPlus(monadZero(),m2); } /** * @return Type class for traversables with traverse / sequence operations */ - public static Traverse traverse(){ + public static Traverse traverse(){ BiFunction,ListKind>,Higher>> sequenceFn = (ap, list) -> { @@ -507,7 +507,7 @@ public static class Instances { }; - BiFunction,Higher>,Higher>> sequenceNarrow = + BiFunction,Higher>,Higher>> sequenceNarrow = (a,b) -> ListKind.widen2(sequenceFn.apply(a, ListKind.narrowK(b))); return General.traverse(zippingApplicative(), sequenceNarrow); } @@ -528,9 +528,9 @@ public static class Instances { * * @return Type class for folding / reduction operations */ - public static Foldable foldable(){ - BiFunction,Higher,T> foldRightFn = (m, l)-> ListX.fromIterable(ListKind.narrow(l)).foldRight(m); - BiFunction,Higher,T> foldLeftFn = (m, l)-> ListX.fromIterable(ListKind.narrow(l)).reduce(m); + public static Foldable foldable(){ + BiFunction,Higher,T> foldRightFn = (m, l)-> ListX.fromIterable(ListKind.narrow(l)).foldRight(m); + BiFunction,Higher,T> foldLeftFn = (m, l)-> ListX.fromIterable(ListKind.narrow(l)).reduce(m); return General.foldable(foldRightFn, foldLeftFn); } @@ -545,7 +545,7 @@ private static ListKind ap(ListKind> lt, ListKind li return ListKind.widen(lt.zipWith(list.narrow(),(a, b)->a.apply(b))); } - private static Higher flatMap(Higher lt, Function> fn){ + private static Higher flatMap(Higher lt, Function> fn){ return ListKind.widen(ListKind.narrow(lt).bind(in->fn.andThen(ListKind::narrow).apply(in))); } private static ListKind map(ListKind lt, Function fn){ diff --git a/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/NonEmptyLists.java b/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/NonEmptyLists.java index 62bbfaa6..4ce14708 100644 --- a/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/NonEmptyLists.java +++ b/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/NonEmptyLists.java @@ -197,7 +197,7 @@ public static class Instances { * * @return A functor for NonEmptyLists */ - public static Functor functor(){ + public static Functor functor(){ BiFunction,Function,NonEmptyListKind> map = Instances::map; return General.functor(map); } @@ -216,8 +216,8 @@ public static class Instances { * * @return A factory for NonEmptyLists */ - public static Pure unit(){ - return General.unit(Instances::of); + public static Pure unit(){ + return General.unit(Instances::of); } /** * @@ -256,7 +256,7 @@ public static class Instances { * * @return A zipper for NonEmptyLists */ - public static Applicative zippingApplicative(){ + public static Applicative zippingApplicative(){ BiFunction>,NonEmptyListKind,NonEmptyListKind> ap = Instances::ap; return General.applicative(functor(), unit(), ap); } @@ -286,9 +286,9 @@ public static class Instances { * * @return Type class with monad functions for NonEmptyLists */ - public static Monad monad(){ + public static Monad monad(){ - BiFunction,Function>,Higher> flatMap = Instances::flatMap; + BiFunction,Function>,Higher> flatMap = Instances::flatMap; return General.monad(zippingApplicative(), flatMap); } @@ -312,9 +312,9 @@ public static class Instances { * * @return Type class for folding / reduction operations */ - public static Foldable foldable(){ - BiFunction,Higher,T> foldRightFn = (m, l)-> ListX.fromIterable(NonEmptyListKind.narrow(l)).foldRight(m); - BiFunction,Higher,T> foldLeftFn = (m, l)-> ListX.fromIterable(NonEmptyListKind.narrow(l)).reduce(m); + public static Foldable foldable(){ + BiFunction,Higher,T> foldRightFn = (m, l)-> ListX.fromIterable(NonEmptyListKind.narrow(l)).foldRight(m); + BiFunction,Higher,T> foldLeftFn = (m, l)-> ListX.fromIterable(NonEmptyListKind.narrow(l)).reduce(m); return General.foldable(foldRightFn, foldLeftFn); } @@ -326,7 +326,7 @@ private static NonEmptyListKind ap(NonEmptyListKind> lt return NonEmptyListKind.widen(lt.zipWith(list.narrow().toList(),(a, b)->a.apply(b))); } - private static Higher flatMap(Higher lt, Function> fn){ + private static Higher flatMap(Higher lt, Function> fn){ return NonEmptyListKind.widen(NonEmptyListKind.narrow(lt).bind(in->fn.andThen(NonEmptyListKind::narrow).apply(in))); } private static NonEmptyListKind map(NonEmptyListKind lt, Function fn){ diff --git a/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/Options.java b/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/Options.java index 42a517df..c9ba327b 100644 --- a/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/Options.java +++ b/cyclops-functionaljava/src/main/java/cyclops/companion/functionaljava/Options.java @@ -619,7 +619,7 @@ public static class Instances { * * @return A functor for Options */ - public static Functor functor(){ + public static Functor