Basic functional style question

mohan.radhakrishnan at polarisFT.com mohan.radhakrishnan at polarisFT.com
Wed Nov 27 02:47:42 PST 2013


Yes. Thanks Stuart. 

I also have these questions.

1. How do I apply a large number of regex patterns one by one in the same 
pipeline ? When is a new functional interface needed ?

2.  function(){
        return (data$V1[ grep ("(.*)Full GC(.*)", data[,1])])
     }

     Is there a way to do this ? This R code uses code to change values in 
a data structure in-place. Code and data are together here.

Mohan




From:   Stuart Marks <stuart.marks at oracle.com>
To:     mohan.radhakrishnan at polarisft.com, lambda-dev at openjdk.java.net
Date:   11/27/2013 01:46 PM
Subject:        Re: Basic functional style question



I started scratching this out and I got a sense of déjà vu, and then I 
realized 
Kirk Pepperdine recently wrote a blog post last week about using lambda to 
parse 
and pattern match GC logs. Were you at his talk at Øredev?

https://weblogs.java.net/blog/kcpeppe/archive/2013/11/10/fun-lambdas

The approach he ended up with (with help from Brian Goetz), applied to 
your 
example, would look something like this:

     Pattern pat = Pattern.compile("Current.*?[/|]");
     Files.lines(Paths.get(...))
          .map(pat::matcher)
          .filter(Matcher::find)
          .map(Matcher::group)
          .forEach(System.out::println);

Note this uses Pattern and Matcher instead of Scanner, as they're more 
flexible.

The insight is to map each input string to a Matcher so that midway 
through the 
pipeline we have Stream<Matcher>. We then filter the matchers to get only 
the 
successful ones, and then extract the matched string from them.

Does this do what you want?

s'marks

On 11/26/13 10:38 PM, mohan.radhakrishnan at polarisft.com wrote:
> Sent by mistake. The actual question is this.
>
> The code in the previous mail works but when I use 'R' language this is
> accomplished by this line.
>
> y <- apply( y, 1, function(z) str_extract(z,"Current.*?[/|]"))
>
> I can again operate on the data structure like this.
>
> z <- str_extract_all(y,"[0-9]+(/,|/|)")
>
> I think this is possible using Streams and filters.
>
> How should I use these lines without defining new Functional interfaces 
?
>
>          Scanner s = new Scanner( data );
>          s.findInLine("Current.*?[/|]");
>          System.out.println(s.match().group());
>
> Thanks.
>
>
>
> From:   Mohan Radhakrishnan/BSC31/polarisFT
> To:     lambda-dev at openjdk.java.net
> Date:   11/27/2013 11:57 AM
> Subject:        Basic functional style question
>
>
> Hi,
>
> This is a question from a user. I am trying to jdk 8 to parse a file and
> apply a regex on it.
>
> public class GCLogParser {
>
>      private void readFile() throws IOException {
>
>          Stream<String> s = Files.lines(get(("D:\\GC
> Analysis\\usage.txt")), UTF_8);
>          s.forEach( this::parse  );
>      }
>
>      public static void parseDocument(  Parseable parser,
>   String data){
>          parser.apply(data);
>      }
>      private void parse( String data ){
>
>          Scanner s = new Scanner( data );
>          s.findInLine("Current.*?[/|]");
>          System.out.println(s.match().group());
>      }
>
>      public static void main( String... argv ) throws IOException {
>          new GCLogParser().readFile();
>      }
>
>      @FunctionalInterface
>      public interface Parseable{
>           void apply( String s);
>      }
> }
>
>
>
> This e-Mail may contain proprietary and confidential information and is 
sent for the intended recipient(s) only.  If by an addressing or 
transmission error this mail has been misdirected to you, you are 
requested to delete this mail immediately. You are also hereby notified 
that any use, any form of reproduction, dissemination, copying, 
disclosure, modification, distribution and/or publication of this e-mail 
message, contents or its attachment other than by its intended recipient/s 
is strictly prohibited.
>
> Visit us at http://www.polarisFT.com
>




This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only.  If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately. You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited.

Visit us at http://www.polarisFT.com


More information about the lambda-dev mailing list