T
- the argument type of the operationpublic interface Op<T> extends Function<T[],T>, Supplier<Op<T>>
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]));
Op
interface are usually immutable and doesn't
maintain internal state. But some instance are ephemeral with changing state.
This classes must override the get()
method inherited from the
Supplier
interface and return a new instance.Var
,
Const
,
EphemeralConst
Modifier and Type | Method and Description |
---|---|
int |
arity()
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 |
isTerminal()
Determines if the operation is a terminal operation.
|
String |
name()
Return the name of the operation.
|
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,
int arity,
Function<T[],T> function)
Create a new operation from the given parameter.
|
static <T> Op<T> |
of(String name,
UnaryOperator<T> function)
Create a new operation with the given name and unary operation.
|
int arity()
default boolean isTerminal()
true
if the operation is a terminal operation,
false
otherwisedefault Op<T> get()
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.get
in interface Supplier<Op<T>>
this
operation, or a new instanceEphemeralConst
static <T> Op<T> of(String name, int arity, Function<T[],T> function)
T
- the operation typename
- the operation namearity
- the arity of the operationfunction
- the function executed by the operation. In order to work
properly, the given function should be stateless and must not have
side effects.NullPointerException
- if the given name
or function
is null
IllegalArgumentException
- if the given arity
is smaller
than zerostatic <T> Op<T> of(String name, UnaryOperator<T> function)
Op
will have arity one.T
- the operation typename
- the name of the returned operationfunction
- the used function of the operationNullPointerException
- if the given name
or function
is null
static <T> Op<T> of(String name, BinaryOperator<T> function)
Op
will have arity two.T
- the operation typename
- the name of the returned operationfunction
- the used function of the operationNullPointerException
- if the given name
or function
is null
© 2007-2018 Franz Wilhelmstötter (2018-10-28 17:23)