Skipping an item in a map?

Sometimes, especially when refactoring code where I can’t do much about the design, I run into an issue where I want to map over a collection but skip certain items.

Normally, I could use filter for this. But say for example I want to trigger some kind of side effect (for example a logging event) when skipping an element. My way of doing this is usually to use mapNotNull and return null in the map, but obviously that won’t fly if I want the result to possible contain null.

Basically, I’m looking for an equivalent of


that can be used inside of map/forEach, is there such a thing?

submitted by /u/qhxo
[link] [comments]