Skip to content

Commit afdae4b

Browse files
committed
improved documentation
1 parent c19baf7 commit afdae4b

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

cyclops-reactor/readme.md

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,81 @@ AnyM<list,Mono<Integer>> anyM = trans.unwrap();
343343
System.out.println(anyM);
344344
```
345345

346+
## Using Typeclasses
346347

347-
# Higher Kinded Types and Type classes
348+
### Directly
348349

349-
If you really want / or need to program at a much higher level of abstraction cyclops-reactor provided psuedo Higher Kinded encordings and typeclasses for Fluxs and Monos.
350-
This allows us to leverage truly generic Traverse and Sequence implementations (allowing us to invert the nesting of arbritrary monadic types, rather than hard coding the inversion of a List of Futures to a Future of a List - for example)
350+
Typeclasses can be used directly (although this results in verbose and somewhat cumbersome code)
351+
e.g. using the Pure and Functor typeclasses for Flux
352+
353+
```java
354+
355+
Pure<flux> pure = Fluxs.Instances.unit();
356+
Functor<flux> functor = Fluxs.Instances.functor();
357+
358+
FluxKind<Integer> flux = pure.unit("hello")
359+
.applyHKT(h->functor.map((String v) ->v.length(), h))
360+
.convert(FluxKind::narrowK);
361+
362+
363+
assertThat(list.toJavaList(),equalTo(Arrays.asList("hello".length())));
364+
```
365+
366+
### Via Active
367+
368+
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
369+
370+
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
371+
372+
```java
373+
374+
Active<flux,Integer> active = Fluxs.allTypeClasses(Flux.empty());
375+
376+
Active<flux,Integer> hello = active.unit("hello")
377+
.map(String::length);
378+
379+
Flux<Integer> stream = FluxKind.narrow(hello.getActive());
380+
381+
```
382+
383+
### Via Nested
384+
385+
The Nested class represents a Nested data structure, for example a Mono with a Flux *and* the associated typeclass instances for both types.
386+
387+
```java
388+
import cyclops.companion.reactor.Monos.MonoNested;
389+
390+
Nested<mono,flux,Integer> monoFlux = MonoNested.list(Mono.just(Flux.just(1,10,2,3)))
391+
.map(i -> i * 20);
392+
393+
Mono<Integer> opt = monoFlux.foldsUnsafe()
394+
.foldLeft(Monoids.intMax)
395+
.convert(OptionKind::narrowK);
396+
397+
398+
//[200]
399+
400+
```
401+
402+
### Via Coproduct
403+
404+
Coproduct is a Sum type for HKT encoded types that also stores the associated type classes
405+
406+
```java
407+
import static
408+
Coproduct<flux,mono,Integer> nums = Monos.coproduct(10,Fluxs.Instances.definitions());
409+
410+
411+
int value = nums.map(i->i*2)
412+
.foldUnsafe()
413+
.foldLeft(0,(a,b)->a+b);
414+
415+
//20
416+
417+
```
351418

352-
e.g. using traverse with Maybe and Mono types
353419

354-
## Higher Kinded encoding for Mono
420+
## Direct examples with Mono
355421

356422
We can use MonoKind to encode Mono at a higher level of abstraction.
357423
```java

0 commit comments

Comments
 (0)