Accumulating filtered values
Samir Talwar
samir at noodlesandwich.com
Tue Dec 3 04:43:14 PST 2013
// Sending again. It appears I have to add "lambda-dev" manually to the
reply list, even if I hit Reply All.
It looks like you want to map over the matches, then filter out empty ones.
This becomes clear when you realise you throw away the list you generate in
the predicate. I'm not *sure* you want to throw away the empty ones, so if
that's not true, just delete the appropriate line below.
You also want to avoid printing inside the flatMap. You're already doing
that outside.
Finally, you can move more operations outside the flatMap. Anything that
doesn't rely on the pattern itself is a candidate.
Here's some untested code which should hopefully solve your problems:
Function<Matcher, List<String>> matches = m -> {
List<String> list = new ArrayList<String>();
while(m.find()) {
list.add(m.group());
}
return list;
};
patterns.stream()
.flatMap(p -> source.stream().map(p::matcher))
.map(matches)
.filter(Predicates.not(List::isEmpty)))
.forEach(System.out::println);
— Samir.
On Tue, Dec 3, 2013 at 12:41 PM, Samir Talwar <samir at noodlesandwich.com>wrote:
> It looks like you want to map over the matches, then filter out empty
> ones. This becomes clear when you realise you throw away the list you
> generate in the predicate. I'm not *sure* you want to throw away the empty
> ones, so if that's not true, just delete the appropriate line below.
>
> You also want to avoid printing inside the flatMap. You're already doing
> that outside.
>
> Finally, you can move more operations outside the flatMap. Anything that
> doesn't rely on the pattern itself is a candidate.
>
> Here's some untested code which should hopefully solve your problems:
>
> Function<Matcher, List<String>> matches = m -> {
> List<String> list = new ArrayList<String>();
> while(m.find()) {
> list.add(m.group());
> }
> return list;
> };
>
> patterns.stream()
> .flatMap(p -> source.stream().map(p::matcher))
> .map(matches)
> .filter(Predicates.not(List::isEmpty)))
> .forEach(System.out::println);
>
> — Samir.
>
>
>
> On Tue, Dec 3, 2013 at 11:37 AM, <mohan.radhakrishnan at polarisft.com>wrote:
>
>> Hi,
>> I have two streams. One is the log line and another stream is
>> the pattern list. I was trying to accumulate the result of 'm.find()' in a
>> List because this function returns multiple values in a while loop. I
>> think here instead of a Predicate I need some type of accumulator ? I
>> searched but didn't find an example.
>>
>> This code compiles and runs but I couldn't access the list of accumulated
>> matches.
>>
>> The result now is a set of Matchers.But not the matches. Each Matcher is
>> printed two times.
>>
>> [java.util.regex.Matcher[pattern=Current.*?[/|] region=0,447 lastmatch=]]
>> java.util.regex.Matcher[pattern=Current.*?[/|] region=0,447 lastmatch=]
>> [java.util.regex.Matcher[pattern=Current.*?[/|] region=0,447 lastmatch=]]
>> java.util.regex.Matcher[pattern=Current.*?[/|] region=0,447 lastmatch=]
>> [java.util.regex.Matcher[pattern=[0-9]+(/,|/|) region=0,447 lastmatch=]]
>> java.util.regex.Matcher[pattern=[0-9]+(/,|/|) region=0,447 lastmatch=]
>> [java.util.regex.Matcher[pattern=[0-9]+(/,|/|) region=0,447 lastmatch=]]
>> java.util.regex.Matcher[pattern=[0-9]+(/,|/|) region=0,447 lastmatch=]
>>
>> Thanks.
>>
>> Predicate<Matcher> matches = m -> {
>> List<String> list = new ArrayList();
>> while(m.find()){
>> String match = m.group();
>> list.add(match);
>> }
>> return true;
>> };
>>
>> //Style 1
>> patterns.stream()
>> .flatMap(new Function<Pattern, Stream<Matcher>>() {
>> @Override
>> public Stream<Matcher> apply(Pattern p) {
>> return source.stream()
>> .map(p::matcher)
>> .filter(matches).map(l -> {
>> System.out.println( "[" + l + "]" );
>> return l;
>> });
>> }
>> })
>> .forEach(System.out::println);
>>
>>
>> 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