- DSLs with the Free Monad in Java 8 : Part I
- Cross Library Stream Benchmarking : Playing Scrabble with Shakespeare
cyclops provides a common set of APIs across the major functional libraries for Java. It does this via :
- A common abstraction layer (AnyM short for AnyMonad)
- Companion classes (Optionals, Streams, CompletableFutures, Options, Eithers, Lists, Trys etc) that provide common functionality such as For Comprehensions and Higher Kinded Haskell like type classes
- Provided conversion classes between types via FromXXX and ToXXX classes
- Allows collection types from all major functional libraries to be substituted behind cyclops-react lazy & reactive collection APIs
Define ulta-generic code that can be used by types across Vavr, Reactor, cyclops-react, Guava, JDK, Functional Java, RxJava.
public <W extends WitnessType<W>> AnyMSeq<W,Integer> sumAdjacent(AnyMSeq<W,Integer> sequence){
return sequence.sliding(1)
.map(t->t.sum(i->i).get())
}
Use them with Vavr
import static cyclops.monads.VavrWitness.list;
import cyclops.companion.vavr.Lists;
AnyMSeq<list,Integer> vavrList = Lists.anyM(List.range(0, 10));
AnyMSeq<list,Integer> summedVavr = sumAdjacent(vavrList);
List<Integer> backToVavr = VavrWitness.list(summedVavr);
Or RxJava
import static cyclops.monads.Rx2Witness.observable;
import cyclops.companion.rx.Observables;
AnyMSeq<observable,Integer> rxObservable = Observables.anyM(Observable.range(0, 10));
AnyMSeq<observable,Integer> summedRx = sumAdjacent(rxObservable);
Observable<Integer> backToRx = RxWitness.observable(summedRx);
For comprehensions
import static cyclops.companion.vavr.Trys.*;
Try<String> result = Trys.forEach4(grind("arabica beans"),
ground -> heatWater(new Water(25)),
(ground, water) -> brew(ground, water),
(ground, water ,espresso) -> frothMilk("milk"),
(ground , water ,espresso , foam) -> combine(espresso, foam));
System.out.println(result.get());
Try<String> grind(String beans) {
return Try.of(() -> "ground coffee of " + beans);
}
Try<Water> heatWater(Water water) {
return Try.of(() -> water.withTemperature(85));
}
Try<String> frothMilk(String milk) {
return Try.of(() -> "frothed " + milk);
}
Try<String> brew(String coffee, Water heatedWater) {
return Try.of(() -> "espresso");
}
String combine(String espresso, String frothedMilk) {
return "cappuccino";
}
import static cyclops.companion.reactor.Fluxs.*;
Flux<Integer> result = Fluxs.forEach(Flux.just(10, 20), a -> Flux.<Integer> just(a + 10), (a, b) -> a + b);
result.collectList()
.block(),
//List[30, 50];
cyclops & cyclops-react allow collection types from all major functional libraries to be used behind fast lazy & reactive collection apis.
Most Collection APIs provided by the major functional libraries are eager. That means when a map transformation is invoked on a Functional Java or Vavr List it is executed immediately. Performing multiple chained operations often results in the the collection being processed (traversed) multiple different times. With cyclops map and other functional operations become lazy, and the your chain of commands are only executed when data within the collection is exected for the first time. This allows cyclops to process the entire chain of operations in a single traversal of the data. The resultant transformed collection is then stored in the underlying structure of your favourite functional collection API.
Leveraging cyclops can significantly improve the execution times of operations on collections.
The code for this performance testing speeding up the execution of functional operations on Javaslang Vectors is available here
cyclops also allows data to be pushed asynchronously into collection types from the major functional libraries. For example to asynchronously populate a JavaSlang / Vavr Vector we can write
VectorX<Integer> asyncPopulated = JavaSlangPersistentList.fromStream(Spouts.publishOn(ReactiveSeq.of(1,2,3),
Executors.newFixedThreadPool(1));
E.g. Using a functor and applicative type classes on FunctionalJava Lists (Higher Kinded encoding via ListKind type)
import static com.oath.cyclops.functionaljava.ListKind;
import static cyclops.companion.functionaljava.Lists.Instances.functor;
import static cyclops.companion.functionaljava.Lists.Instances.zippingApplicative;
ListKind<Function1<Integer,Integer>> listFn = ListKind.widen(List.list((Lambda.λ((Integer i) ->i*2))
.convert(ListKind::narrowK);
List<Integer> list = zippingApplicative().ap(listFn,functor().map((String v)->v.length(),
widen(List.list("hello"))))
.convert(ListKind::narrow);
//List.list("hello".length()*2))
There are a number of integration modules for cyclops-react, they are
This screencast gives an overview of how cyclops can help integrate and provide abstractions across the datatypes in the above libraries. [Unifying the cambrian explosion with cyclops-react ] (https://www.youtube.com/watch?v=YgzvpMbxiRo)
where x.y.z represents the latest version
compile 'com.oath.simplereact:cyclops-react:x.y.z'
<dependency>
<groupId>com.oath.simplereact</groupId>
<artifactId>cyclops-react</artifactId>
<version>x.y.z</version>
</dependency>