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

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

import static fj.Function.curry;

import fj.Monoid;
import fj.data.List;
import fj.data.Option;
import static fj.data.Option.some;
import static fj.data.Option.none;
import static fj.Semigroup.intAdditionSemigroup;
import static fj.Semigroup.intMultiplicationSemigroup;

import static java.lang.Math.abs;

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

  /**
   * Curried Integer addition.
   */
  public static final F> add = intAdditionSemigroup.sum();

  /**
   * Curried Integer multiplication.
   */
  public static final F> multiply = intMultiplicationSemigroup.sum();

  /**
   * Curried Integer subtraction.
   */
  public static final F> subtract = curry((x, y) -> x - y);

  /**
   * Negation.
   */
  public static final F negate = x -> x * -1;

  /**
   * Absolute value.
   */
  public static final F abs = Math::abs;

  /**
   * Remainder.
   */
  public static final F> remainder = curry((a, b) -> a % b);

  /**
   * Power.
   */
  public static final F> power = curry((a, b) -> (int) StrictMath.pow(a, b));

  /**
   * Evenness.
   */
  public static final F even = i -> i % 2 == 0;

  /**
   * Sums a list of integers.
   *
   * @param ints A list of integers to sum.
   * @return The sum of the integers in the list.
   */
  public static int sum(final List ints) {
    return Monoid.intAdditionMonoid.sumLeft(ints);
  }

  /**
   * Returns the product of a list of integers.
   *
   * @param ints A list of integers to multiply together.
   * @return The product of the integers in the list.
   */
  public static int product(final List ints) {
    return Monoid.intMultiplicationMonoid.sumLeft(ints);
  }

  /**
   * A function that converts strings to integers.
   *
   * @return A function that converts strings to integers.
   */
  public static F> fromString() {
    return s -> {
        try { return some(Integer.valueOf(s)); }
        catch (final NumberFormatException ignored) {
          return none();
        }
    };
  }

  /**
   * A function that returns true if the given integer is greater than zero.
   */
  public static final F gtZero = i -> i > 0;

  /**
   * A function that returns true if the given integer is greater than or equal to zero.
   */
  public static final F gteZero = i -> i >= 0;

  /**
   * A function that returns true if the given integer is less than zero.
   */
  public static final F ltZero = i -> i < 0;

  /**
   * A function that returns true if the given integer is less than or equal to zero. 
   */
  public static final F lteZero = i -> i <= 0;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy