I am writing a library call that will allow you to filter and map in the same iteration of the loop, this is async/callback style in Java. The problem of course is that for filtering we usually return true/false, but for mapping we need to return the value we want.
So I am thinking of something like this:
(val, v) -> {
v.include(); // we want to keep this value
return val * 2; // the value to map to
}
so the include() method call is like returning true in a typical filter operation. We could add an exclude() method which is returning false.
My question is - is this a good methodology? Are there libraries that do something like this that have a well-understood way of filtering and mapping in the same iteration?
if it's not clear, this would discard the element in the array.
(val, v) -> {
v.exclude();
return null;
}
another solution would be for them to return a unique key if they want to filter the value out:
(val, v) -> {
return DISCARD_KEY; // return a unique key representing discard, like returning false in a typical filter
}