fun filterMap2Long(mapFunc: (Int) -> Int, filterFunc: (Int) -> Boolean): List<Int> {
val list = mutableListOf<Int>()
for (v in listOf(0, 1, 2, 3, 4)) {
val w = mapFunc.invoke(v)
if (filterFunc(w))
list.add(w)
}
return list
}
fun filterMap2Short(mapFunc: (Int) -> Int, filterFunc: (Int) -> Boolean): List<Int> =
listOf(0, 1, 2, 3, 4)
.map(mapFunc)
.filter(filterFunc)
Function Call | Return Value | |||
---|---|---|---|---|
filterMap2Long({ it * 2 }, { it > 3 }) | → | |||
filterMap2Long({ it * 3 }, { it < 7 }) | → | |||
filterMap2Short({ (it * 10).toString().length }, { it == 1 }) | → | |||
filterMap2Short({ it }, { it >= 3 }) | → | |||
filterMap2Short({ it + 2 }, { it < 5 }) | → | |||
filterMap2Short({ it - 1 }, { it == 3 }) | → |
Experiment with this code on Gitpod.io or as a Kotlin Playground