Basic functional style question
Stuart Marks
stuart.marks at oracle.com
Wed Nov 27 00:17:38 PST 2013
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
>
More information about the lambda-dev
mailing list