Interface Op<T>

Type Parameters:
T - the argument type of the operation
All Superinterfaces:
Function<T[],T>, Supplier<Op<T>>
All Known Implementing Classes:
BoolOp, Const, EphemeralConst, MathOp, Program, Val, Var

public interface Op<T> extends Function<T[],T>, Supplier<Op<T>>
Operation interface. An operation is a function that maps some argument type with a given arity to a result object of the same type: T[] -> T.
final Op<Double> add = Op.of("add", 2, v -> v[0] + v[1]);
final Op<Double> add3 = Op.of("add3", 3, v -> v[0] + v[1] + v[2]);
final Op<Double> sub = Op.of("sub", 2, v -> v[0] - v[1]);
final Op<Double> sin = Op.of("sin", 1, v -> Math.sin(v[0]));
Implementations of the Op interface are usually immutable and doesn't maintain internal state. But some instances are ephemeral with changing state. This classes must override the get() method inherited from the Supplier interface and return a new instance.
Since:
3.9
Version:
3.9
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Return the arity of the operation function.
    default Op<T>
    get()
    Return this operation, or a new instance from the same type, if the operation needs to maintain internal state.
    default boolean
    Determines if the operation is a terminal operation.
    Return the name of the operation.
    static <T> Op<T>
    of(String name, int arity, Function<T[],T> function)
    Create a new operation from the given parameter.
    static <T> Op<T>
    of(String name, BinaryOperator<T> function)
    Create a new operation with the given name and binary operation.
    static <T> Op<T>
    of(String name, UnaryOperator<T> function)
    Create a new operation with the given name and unary operation.

    Methods inherited from interface java.util.function.Function

    andThen, apply, compose
  • Method Details

    • name

      Return the name of the operation.
      Returns:
      the name of the operation
    • arity

      int arity()
      Return the arity of the operation function. If the arity is zero, the operation is terminal operation.
      Returns:
      the arity of the operation
    • isTerminal

      default boolean isTerminal()
      Determines if the operation is a terminal operation.
      Returns:
      true if the operation is a terminal operation, false otherwise
    • get

      default Op<T> get()
      Return this operation, or a new instance from the same type, if the operation needs to maintain internal state. This is essentially the case for ephemeral constants.
      Specified by:
      get in interface Supplier<T>
      Returns:
      this operation, or a new instance
      See Also:
    • of

      static <T> Op<T> of(String name, int arity, Function<T[],T> function)
      Create a new operation from the given parameter.
      Type Parameters:
      T - the operation type
      Parameters:
      name - the operation name
      arity - the arity of the operation
      function - the function executed by the operation. In order to work properly, the given function should be stateless and must not have side effects.
      Returns:
      a new operation from the given parameter
      Throws:
      NullPointerException - if the given name or function is null
      IllegalArgumentException - if the given arity is smaller than zero
    • of

      static <T> Op<T> of(String name, UnaryOperator<T> function)
      Create a new operation with the given name and unary operation. The returned Op will have arity one.
      Type Parameters:
      T - the operation type
      Parameters:
      name - the name of the returned operation
      function - the used function of the operation
      Returns:
      a new operation with the given name and unary operation
      Throws:
      NullPointerException - if the given name or function is null
      Since:
      4.0
    • of

      static <T> Op<T> of(String name, BinaryOperator<T> function)
      Create a new operation with the given name and binary operation. The returned Op will have arity two.
      Type Parameters:
      T - the operation type
      Parameters:
      name - the name of the returned operation
      function - the used function of the operation
      Returns:
      a new operation with the given name and unary operation
      Throws:
      NullPointerException - if the given name or function is null
      Since:
      4.0