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

com.aol.cyclops.vavr.adapter.TraversableAdapter Maven / Gradle / Ivy

The newest version!
package com.aol.cyclops.vavr.adapter;

import cyclops.monads.VavrWitness;
import com.aol.cyclops2.types.extensability.AbstractFunctionalAdapter;
import cyclops.monads.AnyM;
import cyclops.stream.ReactiveSeq;
import io.vavr.collection.Traversable;
import lombok.AllArgsConstructor;

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


@AllArgsConstructor
public abstract class TraversableAdapter> extends AbstractFunctionalAdapter {


    W instance;
    @Override
    public  Iterable toIterable(AnyM t) {
        return ()->stream(t).iterator();
    }

    @Override
    public  AnyM ap(AnyM> fn, AnyM apply) {
        Traversable f = stream(apply);
        Traversable> fnF = stream(fn);
        return unitIterable(ReactiveSeq.fromIterable(fnF).zip(f, (a, b) -> a.apply(b)));

    }

    @Override
    public  AnyM filter(AnyM t, Predicate fn) {
        return anyM(stream(t).filter(fn));
    }

     Traversable stream(AnyM anyM){
        return anyM.unwrap();
    }
    public abstract  Traversable emptyTraversable();
    public abstract  Traversable singletonTraversable(T value);
    public abstract  Traversable traversableFromIterable(Iterable value);
    @Override
    public  AnyM empty() {
        return anyM(emptyTraversable());
    }

    private  AnyM anyM(Traversable t){
        return AnyM.ofSeq(t,instance);
    }



    @Override
    public  AnyM flatMap(AnyM t,
                                     Function> fn) {
        return anyM(stream(t).flatMap(a->stream(fn.apply(a))));

    }

    @Override
    public  AnyM unitIterable(Iterable it)  {
        return anyM(traversableFromIterable(it));
    }

    @Override
    public  AnyM unit(T o) {
        return anyM(singletonTraversable(o));
    }

    @Override
    public  AnyM map(AnyM t, Function fn) {
        return anyM(stream(t).map(fn));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy