diff --git a/cyclops-reactor/readme.md b/cyclops-reactor/readme.md index d81769b8..5d8cf39c 100644 --- a/cyclops-reactor/readme.md +++ b/cyclops-reactor/readme.md @@ -360,7 +360,7 @@ e.g. using the Pure and Functor typeclasses for Flux .convert(FluxKind::narrowK); - assertThat(list.toJavaList(),equalTo(Arrays.asList("hello".length()))); + assertThat(list.collectList().block(),equalTo(Arrays.asList("hello".length()))); ``` ### Via Active @@ -392,7 +392,7 @@ Nested monoFlux = MonoNested.list(Mono.just(Flux.just(1,10,2 Mono opt = monoFlux.foldsUnsafe() .foldLeft(Monoids.intMax) - .convert(OptionKind::narrowK); + .convert(MonoKind::narrowK); //[200] diff --git a/cyclops-rxjava2/readme.md b/cyclops-rxjava2/readme.md index 7b5f34c1..7d510996 100644 --- a/cyclops-rxjava2/readme.md +++ b/cyclops-rxjava2/readme.md @@ -467,11 +467,84 @@ System.out.println(anyM); ``` -# Higher Kinded Types and Type classes +## Using Typeclasses -If you really want / or need to program at a much higher level of abstraction cyclops-rx provided psuedo Higher Kinded encordings and typeclasses for Observables, Flowables, Singles and Maybes +### Directly -e.g. using the Pure and Functor typeclasses for Vavr Streams +Typeclasses can be used directly (although this results in verbose and somewhat cumbersome code) +e.g. using the Pure and Functor typeclasses for Observable + +```java + + Pure pure = Observables.Instances.unit(); + Functor functor = Observables.Instances.functor(); + + ObservableKind flux = pure.unit("hello") + .applyHKT(h->functor.map((String v) ->v.length(), h)) + .convert(ObservableKind::narrowK); + + + assertThat(list.toList().blockingGet(),equalTo(Arrays.asList("hello".length()))); +``` + +### Via Active + +The Active class represents a Higher Kinded encoding of a Reactor (or cyclops-react/ JDK/ Vavr / rx etc) type *and* it's associated type classes + +The code above which creates a new Flux containing a single element "hello" and transforms it to a Flux of Integers (the length of each word), can be written much more succintly with Active + +```java + +Active active = Observables.allTypeClasses(Flux.empty()); + +Active hello = active.unit("hello") + .map(String::length); + +Observable stream = ObservableKind.narrow(hello.getActive()); + +``` + +### Via Nested + +The Nested class represents a Nested data structure, for example a Mono with a Flux *and* the associated typeclass instances for both types. + +```java +import cyclops.companion.rxjava2.Singles.SingleNested; + +Nested singleObs = SingleNested.list(Single.just(Observable.just(1,10,2,3))) + .map(i -> i * 20); + +Single opt = singleObs.foldsUnsafe() + .foldLeft(Monoids.intMax) + .convert(SingleKind::narrowK); + + +//[200] + +``` + +### Via Coproduct + +Coproduct is a Sum type for HKT encoded types that also stores the associated type classes + +```java +import static +Coproduct nums = Singles.coproduct(10,Observables.Instances.definitions()); + + +int value = nums.map(i->i*2) + .foldUnsafe() + .foldLeft(0,(a,b)->a+b); + +//20 + +``` + + +# Higher Kinded examples + + +e.g. using the Pure and Functor typeclasses for Observables ```java