
jedi.functors.ComposeableFunctor Maven / Gradle / Ivy
The newest version!
package jedi.functors;
import jedi.functional.Functor;
import jedi.functional.Functor0;
import jedi.functional.Functor2;
/**
* A functor that can be composed with other Functors.
* For example, given:
*
* Functor f that takes T and returns R
* Functor g that takes R and returns NEW_R
* new ComposeableFunctor(f).andThen(g).execute(x) == g(f(x))
*
* or with static imports:
* composeable(f).andThen(g)
* or
* composeable(f).o(g)
* or
* c(f).o(g)
*
* @param
* @param
*/
public class ComposeableFunctor implements Functor {
private final Functor functor;
public ComposeableFunctor(final Functor functor) {
this.functor = functor;
}
public R execute(final T value) {
return functor.execute(value);
}
/**
* Function composition: this.andThen(g) applied to x == g(this.execute(value))
*/
public ComposeableFunctor andThen(final Functor super R, NEW_R> g) {
return new ComposeableFunctor(new Functor() {
public NEW_R execute(T value) {
return g.execute(functor.execute(value));
}
});
}
/**
* Like the Haskell dot operator, this.o(g).execute(x) = this.execute(g.execute(x))
*/
public ComposeableFunctor o(Functor g) {
return composeable(g).andThen(functor);
}
public ComposeableFunctor0 o(Functor0 extends T> g) {
return composeable(g).andThen(functor);
}
public static ComposeableFunctor composeable(Functor functor) {
return new ComposeableFunctor(functor);
}
public static ComposeableFunctor c(Functor functor) {
return composeable(functor);
}
public static ComposeableFunctor0 composeable(Functor0 functor) {
return new ComposeableFunctor0(functor);
}
public static ComposeableFunctor0 c(Functor0 functor) {
return composeable(functor);
}
public static ComposeableFunctor2 composeable(Functor2 functor) {
return new ComposeableFunctor2(functor);
}
public static ComposeableFunctor2 c(Functor2 functor) {
return composeable(functor);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy