Skip to content

Commit 87f78bb

Browse files
committed
more documentation
1 parent afdae4b commit 87f78bb

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

cyclops-reactor/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ e.g. using the Pure and Functor typeclasses for Flux
360360
.convert(FluxKind::narrowK);
361361

362362

363-
assertThat(list.toJavaList(),equalTo(Arrays.asList("hello".length())));
363+
assertThat(list.collectList().block(),equalTo(Arrays.asList("hello".length())));
364364
```
365365

366366
### Via Active
@@ -392,7 +392,7 @@ Nested<mono,flux,Integer> monoFlux = MonoNested.list(Mono.just(Flux.just(1,10,2
392392

393393
Mono<Integer> opt = monoFlux.foldsUnsafe()
394394
.foldLeft(Monoids.intMax)
395-
.convert(OptionKind::narrowK);
395+
.convert(MonoKind::narrowK);
396396

397397

398398
//[200]

cyclops-rxjava2/readme.md

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,84 @@ System.out.println(anyM);
467467
```
468468

469469

470-
# Higher Kinded Types and Type classes
470+
## Using Typeclasses
471471

472-
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
472+
### Directly
473473

474-
e.g. using the Pure and Functor typeclasses for Vavr Streams
474+
Typeclasses can be used directly (although this results in verbose and somewhat cumbersome code)
475+
e.g. using the Pure and Functor typeclasses for Observable
476+
477+
```java
478+
479+
Pure<observable> pure = Observables.Instances.unit();
480+
Functor<observable> functor = Observables.Instances.functor();
481+
482+
ObservableKind<Integer> flux = pure.unit("hello")
483+
.applyHKT(h->functor.map((String v) ->v.length(), h))
484+
.convert(ObservableKind::narrowK);
485+
486+
487+
assertThat(list.toList().blockingGet(),equalTo(Arrays.asList("hello".length())));
488+
```
489+
490+
### Via Active
491+
492+
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
493+
494+
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
495+
496+
```java
497+
498+
Active<observable,Integer> active = Observables.allTypeClasses(Flux.empty());
499+
500+
Active<observable,Integer> hello = active.unit("hello")
501+
.map(String::length);
502+
503+
Observable<Integer> stream = ObservableKind.narrow(hello.getActive());
504+
505+
```
506+
507+
### Via Nested
508+
509+
The Nested class represents a Nested data structure, for example a Mono with a Flux *and* the associated typeclass instances for both types.
510+
511+
```java
512+
import cyclops.companion.rxjava2.Singles.SingleNested;
513+
514+
Nested<single,observable,Integer> singleObs = SingleNested.list(Single.just(Observable.just(1,10,2,3)))
515+
.map(i -> i * 20);
516+
517+
Single<Integer> opt = singleObs.foldsUnsafe()
518+
.foldLeft(Monoids.intMax)
519+
.convert(SingleKind::narrowK);
520+
521+
522+
//[200]
523+
524+
```
525+
526+
### Via Coproduct
527+
528+
Coproduct is a Sum type for HKT encoded types that also stores the associated type classes
529+
530+
```java
531+
import static
532+
Coproduct<observable,single,Integer> nums = Singles.coproduct(10,Observables.Instances.definitions());
533+
534+
535+
int value = nums.map(i->i*2)
536+
.foldUnsafe()
537+
.foldLeft(0,(a,b)->a+b);
538+
539+
//20
540+
541+
```
542+
543+
544+
# Higher Kinded examples
545+
546+
547+
e.g. using the Pure and Functor typeclasses for Observables
475548

476549
```java
477550

0 commit comments

Comments
 (0)