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

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

package com.aol.cyclops.vavr.adapter;


import com.aol.cyclops2.types.anyM.AnyMValue;
import com.aol.cyclops2.types.extensability.ValueAdapter;
import cyclops.conversion.vavr.FromCyclopsReact;
import cyclops.conversion.vavr.ToCyclopsReact;
import cyclops.monads.Vavr;
import cyclops.monads.VavrWitness;
import cyclops.monads.VavrWitness.either;
import com.aol.cyclops2.types.extensability.AbstractFunctionalAdapter;
import cyclops.control.Maybe;
import cyclops.control.Xor;
import cyclops.monads.AnyM;
import io.vavr.control.Either;
import lombok.AllArgsConstructor;

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


@AllArgsConstructor
public class EitherAdapter implements ValueAdapter {

    public  T get(AnyMValue t){
        return either(t).get();
    }

    @Override
    public  Iterable toIterable(AnyM t) {
        return Maybe.fromIterable(t.unwrap());
    }

    @Override
    public  AnyM ap(AnyM> fn, AnyM apply) {
        Either f = either(apply);
        Either> fnF = either(fn);
        Either res = FromCyclopsReact.either(ToCyclopsReact.xor(fnF).combine(ToCyclopsReact.xor(f), (a, b) -> a.apply(b)));
        return Vavr.either(res);

    }

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

     Either either(AnyM anyM){
        return anyM.unwrap();
    }

    @Override
    public  AnyM empty() {
        return Vavr.either(Either.left(null));
    }



    @Override
    public  AnyM flatMap(AnyM t,
                                     Function> fn) {
        return Vavr.either(either(t).flatMap(fn.andThen(a-> either(a))));

    }

    @Override
    public  AnyM unitIterable(Iterable it)  {

        return Vavr.either(FromCyclopsReact.either(Xor.fromIterable(it)));
    }

    @Override
    public  AnyM unit(T o) {
        return Vavr.either(Either.right(o));
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy