transform

inline fun <T, R> Flow<T>.transform(crossinline transform: suspend FlowCollector<R>.(value: T) -> Unit): Flow<R>(source)

Applies transform function to each value of the given flow.

The receiver of the transform is FlowCollector and thus transform is a flexible function that may transform emitted element, skip it or emit it multiple times.

This operator generalizes filter and map operators and can be used as a building block for other operators, for example:

fun Flow<Int>.skipOddAndDuplicateEven(): Flow<Int> = transform { value ->
if (value % 2 == 0) { // Emit only even values, but twice
emit(value)
emit(value)
} // Do nothing if odd
}