Record Class LambdaNode

java.lang.Object
java.lang.Record
cloud.opencode.base.expression.ast.LambdaNode
Record Components:
parameter - the lambda parameter name | Lambda 参数名
body - the lambda body expression | Lambda 函数体表达式
All Implemented Interfaces:
Node

public record LambdaNode(String parameter, Node body) extends Record implements Node
Lambda Expression Node Lambda 表达式节点

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 Details

    • LambdaNode

      public LambdaNode(String parameter, Node body)
      Creates an instance of a LambdaNode record class.
      Parameters:
      parameter - the value for the parameter record component
      body - the value for the body record component
  • Method Details

    • of

      public static LambdaNode of(String parameter, Node body)
      Create lambda expression node 创建 Lambda 表达式节点
      Parameters:
      parameter - the parameter name | 参数名
      body - the body expression | 函数体表达式
      Returns:
      the lambda node | Lambda 节点
    • evaluate

      public Object evaluate(EvaluationContext context)
      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 等函数调用。

      Specified by:
      evaluate in interface Node
      Parameters:
      context - the evaluation context | 求值上下文
      Returns:
      this LambdaNode | 此 LambdaNode
    • apply

      public Object apply(Object argument, EvaluationContext context)
      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

      public Object applyMulti(Object[] args, EvaluationContext context)
      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

      public String toExpressionString()
      Description copied from interface: Node
      Get string representation for debugging 获取用于调试的字符串表示
      Specified by:
      toExpressionString in interface Node
      Returns:
      the string representation | 字符串表示
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • parameter

      public String parameter()
      Returns the value of the parameter record component.
      Returns:
      the value of the parameter record component
    • body

      public Node body()
      Returns the value of the body record component.
      Returns:
      the value of the body record component