[ https://issues.apache.org/jira/browse/METRON-831?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15961864#comment-15961864 ]
ASF GitHub Bot commented on METRON-831: --------------------------------------- Github user jjmeyer0 commented on a diff in the pull request: https://github.com/apache/incubator-metron/pull/517#discussion_r110520125 --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/FunctionalFunctions.java --- @@ -0,0 +1,121 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.metron.common.dsl.functions; + +import com.google.common.collect.ImmutableList; +import org.apache.metron.common.dsl.BaseStellarFunction; +import org.apache.metron.common.dsl.Stellar; +import org.apache.metron.common.stellar.LambdaExpression; + +import java.util.ArrayList; +import java.util.List; + +public class FunctionalFunctions { + @Stellar(name="MAP" + , description="Applies lambda expression to a list of arguments. e.g. MAP( [ 'foo', 'bar' ] , ( x ) -> TO_UPPER(x) ) would yield [ 'FOO', 'BAR' ]" + , params = { + "list - List of arguments." + ,"transform_expression - The lambda expression to apply. This expression is assumed to take one argument." + } + , returns = "The input list transformed item-wise by the lambda expression." + ) + public static class Map extends BaseStellarFunction { + + @Override + public Object apply(List<Object> args) { + List<Object> input = (List<Object>) args.get(0); + LambdaExpression expression = (LambdaExpression)args.get(1); + if(input == null || expression == null) { + return input; + } + List<Object> ret = new ArrayList<>(); + for(Object o : input) { + if(o != null) { + ret.add(expression.apply(ImmutableList.of(o))); + } + } + return ret; + } + } + + @Stellar(name="FILTER" + , description="Applies a filter in the form of a lambda expression to a list. e.g. FILTER( [ 'foo', 'bar' ] , (x) -> x == 'foo') would yield [ 'foo']" + , params = { + "list - List of arguments." + ,"predicate - The lambda expression to apply. This expression is assumed to take one argument and return a boolean." + } + , returns = "The input list filtered by the predicate." + ) + public static class Filter extends BaseStellarFunction { + + @Override + public Object apply(List<Object> args) { + List<Object> input = (List<Object>) args.get(0); + LambdaExpression expression = (LambdaExpression) args.get(1); + if(input == null || expression == null) { + return input; + } + List<Object> ret = new ArrayList<>(); + for(Object o : input) { + if(o == null) { --- End diff -- This will cause the following Stellar expression to return an empty list: `FILTER([ 'foo', 'bar', null ], (item) -> item == null )` > Add lambda expressions and rudimentary functional programming primitives to > Stellar > ----------------------------------------------------------------------------------- > > Key: METRON-831 > URL: https://issues.apache.org/jira/browse/METRON-831 > Project: Metron > Issue Type: Improvement > Reporter: Casey Stella > > To enable use-cases where the user may want to do simple iteration, we can > expose simple functional primitives such as: > * MAP > * REDUCE > * FILTER > To fully support this, we will need lambda expressions as arguments to these > functions. -- This message was sent by Atlassian JIRA (v6.3.15#6346)