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

fj.function.Booleans 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.function;


import static fj.Function.*;

import fj.F;
import fj.Monoid;
import fj.Semigroup;
import fj.data.List;
import fj.data.Stream;

import static fj.Semigroup.disjunctionSemigroup;
import static fj.Semigroup.conjunctionSemigroup;
import static fj.Semigroup.exclusiveDisjunctionSemiGroup;

/**
 * Curried logical functions.
 *
 * @version %build.number%
 */
public final class Booleans {
  private Booleans() {
    throw new UnsupportedOperationException();
  }

  /**
   * Curried form of logical "inclusive or" (disjunction).
   */
  public static final F> or = disjunctionSemigroup.sum();

  /**
   * Curried form of logical "and" (conjunction).
   */
  public static final F> and = conjunctionSemigroup.sum();


  /**
   * Curried form of logical xor (nonequivalence).
   */
  public static final F> xor = exclusiveDisjunctionSemiGroup.sum();

  /**
   * Logical negation.
   */
  public static final F not = p -> !p;

  /**
   * Curried form of logical "only if" (material implication).
   */
  public static final F> implies = curry((p, q) -> !p || q);

  /**
   * Curried form of logical "if" (reverse material implication).
   */
  public static final F> if_ = flip(implies);

  /**
   * Curried form of logical "if and only if" (biconditional, equivalence).
   */
  public static final F> iff = compose2(not, xor);

  /**
   * Curried form of logical "not implies" (nonimplication).
   */
  public static final F> nimp = compose2(not, implies);

  /**
   * Curried form of logical "not if" (reverse nonimplication).
   */
  public static final F> nif = compose2(not, if_);

  /**
   * Curried form of logical "not or".
   */
  public static final F> nor = compose2(not, or);

  /**
   * Returns true if all the elements of the given list are true.
   *
   * @param l A list to check for all the elements being true.
   * @return true if all the elements of the given list are true. False otherwise.
   */
  public static boolean and(final List l) {
    return Monoid.conjunctionMonoid.sumLeft(l);
  }

    /**
     * maps given function to the predicate function
     * @param p predicate to be mapped over
     * @param f function
     * @return predicate function
     */
    public static   F contramap(F f, F p){
        return compose(p, f);
    }

    /**
     * alias for contramap
     * @param p predicate to be mapped over
     * @param f function
     * @return predicate function
     */
    public static   F is(F f, F p){
        return contramap(f, p);
    }

    /**
     * returns inverse of contramap
     * @param p predicate to be mapped over
     * @param f function
     * @return predicate function
     */
    public static   F isnot(F f, F p){
        return compose(not, contramap(f, p));
    }

    /**
     * composes the given predicate using conjunction
     * @param p1 first predicate
     * @param p2 second predicate
     * @return composed predicate function
     */
    public static   F and(F p1, F p2){
        return Semigroup.functionSemigroup(conjunctionSemigroup).sum(p1, p2);
    }

    /**
     * composes the given predicate using exclusive disjunction
     * @param p1 first predicate
     * @param p2 second predicate
     * @return composed predicate function
     */
    public static   F xor(F p1, F p2){
        return Semigroup.functionSemigroup(exclusiveDisjunctionSemiGroup).sum(p1, p2);
    }

    /**
     * returns composed predicate using disjunction
     * @param p1 first predicate
     * @param p2 second predicate
     * @return composed predicate
     */
    public static   F or(F p1, F p2){
        return Semigroup.functionSemigroup(disjunctionSemigroup).sum(p1, p2);
    }

  /**
   * Returns true if all the elements of the given stream are true.
   *
   * @param l A stream to check for all the elements being true.
   * @return true if all the elements of the given stream are true. False otherwise.
   */
  public static boolean and(final Stream l) {
    return Monoid.conjunctionMonoid.sumLeft(l);
  }

  /**
   * Returns composed predicate
   *
   * @param l A stream of predicates
   * @return composed predicate
   */
  public static  F andAll(final Stream> l) {
    return Monoid.functionMonoid(Monoid.conjunctionMonoid).sumLeft(l);
  }

  /**
   * Returns a composed predicate of given List of predicates
   *
   * @param l A list of predicate functions
   * @return composed predicate function
   */
  public static  F andAll(final List> l) {
    return Monoid.functionMonoid(Monoid.conjunctionMonoid).sumLeft(l);
  }

  /**
   * Returns a composed predicate of given List of predicates
   *
   * @param l A list of predicate functions
   * @return composed predicate function
   */
  public static  F orAll(final List> l) {
    return Monoid.functionMonoid(Monoid.disjunctionMonoid).sumLeft(l);
  }

  /**
   * Returns a composed predicate of given Stream of predicates
   *
   * @param l A stream of predicate functions
   * @return composed predicate function
   */
  public static  F orAll(final Stream> l) {
    return Monoid.functionMonoid(Monoid.disjunctionMonoid).sumLeft(l);
  }

  /**
   * Returns true if any element of the given list is true.
   *
   * @param l A list to check for any element being true.
   * @return true if any element of the given list is true. False otherwise.
   */
  public static boolean or(final List l) {
    return Monoid.disjunctionMonoid.sumLeft(l);
  }

  /**
   * Returns true if any element of the given stream is true.
   *
   * @param l A stream to check for any element being true.
   * @return true if any element of the given stream is true. False otherwise.
   */
  public static boolean or(final Stream l) {
    return Monoid.disjunctionMonoid.sumLeft(l);
  }

  /**
   * Negates the given predicate.
   *
   * @param p A predicate to negate.
   * @return The negation of the given predicate.
   */
  public static  F not(final F p) {
    return compose(not, p);
  }

  /**
   * Curried form of conditional. If the first argument is true, returns the second argument,
   * otherwise the third argument.
   *
   * @return A function that returns its second argument if the first argument is true, otherwise the third argument.
   */
  public static  F>> cond() {
    return curry((p, a1, a2) -> p ? a1 : a2);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy