In this chapter, you've seen already how map can be implemented, but this time it will be your task to implement it yourself. So make your own implementation, without looking at what was presented in this chapter. As a help, you can take a look at implementations of filter and flatMap. Here you have a couple of examples how map can be used:
val list = listOf(1, 2, 3)
println(list.map { it * 2 }) // [2, 4, 6]
println(list.map { "$it!" }) // [1!, 2!, 3!]
println(list.map { it % 2 == 0 }) // [false, true, false]
This function should be an extension function on Iterable, that returns List. Consider what is the size of the result collection. If the actual receiver type is Collection, you can take its size, otherwise you can use 10 as the initial size for the result list.
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in functional/collections/Map.kt. You can find there example usage and unit tests.
You can also find there simplified implementations of onEach, filter and flatMap. Use them as inspiration. You can also notice, that to prevent you from using map from the Kotlin standard library, I used a feature called import alias, so I imported this function under a different name.
import kotlin.collections.map as `implement it yourself`
Once you are done with the exercise, you can check your solution here.
Playground
import org.junit.Test
import kotlin.collections.onEach
import kotlin.test.assertEquals
// To prevent unintentional stdlib functions usage
import kotlin.collections.map as `implement it yourself`
import kotlin.collections.onEach as `implement it yourself`
import kotlin.collections.flatMap as `implement it yourself`
import kotlin.collections.filter as `implement it yourself`
inline fun <T, C: Iterable<T>> C.onEach(operation: (T) -> Unit): C {
for (elem in this) {
operation(elem)
}
return this
}
inline fun <T, R> Iterable<T>.flatMap(transformation: (T) -> Iterable<R>): List<R> {
val list = ArrayList<R>()
for (elem in this) {
list.addAll(transformation(elem))
}
return list
}
inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {
val list = ArrayList<T>()
for (elem in this) {
if (predicate(elem)) {
list.add(elem)
}
}
return list
}
// TODO
fun main() {
val numbers = 1..10
val names = listOf("Mike", "Jane", "Marcin", "John", "James")
numbers.onEach { print(it) } // 12345678910
println()
names.onEach { print(it) } // MikeJaneMarcinJohnJames
println()
println(names.filter { it.startsWith("J") }) // [Jane, John, James]
println(names.filter { it.startsWith("M") }) // [Mike, Marcin]
println(names.flatMap { it.toList() }) // [M, i, k, e, J, a, n, e, M, a, r, c, i, n, J, o, h, n, J, a, m, e, s]
println(numbers.flatMap { listOf(it, it + 10) }) // [1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9, 19, 10, 20]
// println(names.map { it.uppercase() }) // [MIKE, JANE, MARCIN, JOHN, JAMES]
// println(numbers.map { it * 10 }) // [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
val list = listOf(1, 2, 3)
println(list.map { it * 2 }) // [2, 4, 6]
println(list.map { "$it!" }) // [1!, 2!, 3!]
println(list.map { it % 2 == 0 }) // [false, true, false]
}
class MapTest {
@Test
fun mapTests() {
val numbers = 1..5
val names = listOf("Mike", "Jane", "Marcin", "John", "James")
val upper = names.map { it.uppercase() }
val doubled = numbers.map { it * 2 }
assertEquals(listOf("MIKE", "JANE", "MARCIN", "JOHN", "JAMES"), upper)
assertEquals(listOf(2, 4, 6, 8, 10), doubled)
val list = listOf(1, 2, 3)
assertEquals(list.map { it * 2 }, listOf(2, 4, 6))
assertEquals(list.map { "$it!" }, listOf("1!", "2!", "3!"))
assertEquals(list.map { it % 2 == 0 }, listOf(false, true, false))
}
}
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.