Package fj.data
Class Natural
- java.lang.Object
-
- java.lang.Number
-
- fj.data.Natural
-
- All Implemented Interfaces:
java.io.Serializable
public final class Natural extends java.lang.NumberRepresents a natural number (zero, one, two, etc.)- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static F<Natural,F<Natural,Natural>>addA function that adds two natural numbers.static F<Natural,java.math.BigInteger>bigIntegerValueA function that returns the BigInteger value of a given Natural.static F<Natural,F<Natural,Natural>>divideA function that divides its second argument by its first.static F<Natural,F<Natural,V2<Natural>>>divmodA function that divides its second argument by its first, yielding both the quotient and the remainder.static F<java.math.BigInteger,Option<Natural>>fromBigIntA function that returns the natural number equal to a given BigIntegerstatic F<Natural,F<Natural,Natural>>modA function that yields the remainder of division of its second argument by its first.static F<Natural,F<Natural,Natural>>multiplyA function that multiplies a natural number by another.static NaturalONEThe natural number oneprivate static longserialVersionUIDstatic F<Natural,F<Natural,Option<Natural>>>subtractA function that subtracts its first argument from its second.private java.math.BigIntegervaluestatic NaturalZEROThe natural number zero
-
Constructor Summary
Constructors Modifier Constructor Description privateNatural(java.math.BigInteger i)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Naturaladd(Natural n)Add two natural numbers together.java.math.BigIntegerbigIntegerValue()Return the BigInteger value of this natural number.Naturaldivide(Natural n)Divide a natural number by another.V2<Natural>divmod(Natural n)Divide a natural number by another yielding both the quotient and the remainder.doubledoubleValue()Return the double value of this natural number.booleanequals(java.lang.Object that)floatfloatValue()Return the float value of this natural number.inthashCode()intintValue()Return the int value of this natural number.longlongValue()Return the long value of this natural number.Naturalmod(Natural n)Take the remainder of a natural number division.Naturalmultiply(Natural n)Multiply a natural number by another.static Option<Natural>natural(long i)Returns the natural number equal to the given longstatic Option<Natural>natural(java.math.BigInteger i)Returns the natural number equal to the given BigIntegerOption<Natural>pred()Return the predecessor of this natural numberstatic F<Natural,Option<Natural>>pred_()First-class predecessor function.static Naturalproduct(List<Natural> ns)Takes the product of a list of natural numbers.static Naturalproduct(Stream<Natural> ns)Takes the product of a stream of natural numbers.Option<Natural>subtract(Natural n)Subtract a natural number from another.Naturalsucc()Return the successor of this natural numberstatic F<Natural,Natural>succ_()First-class successor function.static Naturalsum(List<Natural> ns)Sums a list of natural numbers.static Naturalsum(Stream<Natural> ns)Sums a stream of natural numbers.java.lang.StringtoString()
-
-
-
Field Detail
-
value
private final java.math.BigInteger value
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
fromBigInt
public static final F<java.math.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.
-
-
Method Detail
-
natural
public static Option<Natural> natural(java.math.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 java.math.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:
longValuein classjava.lang.Number- Returns:
- the long value of this natural number.
-
floatValue
public float floatValue()
Return the float value of this natural number.- Specified by:
floatValuein classjava.lang.Number- Returns:
- the float value of this natural number.
-
doubleValue
public double doubleValue()
Return the double value of this natural number.- Specified by:
doubleValuein classjava.lang.Number- Returns:
- the double value of this natural number.
-
intValue
public int intValue()
Return the int value of this natural number.- Specified by:
intValuein classjava.lang.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:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object that)
- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-