Lambda-fied pattern matching
Remi Forax
forax at univ-mlv.fr
Wed Nov 19 23:10:07 UTC 2014
On 11/19/2014 10:54 PM, Dan Smith wrote:
> Working with the pattern matching API, I noticed that it could be made a lot less clumsy with some lambdafication.
>
> Here's the status quo:
>
> Pattern p = Pattern.compile("(\w)*, (\d)*, (\w)*");
> for (String s : lines) {
> Matcher m = p.matcher(str);
> if (m.match(s)) {
> System.out.println(m.group(1));
> }
> }
>
> With a lambda-friendly API:
>
> Pattern p = Pattern.compile("\d*, \d*, \d*");
> for (String s : lines) {
> p.match(str, r -> System.out.println(r.group(1)));
> }
>
> The 'match' is declared as 'match(String, Consumer<MatchResult>)'. You could argue that the functional interface should be a Function rather than a Consumer; whatever.
>
> Could also do 'matchFirst', 'matchAll' -- the latter eliminates even more boilerplate.
>
> If considered useful, this could be added to String too:
>
> str.match("\d*, \d*, \d*", r -> System.out.println(r.group(1)));
>
> Is this something that has been considered? Should I file an RFE?
>
> —Dan
While I agree that we could have a more lambda-ish API,
I prefer having a method Pattern.matchAsStream that returns a Stream of
MatchResult
because its more flexible that the API you propose.
It's also more coherent with the fact that there is already a method
splitAsStream().
Pattern p = Pattern.compile("(\d)*, (\d)*, (\d)*");
for (String s : lines) {
p.matchAsStream(line).forEach(r -> System.out.println(r.group(1)));
}
or written using flatMap()
Pattern p = Pattern.compile("(\d)*, (\d)*, (\d)*");
lines.stream().flatMap(line -> p.matchAsStream(line)).map(r -> r.group(1)).forEach(System.out::println);
and yes, please log a RFE.
Rémi
More information about the core-libs-dev
mailing list