transform

@ExperimentalCoroutinesApi 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 generic function that may transform emitted element, skip it or emit it multiple times.

This operator 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
}