metron-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jjmeyer0 <...@git.apache.org>
Subject [GitHub] incubator-metron pull request #517: METRON-831: Add lambda expressions and r...
Date Sat, 08 Apr 2017 15:13:41 GMT
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 )`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message