All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aol.cyclops.rx.adapter.ObservableAdapter Maven / Gradle / Ivy

There is a newer version: 9.0.0-MI7
Show newest version
package com.aol.cyclops.rx.adapter;


import cyclops.companion.rx.Observables;
import cyclops.monads.RxWitness.obsvervable;
import com.aol.cyclops2.types.extensability.AbstractFunctionalAdapter;
import cyclops.monads.AnyM;
import lombok.AllArgsConstructor;
import rx.Observable;


import java.util.function.Function;
import java.util.function.Predicate;


@AllArgsConstructor
public class ObservableAdapter extends AbstractFunctionalAdapter {





    @Override
    public  Iterable toIterable(AnyM t) {
        return ()-> observable(t).toBlocking().toIterable().iterator();
    }

    @Override
    public  AnyM ap(AnyM> fn, AnyM apply) {
        Observable f = observable(apply);
        Observable> fnF = observable(fn);
        Observable res = fnF.zipWith(f, (a, b) -> a.apply(b));
        return Observables.anyM(res);

    }

    @Override
    public  AnyM filter(AnyM t, Predicate fn) {
        return Observables.anyM(observable(t).filter(e->fn.test(e)));
    }

     Observable observable(AnyM anyM){
        ObservableReactiveSeq seq = anyM.unwrap();
        return seq.observable;
    }

    @Override
    public  AnyM empty() {
        return Observables.anyM(Observable.empty());
    }



    @Override
    public  AnyM flatMap(AnyM t,
                                     Function> fn) {
        return Observables.anyM(observable(t).flatMap(x->observable(fn.apply(x))));

    }

    @Override
    public  AnyM unitIterable(Iterable it)  {
        return Observables.anyM(Observable.from(it));
    }

    @Override
    public  AnyM unit(T o) {
        return Observables.anyM(Observable.just(o));
    }



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy