Record Class LambdaNode
- Record Components:
parameter- the lambda parameter name | Lambda 参数名body- the lambda body expression | Lambda 函数体表达式
- All Implemented Interfaces:
Node
Represents a lambda/arrow function: parameter -> body.
Lambda nodes are first-class values that can be passed as arguments to
higher-order functions like filter, map, and reduce.
表示 lambda/箭头函数:parameter -> body。
Lambda 节点是一等值,可以作为参数传递给高阶函数,如 filter、map 和 reduce。
Features | 主要功能:
- First-class function values in expressions - 表达式中的一等函数值
- Single parameter binding with scoped evaluation - 单参数绑定与作用域求值
- Child context creation for variable isolation - 子上下文创建用于变量隔离
- Multi-argument application support for future extensibility - 多参数应用支持,便于未来扩展
Usage Examples | 使用示例:
// Lambda as filter predicate
Node lambda = LambdaNode.of("x", BinaryOpNode.of(
IdentifierNode.of("x"), ">", LiteralNode.ofInt(10)
));
// evaluate() returns the LambdaNode itself (first-class value)
Object value = lambda.evaluate(ctx); // returns LambdaNode
// Apply lambda to an argument
Object result = lambda.apply(42, ctx); // true (42 > 10)
Security | 安全性:
- Thread-safe: Yes, immutable record - 线程安全: 是,不可变记录
- Null-safe: No, parameter and body must be non-null - 空值安全: 否,参数名和函数体不能为空
- Since:
- JDK 25, opencode-base-expression V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionLambdaNode(String parameter, Node body) Creates an instance of aLambdaNoderecord class. -
Method Summary
Modifier and TypeMethodDescriptionapply(Object argument, EvaluationContext context) Apply this lambda to a single argument 将此 Lambda 应用于单个参数applyMulti(Object[] args, EvaluationContext context) Apply this lambda to multiple arguments (future extensibility) 将此 Lambda 应用于多个参数(未来扩展)body()Returns the value of thebodyrecord component.final booleanIndicates whether some other object is "equal to" this one.evaluate(EvaluationContext context) Evaluate this lambda node 求值此 Lambda 节点final inthashCode()Returns a hash code value for this object.static LambdaNodeCreate lambda expression node 创建 Lambda 表达式节点Returns the value of theparameterrecord component.Get string representation for debugging 获取用于调试的字符串表示final StringtoString()Returns a string representation of this record class.Methods inherited from interface Node
getTypeName
-
Constructor Details
-
LambdaNode
-
-
Method Details
-
of
Create lambda expression node 创建 Lambda 表达式节点- Parameters:
parameter- the parameter name | 参数名body- the body expression | 函数体表达式- Returns:
- the lambda node | Lambda 节点
-
evaluate
Evaluate this lambda node 求值此 Lambda 节点Returns the LambdaNode itself as a first-class value. The lambda is applied later by functions like filter/map via
apply(Object, EvaluationContext).返回 LambdaNode 本身作为一等值。 Lambda 稍后通过
apply(Object, EvaluationContext)由 filter/map 等函数调用。 -
apply
Apply this lambda to a single argument 将此 Lambda 应用于单个参数Creates a child context, binds the parameter to the argument, and evaluates the body in the child context.
创建子上下文,将参数绑定到实参,并在子上下文中求值函数体。
- Parameters:
argument- the argument value | 实参值context- the parent evaluation context | 父求值上下文- Returns:
- the evaluation result | 求值结果
-
applyMulti
Apply this lambda to multiple arguments (future extensibility) 将此 Lambda 应用于多个参数(未来扩展)Binds the first argument to the parameter name. Additional arguments are bound as
_arg1,_arg2, etc. for future multi-parameter lambda support.将第一个参数绑定到参数名。额外的参数绑定为
_arg1、_arg2等, 用于未来的多参数 Lambda 支持。- Parameters:
args- the argument values | 实参值数组context- the parent evaluation context | 父求值上下文- Returns:
- the evaluation result | 求值结果
- Throws:
IllegalArgumentException- if args is empty | 如果参数为空则抛出异常
-
toExpressionString
Description copied from interface:NodeGet string representation for debugging 获取用于调试的字符串表示- Specified by:
toExpressionStringin interfaceNode- Returns:
- the string representation | 字符串表示
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
parameter
-
body
-