nextflow icon indicating copy to clipboard operation
nextflow copied to clipboard

`filterMap` operator

Open Xophmeister opened this issue 3 months ago • 3 comments

New feature: filterMap operator

Nextflow currently supports both the filter and map operators against a queue channel. It is very common to combine these, so it may be useful -- as a convenience -- to have a single filterMap operator that does both operations.

Usage scenario

Before:

myChannel
| filter { someCriteria(it) }
| map { someMap(it) }
| // etc...

After:

myChannel
| filterMap { someCriteria(it) ? someMap(it) : null }
| // etc...

The above may not look like much of an improvement, but bear in mind that it's maximally generalised. Maybe an outer join would be a more realistic example:

// Channel of the keys in myChannelA that are not in myChannelB
// NOTE For sake of the example:
// * myChannelA emits [ meta ]
// * myChannelB emits [ meta, etc ] and is *not* empty
myChannelA
| join(myChannelB, remainder: true)
| filterMap { meta, etc -> etc ? null : meta }

Suggest implementation

I've modelled this on Rust's std::iter::Iterator::filter_map, in which its closure returns an Option<T>: if it's Some<T>, then that's the matched and mapped value (of type T); if it's None, then the filter skips. Groovy/Java (AFAIK) doesn't have an equivalent of Option<T>, so I've gone with not-null and null, respectively (presuming it's unrealistic for the mapping function to return null).

Xophmeister avatar Apr 29 '24 17:04 Xophmeister