Package fj.data

Class Natural

All Implemented Interfaces:
Serializable

public final class Natural extends Number
Represents a natural number (zero, one, two, etc.)
See Also:
  • Field Details

    • value

      private final BigInteger value
    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • fromBigInt

      public static final F<BigInteger,Option<Natural>> fromBigInt
      A function that returns the natural number equal to a given BigInteger
    • ZERO

      public static final Natural ZERO
      The natural number zero
    • ONE

      public static final Natural ONE
      The natural number one
    • add

      public static final F<Natural,F<Natural,Natural>> add
      A function that adds two natural numbers.
    • subtract

      public static final F<Natural,F<Natural,Option<Natural>>> subtract
      A function that subtracts its first argument from its second.
    • multiply

      public static final F<Natural,F<Natural,Natural>> multiply
      A function that multiplies a natural number by another.
    • divide

      public static final F<Natural,F<Natural,Natural>> divide
      A function that divides its second argument by its first.
    • mod

      public static final F<Natural,F<Natural,Natural>> mod
      A function that yields the remainder of division of its second argument by its first.
    • divmod

      public static final F<Natural,F<Natural,V2<Natural>>> divmod
      A function that divides its second argument by its first, yielding both the quotient and the remainder.
    • bigIntegerValue

      public static final F<Natural,BigInteger> bigIntegerValue
      A function that returns the BigInteger value of a given Natural.
  • Constructor Details

  • Method Details

    • natural

      public static Option<Natural> natural(BigInteger i)
      Returns the natural number equal to the given BigInteger
      Parameters:
      i - A given BigInteger
      Returns:
      An optional natural number, or none if the given BigInteger is less than zero.
    • natural

      public static Option<Natural> natural(long i)
      Returns the natural number equal to the given long
      Parameters:
      i - A given long
      Returns:
      An optional natural number, or none if the given long is less than zero.
    • succ

      public Natural succ()
      Return the successor of this natural number
      Returns:
      the successor of this natural number
    • succ_

      public static F<Natural,Natural> succ_()
      First-class successor function.
      Returns:
      A function that returns the successor of a given natural number.
    • pred

      public Option<Natural> pred()
      Return the predecessor of this natural number
      Returns:
      the predecessor of this natural number
    • pred_

      public static F<Natural,Option<Natural>> pred_()
      First-class predecessor function.
      Returns:
      A function that returns the predecessor of a given natural number, or None if it's zero.
    • add

      public Natural add(Natural n)
      Add two natural numbers together.
      Parameters:
      n - A natural number to add to this one.
      Returns:
      the sum of the two natural numbers.
    • subtract

      public Option<Natural> subtract(Natural n)
      Subtract a natural number from another.
      Parameters:
      n - A natural number to subtract from this one.
      Returns:
      The difference between the two numbers, if this number is larger than the given one. Otherwise none.
    • multiply

      public Natural multiply(Natural n)
      Multiply a natural number by another.
      Parameters:
      n - A natural number to multiply by this one.
      Returns:
      The product of the two numbers.
    • divide

      public Natural divide(Natural n)
      Divide a natural number by another.
      Parameters:
      n - A natural number to divide this one by.
      Returns:
      The quotient of this number and the highest number, less than or equal to the given number, that divides this number.
    • mod

      public Natural mod(Natural n)
      Take the remainder of a natural number division.
      Parameters:
      n - A natural number to divide this one by.
      Returns:
      The remainder of division of this number by the given number.
    • divmod

      public V2<Natural> divmod(Natural n)
      Divide a natural number by another yielding both the quotient and the remainder.
      Parameters:
      n - A natural number to divide this one by.
      Returns:
      The quotient and the remainder, in that order.
    • bigIntegerValue

      public BigInteger bigIntegerValue()
      Return the BigInteger value of this natural number.
      Returns:
      the BigInteger value of this natural number.
    • longValue

      public long longValue()
      Return the long value of this natural number.
      Specified by:
      longValue in class Number
      Returns:
      the long value of this natural number.
    • floatValue

      public float floatValue()
      Return the float value of this natural number.
      Specified by:
      floatValue in class Number
      Returns:
      the float value of this natural number.
    • doubleValue

      public double doubleValue()
      Return the double value of this natural number.
      Specified by:
      doubleValue in class Number
      Returns:
      the double value of this natural number.
    • intValue

      public int intValue()
      Return the int value of this natural number.
      Specified by:
      intValue in class Number
      Returns:
      the int value of this natural number.
    • sum

      public static Natural sum(Stream<Natural> ns)
      Sums a stream of natural numbers.
      Parameters:
      ns - A stream of natural numbers.
      Returns:
      The sum of all the natural numbers in the stream.
    • product

      public static Natural product(Stream<Natural> ns)
      Takes the product of a stream of natural numbers.
      Parameters:
      ns - A stream of natural numbers.
      Returns:
      The product of all the natural numbers in the stream.
    • sum

      public static Natural sum(List<Natural> ns)
      Sums a list of natural numbers.
      Parameters:
      ns - A list of natural numbers.
      Returns:
      The sum of all the natural numbers in the list.
    • product

      public static Natural product(List<Natural> ns)
      Takes the product of a list of natural numbers.
      Parameters:
      ns - A list of natural numbers.
      Returns:
      The product of all the natural numbers in the list.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object that)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object