Git Product home page Git Product logo

cyclops-integration's Introduction

Integration modules home

screen shot 2016-02-22 at 8 44 42 pm

Documentation

Latest Articles

A common API across the functional landscape

cyclops provides a common set of APIs across the major functional libraries for Java. It does this via :

  1. A common abstraction layer (AnyM short for AnyMonad)
  2. 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
  3. Provided conversion classes between types via FromXXX and ToXXX classes
  4. Allows collection types from all major functional libraries to be substituted behind cyclops-react lazy & reactive collection APIs

AnyM : a type safe abstraction across Any Monadic type in Java.

Define ultra-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);

Common functionality in Companion classes

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];

Lazy and Reactive Collection APIs

cyclops & cyclops-react allow collection types from all major functional libraries to be used behind fast lazy & reactive collection apis.

What do we mean by fast lazy & reactive 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.

The Performance benefit

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-speed-up

Reactive APIs

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));

Type safe, higher kinded typeclassess

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)

Getting cyclops-react

  • Maven Central : cyclops-react

Gradle

where x.y.z represents the latest version

compile 'com.oath.simplereact:cyclops-react:x.y.z'

Maven

<dependency>
    <groupId>com.oath.simplereact</groupId>
    <artifactId>cyclops-react</artifactId>
    <version>x.y.z</version>
</dependency>

cyclops-integration's People

Contributors

johnmcclean avatar 0xflotus avatar c0nscience avatar jbgi avatar lukaseder avatar h3xstream avatar seanf avatar scr-oath avatar gitter-badger avatar awturner avatar bigkahuna1uk avatar earlzero avatar greg2001 avatar slnowak avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.