/** * A first-class version of the toEither method. * * @return A function that returns an either projection of a given optional value, given a value to * return in left. */ public static <A, X> F<Option<A>, F<X, Either<X, A>>> toEither() { return curry(Option::toEither); }
/** * A first-class version of the zipWith function. * * @return a function that zips two given streams with a given function. */ public static <A, B, C> F<Stream<A>, F<Stream<B>, F<F<A, F<B, C>>, Stream<C>>>> zipWith() { return curry((F3<Stream<A>, Stream<B>, F<A, F<B, C>>, Stream<C>>) Stream::zipWith); }
/** * A first-class version of the iterate function. * * @return A function that returns a stream constructed by applying a given iteration function * starting at a given value. */ public static <A> F<F<A, A>, F<A, Stream<A>>> iterate() { return curry(Stream::iterate); }
/** * First-class constructor for tree zippers. * * @return A function that returns a new tree zipper, given a selected tree, left and right siblings, * and a parent context. */ public static <A> F<Tree<A>, F<Stream<Tree<A>>, F<Stream<Tree<A>>, F<Stream<P3<Stream<Tree<A>>, A, Stream<Tree<A>>>>, TreeZipper<A>>>>> treeZipper() { return curry( TreeZipper::treeZipper); }
/** * A first-class map function. * * @return A function that maps a given function across a given optional value. */ public static <A, B> F<F<A, B>, F<Option<A>, Option<B>>> map() { return curry((abf, option) -> option.map(abf)); }
/** * Provides a first-class version of foldLeft. * * @return The left fold function for lists. */ public static <A, B> F<F<B, F<A, B>>, F<B, F<List<A>, B>>> foldLeft() { return curry((f, b, as) -> as.foldLeft(f, b)); }
/** * Provides a first-class version of take. * * @return First-class version of take. */ public static <A> F<Integer, F<List<A>, List<A>>> take() { return curry((n, as) -> as.take(n)); }
/** * Performs a left-fold accummulating and returns an array of the intermediate results. * This function runs in constant stack space. * * @param f The function to apply on each argument pair (initial value/previous result and next array element) * @param b The beginning value to start the application from. * @return The array containing all intermediate results of the left-fold reduction. */ public <B> Array<B> scanLeft(final F2<B, A, B> f, final B b) { return scanLeft(curry(f), b); }
/** * First-class insertion function. * * @return A function that inserts a given element into a given set. */ public static <A> F<A, F<Set<A>, Set<A>>> insert() { return curry((a, set) -> set.insert(a)); }
/** * First-class function that maps a concurrent function over a Stream inside a promise. * * @return a function that maps a concurrent function over a Stream inside a promise. */ public <A, B> F<F<A, Promise<B>>, F<Stream<A>, Promise<Stream<B>>>> mapStream() { return curry((f, stream) -> mapM(stream, f)); }
/** * A first-class function that maps another function across a stream in parallel. * * @return A function that maps another function across a stream in parallel. */ public <A, B> F<F<A, B>, F<Stream<A>, Promise<Stream<B>>>> parMapStream() { return curry((abf, stream) -> parMap(stream, abf)); }
/** * A first-class function that maps another function across an array in parallel. * * @return A function that maps another function across an array in parallel. */ public <A, B> F<F<A, B>, F<Array<A>, Promise<Array<B>>>> parMapArray() { return curry((abf, array) -> parMap(array, abf)); }
/** * Provides a first-class version of bind() * * @return The bind function for lists. */ public static <A, B> F<F<A, List<B>>, F<List<A>, List<B>>> bind_() { return curry((f, as) -> as.bind(f)); }
/** * Performs a left-fold accummulating using first array element as a starting value * and returns an array of the intermediate results. * It will fail for empty arrays. * This function runs in constant stack space. * * @param f The function to apply on each argument pair (next array element and first array element/previous result) * @return The array containing all intermediate results of the left-fold reduction. */ public Array<A> scanLeft1(final F2<A, A, A> f) { return scanLeft1(curry(f)); }
/** * Performs a right-fold accummulating using last array element as a starting value * and returns an array of the intermediate results. * It will fail for empty arrays. * This function runs in constant stack space. * * @param f The function to apply on each argument pair (previous array element and last array element/previous result) * @return The array containing all intermediate results of the right-fold reduction. */ public Array<A> scanRight1(final F2<A, A, A> f) { return scanRight1(curry(f)); }
/** * Binds the given function across each element of this array and the given array with a final * join. * * @param sb A given array to bind the given function with. * @param f The function to apply to each element of this array and the given array. * @return A new array after performing the map, then final join. */ public <B, C> Array<C> bind(final Array<B> sb, final F2<A, B, C> f) { return bind(sb, curry(f)); }
/** * Uses an association list to perform a lookup with equality and returns a function that can be applied to a default, * followed by the associated key to return a value. * * @param x The association list. * @param eq The equality for the association list keys. * @return A function that can be applied to a default value (there is no association) and an associated key. */ public static <A, B> F<P1<B>, F<A, B>> associationLazy(final List<P2<A, B>> x, final Equal<A> eq) { return curry((def, a) -> lookup(eq, x, a).orSome(def)); } }
/** * The catamorphism for Iterables, implemented as a right fold. * * @param f The function with which to fold the wrapped iterable. * @param z The base case value of the destination type, applied last (rightmost) to the fold. * @return The result of the catamorphism. */ public <B> B foldRight(final F2<A, B, B> f, final B z) { final F<B, B> id = identity(); return foldLeft(curry((k, a, b) -> k.f(f.f(a, b))), id).f(z); }
/** * Turn a stream of functions into a function returning a stream. * * @param fs The stream of functions to sequence into a single function that returns a stream. * @return A function that, when given an argument, applies all the functions in the given stream to it * and returns a stream of the results. */ public static <A, B> F<B, Stream<A>> sequence_(final Stream<F<B, A>> fs) { return fs.foldRight((baf, p1) -> Function.bind(baf, p1._1(), curry((a, stream) -> cons(a, p(stream)))), Function .constant(Stream.nil())); }
/** * Maps a function across a Zipper in parallel. * * @param za A Zipper to map across in parallel. * @param f A function to map across the given Zipper. * @return A promise of a new Zipper with the given function applied to each element. */ public <A, B> Promise<Zipper<B>> parMap(final Zipper<A> za, final F<A, B> f) { return parMap(za.rights(), f) .apply(promise(f).f(za.focus()).apply(parMap(za.lefts(), f).fmap(curry(Zipper.zipper())))); }