How do I use scope functions in a functional reactive context with Kotlin Flows?

In Kotlin Flow code, scope functions are useful, but they should usually play a supporting role. The main structure of your reactive pipeline should come from Flow operators such as map, filter, flatMapLatest, combine, onEach, catch, and stateIn.

A good rule of thumb:

Flow operators describe the stream.
Scope functions describe what you do with each value.

1. Use map for stream transformation, let for local value transformation

If you are transforming each emitted value, the outer operation should usually be map.

val userNames: Flow<String> =
    usersFlow.map { user ->
        user.let {
            "${it.firstName} ${it.lastName}"
        }
    }

In simple cases, let may be unnecessary:

val userNames: Flow<String> =
    usersFlow.map { user ->
        "${user.firstName} ${user.lastName}"
    }

Use let inside map when it clarifies a local transformation, especially for nullable values or multistep conversion.

val profileNames: Flow<String> =
    usersFlow.map { user ->
        user.profile?.let { profile ->
            profile.displayName
        } ?: "Anonymous"
    }

2. Use onEach for stream side effects, not also as the main Flow operator

For logging, analytics, caching, or debugging, prefer onEach.

val users: Flow<List<User>> =
    userRepository.users()
        .onEach { users ->
            logger.info("Loaded ${users.size} users")
        }

Inside a transformation, also can be fine when you want to return the same value after a local side effect:

val users: Flow<List<User>> =
    userRepository.users()
        .map { users ->
            users.filter { it.isActive }
                .also { activeUsers ->
                    logger.debug("Active users: ${activeUsers.size}")
                }
        }

But avoid using also where onEach expresses the intent better:

val users: Flow<List<User>> =
    userRepository.users()
        .onEach { logger.debug("Received users: $it") }
        .map { users -> users.filter { it.isActive } }

3. Use run when computing one result from an emitted object

run is useful when each emitted value needs a multistep computation.

val summaries: Flow<UserSummary> =
    usersFlow.map { user ->
        user.run {
            val fullName = "$firstName $lastName"
            val status = if (isActive) "active" else "inactive"

            UserSummary(
                id = id,
                name = fullName,
                status = status
            )
        }
    }

This works well when you want receiver-style access with this.

4. Use apply when constructing objects inside a Flow

apply is useful for configuring a mutable object before emitting or returning it.

val requests: Flow<Request> =
    userIds.map { userId ->
        Request().apply {
            method = "GET"
            path = "/users/$userId"
            headers["Accept"] = "application/json"
        }
    }

That said, in reactive code, immutable data classes are often clearer:

val requests: Flow<Request> =
    userIds.map { userId ->
        Request(
            method = "GET",
            path = "/users/$userId",
            headers = mapOf("Accept" to "application/json")
        )
    }

Use apply mainly when an API requires mutable configuration.

5. Use with sparingly inside Flow chains

with can be useful when working with an existing object, but nested receivers can become confusing inside Flow pipelines.

val messages: Flow<String> =
    events.map { event ->
        with(event.metadata) {
            "source=$source, timestamp=$timestamp"
        }
    }

This is fine if the receiver is obvious. But if you already have multiple nested lambdas, explicit names may be clearer:

val messages: Flow<String> =
    events.map { event ->
        val metadata = event.metadata
        "source=${metadata.source}, timestamp=${metadata.timestamp}"
    }

6. Be careful with nested it

Flow pipelines often contain nested lambdas. Scope functions can make that worse if every lambda uses implicit it.

Harder to read:

val result: Flow<List<String>> =
    usersFlow.map {
        it.filter {
            it.isActive
        }.map {
            it.name
        }
    }

Clearer:

val result: Flow<List<String>> =
    usersFlow.map { users ->
        users.filter { user ->
            user.isActive
        }.map { user ->
            user.name
        }
    }

This matters even more with scope functions:

val result: Flow<UserDto> =
    usersFlow.map { user ->
        user.profile?.let { profile ->
            UserDto(
                id = user.id,
                displayName = profile.displayName
            )
        } ?: UserDto(
            id = user.id,
            displayName = "Anonymous"
        )
    }

Prefer named lambda parameters when combining Flow operators and scope functions.

7. Use takeIf / takeUnless with care

Although not scope functions in the same group, takeIf and takeUnless often appear with let.

For simple filtering, prefer Flow’s filter:

val activeUsers: Flow<User> =
    usersFlow.filter { user ->
        user.isActive
    }

Instead of:

val activeUsers: Flow<User> =
    usersFlow.mapNotNull { user ->
        user.takeIf { it.isActive }
    }

But takeIf can be useful when a transformation may produce null:

val validEmails: Flow<String> =
    usersFlow.mapNotNull { user ->
        user.email
            ?.takeIf { email -> email.contains("@") }
            ?.lowercase()
    }

8. Use mapNotNull with let for nullable values

This is a widespread Flow pattern.

val avatars: Flow<Avatar> =
    usersFlow.mapNotNull { user ->
        user.avatarUrl?.let { url ->
            Avatar(url)
        }
    }

Or:

val displayNames: Flow<String> =
    usersFlow.mapNotNull { user ->
        user.profile?.displayName
    }

Use let when constructing a result from a nullable value is more involved.

9. Use flatMapLatest when the scope contains another Flow

If the transformation returns another Flow, do not use only let or map unless you intentionally want a nested Flow<Flow<T>>.

Usually:

val userDetails: Flow<UserDetails> =
    selectedUserId
        .filterNotNull()
        .flatMapLatest { userId ->
            userRepository.observeUserDetails(userId)
        }

If the ID is nullable, and you need fallback behavior:

val userDetails: Flow<UserDetails?> =
    selectedUserId.flatMapLatest { userId ->
        userId?.let {
            userRepository.observeUserDetails(it)
        } ?: flowOf(null)
    }

Here, let is handling the nullable value, while flatMapLatest handles the reactive flattening.

10. Prefer Flow operators for lifecycle and errors

Use catch, onStart, onCompletion, and retry rather than trying to encode those behaviors with scope functions.

val uiState: Flow<UiState> =
    userRepository.users()
        .map { users ->
            UiState.Success(users)
        }
        .onStart {
            emit(UiState.Loading)
        }
        .catch { throwable ->
            emit(UiState.Error(throwable.message ?: "Unknown error"))
        }

Scope functions can still help locally:

val uiState: Flow<UiState> =
    userRepository.users()
        .map { users ->
            users
                .filter { user -> user.isActive }
                .let { activeUsers -> UiState.Success(activeUsers) }
        }
        .onStart {
            emit(UiState.Loading)
        }
        .catch { throwable ->
            emit(UiState.Error(throwable.message ?: "Unknown error"))
        }

Practical mapping

Intent in Flow code Prefer Scope function role
Transform each emission map Use let/run inside if helpful
Remove nulls filterNotNull, mapNotNull Use let for nullable conversion
Side effect per emission onEach Use also only locally
Build/configure object map + constructor or apply apply for mutable setup
Switch to another Flow flatMapLatest, flatMapConcat, flatMapMerge Use let for nullable branch
Combine streams combine, zip Scope functions only inside result builder
Handle errors catch, retry Scope functions rarely needed
Emit loading state onStart Scope functions rarely needed

Example: realistic UI state pipeline

val uiState: StateFlow<UserUiState> =
    selectedUserId
        .filterNotNull()
        .flatMapLatest { userId ->
            userRepository.observeUser(userId)
        }
        .map { user ->
            user.run {
                UserUiState.Content(
                    id = id,
                    title = "$firstName $lastName",
                    subtitle = email ?: "No email"
                )
            }
        }
        .onEach { state ->
            analytics.logScreenState(state)
        }
        .catch { throwable ->
            emit(UserUiState.Error(throwable.message ?: "Unable to load user"))
        }
        .stateIn(
            scope = viewModelScope,
            started = SharingStarted.WhileSubscribed(5_000),
            initialValue = UserUiState.Loading
        )

Here:

  • filterNotNull handles nullable IDs.
  • flatMapLatest switches to the latest selected user stream.
  • run computes a UI model from a User.
  • onEach performs a side effect.
  • catch handles errors.
  • stateIn turns the cold flow into a StateFlow.

Main guideline

Use scope functions in Flow pipelines when they improve the readability of local value handling.

Avoid using them to replace Flow operators.

Good:
Flow operators for stream behavior.
Scope functions for per-value clarity.

Risky:
Long chains of map/let/also/run with nested it everywhere.

If the chain starts becoming hard to read, introduce named lambda parameters or local variables.

How do I combine multiple collection operations in a single Kotlin chain?

In Kotlin, you can combine multiple collection operations by chaining functions like filter, map, sortedBy, take, groupBy, and others.

Each operation returns a new collection, so you can call the next operation directly on the result.

val numbers = listOf(1, 2, 3, 4, 5, 6)

val result = numbers
    .filter { it % 2 == 0 }
    .map { it * 10 }
    .sorted()

println(result) // [20, 40, 60]

Here’s what happens:

  1. filter { it % 2 == 0 } keeps only even numbers
  2. map { it * 10 } transforms each number
  3. sorted() sorts the result

You can also chain operations on objects:

data class User(
    val name: String,
    val age: Int,
    val active: Boolean
)

val users = listOf(
    User("Alice", 30, true),
    User("Bob", 17, true),
    User("Charlie", 25, false),
    User("Diana", 22, true)
)

val activeAdultNames = users
    .filter { it.active }
    .filter { it.age >= 18 }
    .map { it.name }
    .sorted()

println(activeAdultNames) // [Alice, Diana]

You can often combine related filters into one:

val activeAdultNames = users
    .filter { it.active && it.age >= 18 }
    .map { it.name }
    .sorted()

For maps, you can chain over entries:

val scores = mapOf(
    "Alice" to 90,
    "Bob" to 75,
    "Charlie" to 85
)

val passedNames = scores
    .filter { (_, score) -> score >= 80 }
    .map { (name, _) -> name }
    .sorted()

println(passedNames) // [Alice, Charlie]

If the collection is large or the chain is expensive, use asSequence() to make intermediate operations lazy:

val result = numbers
    .asSequence()
    .filter { it % 2 == 0 }
    .map { it * 10 }
    .sorted()
    .toList()

Use regular collection chains for simple cases, and asSequence() when you want to avoid creating intermediate collections during multi-step processing.

How do I use map, filter and foreach with Kotlin collections?

In Kotlin collections:

  • map transforms each element into a new value.
  • filter keeps only elements that match a condition.
  • forEach performs an action for each element.

map: transform elements

Use map when you want to create a new collection by changing each item.

val numbers = listOf(1, 2, 3, 4)

val doubled = numbers.map { number ->
    number * 2
}

println(doubled) // [2, 4, 6, 8]

You can use it when the lambda has one parameter:

val numbers = listOf(1, 2, 3, 4)

val doubled = numbers.map { it * 2 }

println(doubled) // [2, 4, 6, 8]

filter: keep matching elements

Use filter when you want only items that satisfy a condition.

val numbers = listOf(1, 2, 3, 4, 5, 6)

val evenNumbers = numbers.filter { it % 2 == 0 }

println(evenNumbers) // [2, 4, 6]

Another example with strings:

val names = listOf("Alice", "Bob", "Charlie", "Anna")

val namesStartingWithA = names.filter { it.startsWith("A") }

println(namesStartingWithA) // [Alice, Anna]

forEach: perform an action

Use forEach when you want to do something with each element, such as printing.

val names = listOf("Alice", "Bob", "Charlie")

names.forEach { name ->
    println(name)
}

Using it:

val names = listOf("Alice", "Bob", "Charlie")

names.forEach {
    println(it)
}

Chaining them together

You can combine filter, map, and forEach.

val numbers = listOf(1, 2, 3, 4, 5, 6)

numbers
    .filter { it % 2 == 0 }
    .map { it * 10 }
    .forEach { println(it) }

Output:

20
40
60

This means:

  1. Keep only even numbers: [2, 4, 6]
  2. Multiply each by 10: [20, 40, 60]
  3. Print each result

Example with objects

data class User(
    val name: String,
    val age: Int
)

val users = listOf(
    User("Alice", 25),
    User("Bob", 17),
    User("Charlie", 30)
)

val adultNames = users
    .filter { it.age >= 18 }
    .map { it.name }

println(adultNames) // [Alice, Charlie]

Important difference

map and filter return new collections:

val numbers = listOf(1, 2, 3)

val doubled = numbers.map { it * 2 }

println(numbers) // [1, 2, 3]
println(doubled) // [2, 4, 6]

forEach is usually used for side effects and does not create a transformed list:

val numbers = listOf(1, 2, 3)

numbers.forEach { println(it) }

Quick summary

val numbers = listOf(1, 2, 3, 4, 5)

val squared = numbers.map { it * it }
// [1, 4, 9, 16, 25]

val greaterThanTwo = numbers.filter { it > 2 }
// [3, 4, 5]

numbers.forEach { println(it) }
// Prints each number

Use:

  • map when you want to transform values
  • filter when you want to select values
  • forEach when you want to perform an action for each value

How do I create and use lists, sets, and maps in Kotlin?

In Kotlin, the main collection types are List, Set, and Map.

Kotlin provides both read-only and mutable versions:

Collection Read-only Mutable
List List<T> MutableList<T>
Set Set<T> MutableSet<T>
Map Map<K, V> MutableMap<K, V>

Lists

A list is an ordered collection. It can contain duplicate elements.

Read-only list

val numbers = listOf(1, 2, 3, 3)

println(numbers[0])        // 1
println(numbers.size)      // 4
println(numbers.contains(2)) // true

You cannot add or remove items from a read-only List.

val names = listOf("Alice", "Bob", "Charlie")

for (name in names) {
    println(name)
}

Mutable list

val names = mutableListOf("Alice", "Bob")

names.add("Charlie")
names.remove("Alice")
names[0] = "Bobby"

println(names) // [Bobby, Charlie]

You can also create an empty mutable list:

val items = mutableListOf<String>()

items.add("Book")
items.add("Pen")

println(items) // [Book, Pen]

Sets

A set is a collection of unique elements. It does not allow duplicates.

Read-only set

val numbers = setOf(1, 2, 3, 3)

println(numbers) // [1, 2, 3]
println(2 in numbers) // true

Mutable set

val fruits = mutableSetOf("Apple", "Banana")

fruits.add("Orange")
fruits.add("Apple") // Duplicate, ignored
fruits.remove("Banana")

println(fruits) // [Apple, Orange]

Empty mutable set:

val ids = mutableSetOf<Int>()

ids.add(101)
ids.add(102)

println(ids) // [101, 102]

Maps

A map stores key-value pairs. Each key is unique.

Read-only map

val ages = mapOf(
    "Alice" to 25,
    "Bob" to 30,
    "Charlie" to 35
)

println(ages["Alice"]) // 25
println(ages["Unknown"]) // null
println(ages.containsKey("Bob")) // true
println(ages.containsValue(30)) // true

Mutable map

val scores = mutableMapOf(
    "Alice" to 90,
    "Bob" to 85
)

scores["Charlie"] = 95
scores["Alice"] = 100
scores.remove("Bob")

println(scores) // {Alice=100, Charlie=95}

Empty mutable map:

val phoneBook = mutableMapOf<String, String>()

phoneBook["Alice"] = "123-456"
phoneBook["Bob"] = "987-654"

println(phoneBook["Alice"]) // 123-456

Common operations

Iterating over a list or set

val colors = listOf("Red", "Green", "Blue")

for (color in colors) {
    println(color)
}

Iterating over a map

val ages = mapOf(
    "Alice" to 25,
    "Bob" to 30
)

for ((name, age) in ages) {
    println("$name is $age years old")
}

Filtering

val numbers = listOf(1, 2, 3, 4, 5, 6)

val evenNumbers = numbers.filter { it % 2 == 0 }

println(evenNumbers) // [2, 4, 6]

Mapping values

val names = listOf("alice", "bob", "charlie")

val uppercaseNames = names.map { it.uppercase() }

println(uppercaseNames) // [ALICE, BOB, CHARLIE]

Sorting

val numbers = listOf(5, 2, 8, 1)

val sorted = numbers.sorted()

println(sorted) // [1, 2, 5, 8]

Checking contents

val names = listOf("Alice", "Bob")

println("Alice" in names) // true
println("Charlie" !in names) // true

Choosing between them

Use a List when:

  • Order matters
  • Duplicates are allowed
  • You access elements by index
val tasks = listOf("Write", "Test", "Deploy")

Use a Set when:

  • Values must be unique
  • You mainly check whether something exists
val uniqueTags = setOf("kotlin", "backend", "api")

Use a Map when:

  • You need key-value lookup
  • Each key maps to one value
val userRoles = mapOf(
    1 to "Admin",
    2 to "Editor",
    3 to "Viewer"
)

Quick summary

val readOnlyList = listOf("A", "B", "C")
val mutableList = mutableListOf("A", "B")
mutableList.add("C")

val readOnlySet = setOf("A", "B", "A") // [A, B]
val mutableSet = mutableSetOf("A", "B")
mutableSet.add("C")

val readOnlyMap = mapOf("Alice" to 25, "Bob" to 30)
val mutableMap = mutableMapOf("Alice" to 25)
mutableMap["Bob"] = 30

In short:

  • listOf() creates a read-only list
  • mutableListOf() creates a mutable list
  • setOf() creates a read-only set
  • mutableSetOf() creates a mutable set
  • mapOf() creates a read-only map
  • mutableMapOf() creates a mutable map

How do I use Map.forEach() for concise iteration?

The Map.forEach method in Java provides a concise and elegant way to iterate over all key-value pairs in a Map. This method accepts a lambda function (or method reference), which processes each entry in the map.

Here’s how you can use Map.forEach for concise iteration:

Syntax:

map.forEach((key, value) -> {
    // Your logic here
});

Example:

Suppose you have a map, and you want to print each key-value pair:

Map<String, Integer> map = new HashMap<>();
map.put("Apple", 10);
map.put("Orange", 20);
map.put("Banana", 30);

// Use forEach for iteration
map.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));

Explanation:

  1. Lambda Expression:
    • (key, value) are the parameters representing the key and the value of each entry in the map.
    • The code block after -> defines what happens for each entry in the map.
  2. Conciseness:
    • No need to use nested loops or explicitly retrieve entries from the map using entrySet or keySet.

Use Cases:

  • Logging or printing map entries.
  • Applying transformations (e.g., modifying values).
  • Collecting or filtering certain entries based on some condition.

Method Reference:

If your logic can be represented as a method, you can use a method reference:

map.forEach(System.out::println); // Prints entries like "Apple=10"

This keeps the code concise, readable, and functional.