Package com.singularsys.jep.functions

# Class NaryBinaryFunction

java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
com.singularsys.jep.functions.NaryBinaryFunction

All Implemented Interfaces:
  `PostfixMathCommandI`, `Serializable`

Direct Known Subclasses:
  `Add`, `Concat`, `JepTest.MyNaryBinary`, `Multiply`, `NullWrapTest.MyNaryBinary`

public abstract class NaryBinaryFunction extends PostfixMathCommand
Convenient base class for n-ary functions backed by a binary operation. Defines an `Object eval(Object l,Object r)` method for calculating the result. The operation must be associative.

Author:
  Richard Morris
See Also:
  - Serialized Form

- ## Field Summary

  ### Fields inherited from class com.singularsys.jep.functions.PostfixMathCommand

  `curNumberOfParameters, description, name, NaN, numberOfParameters`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `NaryBinaryFunction()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `checkNumberOfParameters(int n)`
  Checks the number of parameters of the function.
  `Object`
  `eval(Object[] args)`
  Evaluate given an array of arguments.
  `abstract Object`
  `eval(Object l, Object r)`
  Evaluate given a pair of arguments.
  `static NaryBinaryFunction`
  `instanceOf(BinaryFunction.BiFunctionWithException<Object> fun)`
  Create a NaryBinaryFunction from a lambda expression where arguments can be Objects.
  `static NaryBinaryFunction`
  `instanceOf(BinaryFunction.BiFunctionWithException<Object> fun, String name)`
  Create a named NaryBinaryFunction from a lambda expression where arguments can be Objects.
  `static <T> NaryBinaryFunction`
  `instanceOf(Class<T> type, BinaryFunction.BiFunctionWithException<T> fun)`
  Create a NaryBinaryFunction from a lambda expression where both arguments are of a specified type.
  `static <T> NaryBinaryFunction`
  `instanceOf(Class<T> type, BinaryFunction.BiFunctionWithException<T> fun, String name)`
  Create a NaryBinaryFunction from a lambda expression where both arguments are of a specified type.
  `void`
  `run(Stack<Object> stack)`
  Calls `eval(Object, Object)`

  ### Methods inherited from class com.singularsys.jep.functions.PostfixMathCommand

  `asArray, asBool, asDouble, asInt, asLong, asStrictInt, asString, asType, getDescription, getDescription, getDescriptionWithType, getName, getNumberOfParameters, setCurNumberOfParameters, setDescription, setName, toString, toString`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### NaryBinaryFunction

    public NaryBinaryFunction()

- ## Method Details

  - ### checkNumberOfParameters

    public boolean checkNumberOfParameters(int n)
    Description copied from class: `PostfixMathCommand`
    Checks the number of parameters of the function. Functions which set numberOfParameter=-1 should overload this method
    Specified by:
      `checkNumberOfParameters` in interface `PostfixMathCommandI`
    Overrides:
      `checkNumberOfParameters` in class `PostfixMathCommand`
    Parameters:
      `n` - number of parameters function will be called with.
    Returns:
      false if an illegal number of parameters is supplied, true otherwise.

  - ### run

    public void run(Stack<Object> stack) throws EvaluationException
    Calls `eval(Object, Object)`
    Specified by:
      `run` in interface `PostfixMathCommandI`
    Overrides:
      `run` in class `PostfixMathCommand`
    Parameters:
      `stack` - arguments for function
    Throws:
      `EvaluationException` - if function cannot be evaluated

  - ### eval

    public Object eval(Object[] args) throws EvaluationException
    Evaluate given an array of arguments. Calls the `eval(Object, Object)` method for the first pair of arguments, then repeatedly with the current result and the next argument.
    Parameters:
      `args` - array of arguments to function, must have at least one element
    Returns:
      result of function
    Throws:
      `EvaluationException` - if empty array

  - ### eval

    public abstract Object eval(Object l, Object r) throws EvaluationException
    Evaluate given a pair of arguments. Must be specified for all implementing classes.
    Parameters:
      `l` - lhs argument
      `r` - rhs argument
    Returns:
      result of applying the function to the arguments
    Throws:
      `EvaluationException` - on error.

  - ### instanceOf

    public static NaryBinaryFunction instanceOf(BinaryFunction.BiFunctionWithException<Object> fun)
    Create a NaryBinaryFunction from a lambda expression where arguments can be Objects. For example `NaryBinaryFunction.instanceOf((x,y) -> ((Number) x).doubleValue() + ((Number) y).doubleValue())`
    Parameters:
      `fun` - the lambda function
    Returns:
      a new NaryBinaryFunction instance
    Since:
      Jep 4.0

  - ### instanceOf

    public static NaryBinaryFunction instanceOf(BinaryFunction.BiFunctionWithException<Object> fun, String name)
    Create a named NaryBinaryFunction from a lambda expression where arguments can be Objects. For example `NaryBinaryFunction.instanceOf((x,y) -> ((Number) x).doubleValue() + ((Number) y).doubleValue())`
    Parameters:
      `fun` - the lambda function
    Returns:
      a new NaryBinaryFunction instance
    Since:
      Jep 4.0

  - ### instanceOf

    public static <T> NaryBinaryFunction instanceOf(Class<T> type, BinaryFunction.BiFunctionWithException<T> fun)
    Create a NaryBinaryFunction from a lambda expression where both arguments are of a specified type. For example `NaryBinaryFunction.instanceOf(Integer.class, (x,y) -> x * y)` The `eval(Object[])` and `eval(Object, Object)` will throw an `IllegalParameterException` if any argument is not of the correct type.
    Type Parameters:
      `T` - type of arguments and return value
    Parameters:
      `type` - type of the arguments
      `fun` - the lambda function
    Returns:
      a new NaryBinaryFunction instance
    Since:
      Jep 4.0

  - ### instanceOf

    public static <T> NaryBinaryFunction instanceOf(Class<T> type, BinaryFunction.BiFunctionWithException<T> fun, String name)
    Create a NaryBinaryFunction from a lambda expression where both arguments are of a specified type. For example `NaryBinaryFunction.instanceOf(Integer.class, (x,y) -> x * y)` The `eval(Object[])` and `eval(Object, Object)` will throw an `IllegalParameterException` if any argument is not of the correct type.
    Type Parameters:
      `T` - type of arguments and return value
    Parameters:
      `type` - type of the arguments
      `fun` - the lambda function
    Returns:
      a new NaryBinaryFunction instance
    Since:
      Jep 4.0