Basic functional style question

Paul Sandoz paul.sandoz at oracle.com
Wed Nov 27 04:34:48 PST 2013


On Nov 27, 2013, at 1:08 PM, mohan.radhakrishnan at polarisFT.com wrote:

> Not a fork. I used to filter 'Full GC' lines and then apply a pattern to 
> filter the times and then get the sizes of memory pools. How is this done 
> if I have several regex patterns ?
> The pipeline could be long.
> 

There are a couple of options:

- abstract out the creation of the sub-stream pipeline as a bi-function of Pattern,Stream -> Stream

Stream<String> s = ...
BiFunction<Pattern, Stream<String>, Stream<String>> f = ...
for (Pattern p : ps) {
  s = s.apply(p, s);  

- use flatMap, which can be used as a combined filter+map.


> I have seen that array initializers use lambdas.

Yes, but you still have to be careful if you want to do in-place updates, as the functions passed, to say setAll, will have side-effects as they are are not "pure" functions whose result depends solely on what is input.


> So I thought that an 
> array/ArrayList of values can be filtered in-place by applying a function 
> assuming no concurrency is required. 
> 

Yes, as i said it depends on the source and you need to be careful.

Given that we don't have indexed-based streams a common pattern when operating on arrays is to create an IntStream over the range of indexes of the array

Paul.


More information about the lambda-dev mailing list