Skip to content

Commit

Permalink
more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmcclean committed Jul 6, 2017
1 parent afdae4b commit 87f78bb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cyclops-reactor/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -392,7 +392,7 @@ Nested<mono,flux,Integer> monoFlux = MonoNested.list(Mono.just(Flux.just(1,10,2

Mono<Integer> opt = monoFlux.foldsUnsafe()
.foldLeft(Monoids.intMax)
.convert(OptionKind::narrowK);
.convert(MonoKind::narrowK);


//[200]
Expand Down
79 changes: 76 additions & 3 deletions cyclops-rxjava2/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<observable> pure = Observables.Instances.unit();
Functor<observable> functor = Observables.Instances.functor();

ObservableKind<Integer> 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<observable,Integer> active = Observables.allTypeClasses(Flux.empty());

Active<observable,Integer> hello = active.unit("hello")
.map(String::length);

Observable<Integer> 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<single,observable,Integer> singleObs = SingleNested.list(Single.just(Observable.just(1,10,2,3)))
.map(i -> i * 20);

Single<Integer> 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<observable,single,Integer> 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

Expand Down

0 comments on commit 87f78bb

Please sign in to comment.