Package io.jenetics.prog.op
Interface Op<T>
-
- Type Parameters:
T
- the argument type of the operation
public interface Op<T> extends Function<T[],T>, Supplier<Op<T>>
Operation interface. An operation is a function which maps some argument type with a given arity to a result object of the same type:T[] -> T
.Implementations of thefinal 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 theget()
method inherited from theSupplier
interface and return a new instance.- Since:
- 3.9
- Version:
- 3.9
- See Also:
Var
,Const
,EphemeralConst
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description int
arity()
Return the arity of the operation function.default Op<T>
get()
Returnthis
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, 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.
-
-
-
Method Detail
-
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()
Returnthis
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 interfaceSupplier<T>
- Returns:
this
operation, or a new instance- See Also:
EphemeralConst
-
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 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.- Returns:
- a new operation from the given parameter
- Throws:
NullPointerException
- if the givenname
orfunction
isnull
IllegalArgumentException
- if the givenarity
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 returnedOp
will have arity one.- Type Parameters:
T
- the operation type- Parameters:
name
- the name of the returned operationfunction
- the used function of the operation- Returns:
- a new operation with the given name and unary operation
- Throws:
NullPointerException
- if the givenname
orfunction
isnull
- 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 returnedOp
will have arity two.- Type Parameters:
T
- the operation type- Parameters:
name
- the name of the returned operationfunction
- the used function of the operation- Returns:
- a new operation with the given name and unary operation
- Throws:
NullPointerException
- if the givenname
orfunction
isnull
- Since:
- 4.0
-
-