fj.F2Functions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionaljava Show documentation
Show all versions of functionaljava Show documentation
Functional Java is an open source library that supports closures for the Java programming language
package fj;
import fj.control.parallel.Promise;
import fj.data.*;
import fj.function.Try2;
import static fj.P.p;
import static fj.data.IterableW.wrap;
import static fj.data.Set.iterableSet;
import static fj.data.Tree.node;
import static fj.data.TreeZipper.treeZipper;
import static fj.data.Zipper.zipper;
/**
* Created by MarkPerry on 6/04/2014.
*/
public class F2Functions {
/**
* Partial application.
*
* @param a The A
to which to apply this function.
* @return The function partially applied to the given argument.
*/
static public F f(final F2 f, final A a) {
return b -> f.f(a, b);
}
/**
* Curries this wrapped function to a wrapped function of arity-1 that returns another wrapped function.
*
* @return a wrapped function of arity-1 that returns another wrapped function.
*/
static public F> curry(final F2 f) {
return a -> b -> f.f(a, b);
}
/**
* Flips the arguments of this function.
*
* @return A new function with the arguments of this function flipped.
*/
static public F2 flip(final F2 f) {
return (b, a) -> f.f(a, b);
}
/**
* Uncurries this function to a function on tuples.
*
* @return A new function that calls this function with the elements of a given tuple.
*/
static public F, C> tuple(final F2 f) {
return p -> f.f(p._1(), p._2());
}
/**
* Promotes this function to a function on Arrays.
*
* @return This function promoted to transform Arrays.
*/
static public F2, Array, Array> arrayM(final F2 f) {
return (a, b) -> a.bind(b, curry(f));
}
/**
* Promotes this function to a function on Promises.
*
* @return This function promoted to transform Promises.
*/
static public F2, Promise, Promise> promiseM(final F2 f) {
return (a, b) -> a.bind(b, curry(f));
}
/**
* Promotes this function to a function on Iterables.
*
* @return This function promoted to transform Iterables.
*/
static public F2, Iterable, IterableW> iterableM(final F2 f) {
return (a, b) -> IterableW.liftM2(curry(f)).f(a).f(b);
}
/**
* Promotes this function to a function on Lists.
*
* @return This function promoted to transform Lists.
*/
static public F2, List, List> listM(final F2 f) {
return (a, b) -> List.liftM2(curry(f)).f(a).f(b);
}
/**
* Promotes this function to a function on non-empty lists.
*
* @return This function promoted to transform non-empty lists.
*/
static public F2, NonEmptyList, NonEmptyList> nelM(final F2 f) {
return (as, bs) -> NonEmptyList.fromList(as.toList().bind(bs.toList(), f)).some();
}
/**
* Promotes this function to a function on Options.
*
* @return This function promoted to transform Options.
*/
static public F2