@Override protected String visitLambdaExpression(LambdaExpression node, StackableAstVisitorContext<Integer> indent) { StringBuilder builder = new StringBuilder(); builder.append('('); Joiner.on(", ").appendTo(builder, node.getArguments()); builder.append(") -> "); builder.append(process(node.getBody(), indent)); return builder.toString(); }
@Override public Node visitLambda(SqlBaseParser.LambdaContext context) { List<String> arguments = context.identifier().stream() .map(SqlBaseParser.IdentifierContext::getText) .collect(toList()); Expression body = (Expression) visit(context.expression()); return new LambdaExpression(arguments, body); }
@Override protected String visitLambdaExpression(LambdaExpression node, Boolean unmangleNames) { StringBuilder builder = new StringBuilder(); builder.append('('); Joiner.on(", ").appendTo(builder, node.getArguments()); builder.append(") -> "); builder.append(process(node.getBody(), unmangleNames)); return builder.toString(); }
@Test public void testLambda() throws Exception { assertExpression("x -> sin(x)", new LambdaExpression( ImmutableList.of("x"), new FunctionCall(QualifiedName.of("sin"), ImmutableList.of(new QualifiedNameReference(QualifiedName.of("x")))))); assertExpression("(x, y) -> mod(x, y)", new LambdaExpression( ImmutableList.of("x", "y"), new FunctionCall( QualifiedName.of("mod"), ImmutableList.of(new QualifiedNameReference(QualifiedName.of("x")), new QualifiedNameReference(QualifiedName.of("y")))))); }