com.aol.cyclops.vavr.adapter.TraversableAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cyclops-vavr Show documentation
Show all versions of cyclops-vavr Show documentation
Converters and Comprehenders for Javaslang
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 extends Function super T, ? extends R>> fnF = stream(fn);
return unitIterable(ReactiveSeq.fromIterable(fnF).zip(f, (a, b) -> a.apply(b)));
}
@Override
public AnyM filter(AnyM t, Predicate super T> 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 super T, ? extends AnyM> 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 super T, ? extends R> fn) {
return anyM(stream(t).map(fn));
}
}