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

fj.F1W Maven / Gradle / Ivy

Go to download

Functional Java is an open source library that supports closures for the Java programming language

There is a newer version: 5.0
Show newest version
package fj;

import fj.control.parallel.Actor;
import fj.control.parallel.Promise;
import fj.control.parallel.Strategy;
import fj.data.*;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.TreeSet;
import java.util.concurrent.*;

/**
 * Created by MarkPerry on 22/01/2015.
 */
public abstract class F1W implements F {

    /**
     * Function composition
     *
     * @param g A function to compose with this one.
     * @return The composed function such that this function is applied last.
     */
    public  F1W o(final F g) {
        return lift(F1Functions.o(this, g));
    }

    /**
     * First-class function composition
     *
     * @return A function that composes this function with another.
     */
    public  F1W, F> o() {
        return lift(F1Functions.o(this));
    }

    /**
     * Function composition flipped.
     *
     * @param g A function with which to compose this one.
     * @return The composed function such that this function is applied first.
     */
    @SuppressWarnings({"unchecked"})
    public  F1W andThen(final F g) {
        return lift(F1Functions.andThen(this, g));
    }

    /**
     * First-class composition flipped.
     *
     * @return A function that invokes this function and then a given function on the result.
     */
    public  F1W, F> andThen() {
        return lift( F1Functions.andThen(this));
    }

    /**
     * Binds a given function across this function (Reader Monad).
     *
     * @param g A function that takes the return value of this function as an argument, yielding a new function.
     * @return A function that invokes this function on its argument and then the given function on the result.
     */
    public  F1W bind(final F> g) {
        return lift(F1Functions.bind(this, g));
    }


    /**
     * First-class function binding.
     *
     * @return A function that binds another function across this function.
     */
    public  F1W>, F> bind() {
        return lift(F1Functions.bind(this));
    }

    /**
     * Function application in an environment (Applicative Functor).
     *
     * @param g A function with the same argument type as this function, yielding a function that takes the return
     *          value of this function.
     * @return A new function that invokes the given function on its argument, yielding a new function that is then
     *         applied to the result of applying this function to the argument.
     */
    public  F1W apply(final F> g) {
        return lift(F1Functions.apply(this, g));
    }


    /**
     * First-class function application in an environment.
     *
     * @return A function that applies a given function within the environment of this function.
     */
    public  F1W>, F> apply() {
        return lift(F1Functions.apply(this));
    }

    /**
     * Applies this function over the arguments of another function.
     *
     * @param g The function over whose arguments to apply this function.
     * @return A new function that invokes this function on its arguments before invoking the given function.
     */
    public  F1W> on(final F> g) {
        return lift(F1Functions.on(this, g));
    }


    /**
     * Applies this function over the arguments of another function.
     *
     * @return A function that applies this function over the arguments of another function.
     */
    public  F1W>, F>> on() {
        return lift(F1Functions.on(this));
    }

    /**
     * Promotes this function so that it returns its result in a product-1. Kleisli arrow for P1.
     *
     * @return This function promoted to return its result in a product-1.
     */
    public F1W> lazy() {
        return lift(F1Functions.lazy(this));
    }


    /**
     * Partial application.
     *
     * @param a The A to which to apply this function.
     * @return The function partially applied to the given argument to return a lazy value.
     */
    public P1 lazy(final A a) {
        return F1Functions.f(this, a);
    }

    /**
     * Promotes this function to map over a product-1.
     *
     * @return This function promoted to map over a product-1.
     */
    public F1W, P1> mapP1() {
        return lift(F1Functions.mapP1(this));
    }

    /**
     * Promotes this function so that it returns its result in an Option. Kleisli arrow for Option.
     *
     * @return This function promoted to return its result in an Option.
     */
    public F1W> optionK() {
        return lift(F1Functions.optionK(this));
    }


    /**
     * Promotes this function to map over an optional value.
     *
     * @return This function promoted to map over an optional value.
     */
    public F1W, Option> mapOption() {
        return lift(F1Functions.mapOption(this));
    }

    /**
     * Promotes this function so that it returns its result in a List. Kleisli arrow for List.
     *
     * @return This function promoted to return its result in a List.
     */
    public F1W> listK() {
        return lift( F1Functions.listK(this));
    }

    /**
     * Promotes this function to map over a List.
     *
     * @return This function promoted to map over a List.
     */
    public F1W, List> mapList() {
        return lift(F1Functions.mapList(this));
    }

    /**
     * Promotes this function so that it returns its result in a Stream. Kleisli arrow for Stream.
     *
     * @return This function promoted to return its result in a Stream.
     */
    public F1W> streamK() {
        return lift(F1Functions.streamK(this));
    }

    /**
     * Promotes this function to map over a Stream.
     *
     * @return This function promoted to map over a Stream.
     */
    public F1W, Stream> mapStream() {
        return lift(F1Functions.mapStream(this));
    }

    /**
     * Promotes this function so that it returns its result in a Array. Kleisli arrow for Array.
     *
     * @return This function promoted to return its result in a Array.
     */
    public F1W> arrayK() {
        return lift(F1Functions.arrayK(this));

    }

    /**
     * Promotes this function to map over a Array.
     *
     * @return This function promoted to map over a Array.
     */
    public F1W, Array> mapArray() {
        return lift(F1Functions.mapArray(this));
    }

    /**
     * Returns a function that contramaps over a given actor.
     *
     * @return A function that contramaps over a given actor.
     */
    public F1W, Actor> contramapActor() {
        return lift(F1Functions.contramapActor(this));
    }

    /**
     * Promotes this function to a concurrent function that returns a Promise of a value.
     *
     * @param s A parallel strategy for concurrent execution.
     * @return A concurrent function that returns a Promise of a value.
     */
    public F1W> promiseK(final Strategy s) {
        return lift(F1Functions.promiseK(this, s));
    }

    /**
     * Promotes this function to map over a Promise.
     *
     * @return This function promoted to map over Promises.
     */
    public F1W, Promise> mapPromise() {
        return lift(F1Functions.mapPromise(this));
    }

    /**
     * Promotes this function so that it returns its result on the left side of an Either.
     * Kleisli arrow for the Either left projection.
     *
     * @return This function promoted to return its result on the left side of an Either.
     */
    @SuppressWarnings({"unchecked"})
    public  F1W> eitherLeftK() {
        return lift(F1Functions.eitherLeftK(this));
    }

    /**
     * Promotes this function so that it returns its result on the right side of an Either.
     * Kleisli arrow for the Either right projection.
     *
     * @return This function promoted to return its result on the right side of an Either.
     */
    @SuppressWarnings({"unchecked"})
    public  F1W> eitherRightK() {
        return lift(F1Functions.eitherRightK(this));
    }

    /**
     * Promotes this function to map over the left side of an Either.
     *
     * @return This function promoted to map over the left side of an Either.
     */
    @SuppressWarnings({"unchecked"})
    public  F1W, Either> mapLeft() {
        return lift(F1Functions.mapLeft(this));
    }

    /**
     * Promotes this function to map over the right side of an Either.
     *
     * @return This function promoted to map over the right side of an Either.
     */
    @SuppressWarnings({"unchecked"})
    public  F1W, Either> mapRight() {
        return lift(F1Functions.mapRight(this));
    }

    /**
     * Returns a function that returns the left side of a given Either, or this function applied to the right side.
     *
     * @return a function that returns the left side of a given Either, or this function applied to the right side.
     */
    public F1W, B> onLeft() {
        return lift(F1Functions.onLeft(this));
    }

    /**
     * Returns a function that returns the right side of a given Either, or this function applied to the left side.
     *
     * @return a function that returns the right side of a given Either, or this function applied to the left side.
     */
    public F1W, B> onRight() {
        return lift(F1Functions.onRight(this));
    }

    /**
     * Promotes this function to return its value in an Iterable.
     *
     * @return This function promoted to return its value in an Iterable.
     */
    @SuppressWarnings({"unchecked"})
    public F1W> iterableK() {
        return lift( F1Functions.iterableK(this));
    }

    /**
     * Promotes this function to map over Iterables.
     *
     * @return This function promoted to map over Iterables.
     */
    @SuppressWarnings({"unchecked"})
    public F1W, IterableW> mapIterable() {
        return lift( F1Functions.mapIterable(this));
    }

    /**
     * Promotes this function to return its value in a NonEmptyList.
     *
     * @return This function promoted to return its value in a NonEmptyList.
     */
    @SuppressWarnings({"unchecked"})
    public F1W> nelK() {
        return lift(F1Functions.nelK(this));
    }

    /**
     * Promotes this function to map over a NonEmptyList.
     *
     * @return This function promoted to map over a NonEmptyList.
     */
    public F1W, NonEmptyList> mapNel() {
        return lift(F1Functions.mapNel(this));
    }

    /**
     * Promotes this function to return its value in a Set.
     *
     * @param o An order for the set.
     * @return This function promoted to return its value in a Set.
     */
    public F1W> setK(final Ord o) {
        return lift(F1Functions.setK(this, o));
    }

    /**
     * Promotes this function to map over a Set.
     *
     * @param o An order for the resulting set.
     * @return This function promoted to map over a Set.
     */
    public F1W, Set> mapSet(final Ord o) {
        return lift(F1Functions.mapSet(this, o));
    }

    /**
     * Promotes this function to return its value in a Tree.
     *
     * @return This function promoted to return its value in a Tree.
     */
    public F1W> treeK() {
        return lift(F1Functions.treeK(this));
    }

    /**
     * Promotes this function to map over a Tree.
     *
     * @return This function promoted to map over a Tree.
     */
    @SuppressWarnings({"unchecked"})
    public F1W, Tree> mapTree() {
        return lift(F1Functions.mapTree(this));
    }

    /**
     * Returns a function that maps this function over a tree and folds it with the given monoid.
     *
     * @param m The monoid with which to fold the mapped tree.
     * @return a function that maps this function over a tree and folds it with the given monoid.
     */
    public F1W, B> foldMapTree(final Monoid m) {
        return lift(F1Functions.foldMapTree(this, m));
    }

    /**
     * Promotes this function to return its value in a TreeZipper.
     *
     * @return This function promoted to return its value in a TreeZipper.
     */
    public F1W> treeZipperK() {
        return lift(F1Functions.treeZipperK(this));
    }

    /**
     * Promotes this function to map over a TreeZipper.
     *
     * @return This function promoted to map over a TreeZipper.
     */
    public F1W, TreeZipper> mapTreeZipper() {
        return lift(F1Functions.mapTreeZipper(this));
    }

    /**
     * Promotes this function so that it returns its result on the failure side of a Validation.
     * Kleisli arrow for the Validation failure projection.
     *
     * @return This function promoted to return its result on the failure side of a Validation.
     */
    public  F1W> failK() {
        return lift(F1Functions.failK(this));
    }

    /**
     * Promotes this function so that it returns its result on the success side of an Validation.
     * Kleisli arrow for the Validation success projection.
     *
     * @return This function promoted to return its result on the success side of an Validation.
     */
    public  F1W> successK() {
        return lift( F1Functions.successK(this));
    }

    /**
     * Promotes this function to map over the failure side of a Validation.
     *
     * @return This function promoted to map over the failure side of a Validation.
     */
    public  F1W, Validation> mapFail() {
        return lift(F1Functions.mapFail(this));
    }

    /**
     * Promotes this function to map over the success side of a Validation.
     *
     * @return This function promoted to map over the success side of a Validation.
     */
    public  F1W, Validation> mapSuccess() {
        return lift(F1Functions.mapSuccess(this));
    }

    /**
     * Returns a function that returns the failure side of a given Validation,
     * or this function applied to the success side.
     *
     * @return a function that returns the failure side of a given Validation,
     *         or this function applied to the success side.
     */
    public F1W, B> onFail() {
        return lift(F1Functions.onFail(this));
    }

    /**
     * Returns a function that returns the success side of a given Validation,
     * or this function applied to the failure side.
     *
     * @return a function that returns the success side of a given Validation,
     *         or this function applied to the failure side.
     */
    public F1W, B> onSuccess() {
        return lift(F1Functions.onSuccess(this));
    }

    /**
     * Promotes this function to return its value in a Zipper.
     *
     * @return This function promoted to return its value in a Zipper.
     */
    public F1W> zipperK() {
        return lift(F1Functions.zipperK(this));
    }

    /**
     * Promotes this function to map over a Zipper.
     *
     * @return This function promoted to map over a Zipper.
     */
    public F1W, Zipper> mapZipper() {
        return lift(F1Functions.mapZipper(this));
    }

    /**
     * Promotes this function to map over an Equal as a contravariant functor.
     *
     * @return This function promoted to map over an Equal as a contravariant functor.
     */
    public F1W, Equal> contramapEqual() {
        return lift(F1Functions.contramapEqual(this));
    }

    /**
     * Promotes this function to map over a Hash as a contravariant functor.
     *
     * @return This function promoted to map over a Hash as a contravariant functor.
     */
    public F1W, Hash> contramapHash() {
        return lift(F1Functions.contramapHash(this));
    }

    /**
     * Promotes this function to map over a Show as a contravariant functor.
     *
     * @return This function promoted to map over a Show as a contravariant functor.
     */
    public F1W, Show> contramapShow() {
        return lift(F1Functions.contramapShow(this));
    }

    /**
     * Promotes this function to map over the first element of a pair.
     *
     * @return This function promoted to map over the first element of a pair.
     */
    public  F1W, P2> mapFst() {
        return lift(F1Functions.mapFst(this));
    }

    /**
     * Promotes this function to map over the second element of a pair.
     *
     * @return This function promoted to map over the second element of a pair.
     */
    public  F1W, P2> mapSnd() {
        return lift(F1Functions.mapSnd(this));
    }

    /**
     * Promotes this function to map over both elements of a pair.
     *
     * @return This function promoted to map over both elements of a pair.
     */
    public F1W, P2> mapBoth() {
        return lift(F1Functions.mapBoth(this));
    }

    /**
     * Maps this function over a SynchronousQueue.
     *
     * @param as A SynchronousQueue to map this function over.
     * @return A new SynchronousQueue with this function applied to each element.
     */
    public SynchronousQueue mapJ(final SynchronousQueue as) {
        return F1Functions.mapJ(this, as);
    }


    /**
     * Maps this function over a PriorityBlockingQueue.
     *
     * @param as A PriorityBlockingQueue to map this function over.
     * @return A new PriorityBlockingQueue with this function applied to each element.
     */
    public PriorityBlockingQueue mapJ(final PriorityBlockingQueue as) {
        return F1Functions.mapJ(this, as);
    }

    /**
     * Maps this function over a LinkedBlockingQueue.
     *
     * @param as A LinkedBlockingQueue to map this function over.
     * @return A new LinkedBlockingQueue with this function applied to each element.
     */
    public LinkedBlockingQueue mapJ(final LinkedBlockingQueue as) {
        return F1Functions.mapJ(this, as);
    }

    /**
     * Maps this function over a CopyOnWriteArraySet.
     *
     * @param as A CopyOnWriteArraySet to map this function over.
     * @return A new CopyOnWriteArraySet with this function applied to each element.
     */
    public CopyOnWriteArraySet mapJ(final CopyOnWriteArraySet as) {
        return F1Functions.mapJ(this, as);
    }

    /**
     * Maps this function over a CopyOnWriteArrayList.
     *
     * @param as A CopyOnWriteArrayList to map this function over.
     * @return A new CopyOnWriteArrayList with this function applied to each element.
     */
    public CopyOnWriteArrayList mapJ(final CopyOnWriteArrayList as) {
        return F1Functions.mapJ(this, as);
    }

    /**
     * Maps this function over a ConcurrentLinkedQueue.
     *
     * @param as A ConcurrentLinkedQueue to map this function over.
     * @return A new ConcurrentLinkedQueue with this function applied to each element.
     */
    public ConcurrentLinkedQueue mapJ(final ConcurrentLinkedQueue as) {
        return F1Functions.mapJ(this, as);
    }

    /**
     * Maps this function over an ArrayBlockingQueue.
     *
     * @param as An ArrayBlockingQueue to map this function over.
     * @return A new ArrayBlockingQueue with this function applied to each element.
     */
    public ArrayBlockingQueue mapJ(final ArrayBlockingQueue as) {
        return F1Functions.mapJ(this, as);
    }


    /**
     * Maps this function over a TreeSet.
     *
     * @param as A TreeSet to map this function over.
     * @return A new TreeSet with this function applied to each element.
     */
    public TreeSet mapJ(final TreeSet as) {
        return F1Functions.mapJ(this, as);
    }

    /**
     * Maps this function over a PriorityQueue.
     *
     * @param as A PriorityQueue to map this function over.
     * @return A new PriorityQueue with this function applied to each element.
     */
    public PriorityQueue mapJ(final PriorityQueue as) {
        return F1Functions.mapJ(this, as);
    }

    /**
     * Maps this function over a LinkedList.
     *
     * @param as A LinkedList to map this function over.
     * @return A new LinkedList with this function applied to each element.
     */
    public LinkedList mapJ(final LinkedList as) {
        return F1Functions.mapJ(this, as);
    }

    /**
     * Maps this function over an ArrayList.
     *
     * @param as An ArrayList to map this function over.
     * @return A new ArrayList with this function applied to each element.
     */
    public ArrayList mapJ(final ArrayList as) {
        return F1Functions.mapJ(this, as);
    }

    public  F1W map(F f) {
        return lift(F1Functions.map(this, f));
    }

    public  F1W contramap(F f) {
        return lift(F1Functions.contramap(this, f));
    }

    public static class F1WFunc extends F1W {
        final F func;
        public F1WFunc(F f) {
            func = f;
        }

        @Override
        public B f(A a) {
            return func.f(a);
        }
    }

    /**
     * Lifts the function into the fully featured function wrapper
     */
    public static  F1W lift(final F f) {
        return new F1WFunc(f);
    }
}