id/email
password
forgot password | create account
about | help | prefs
ReadingBatcode reading practice

 

 

Collection TransformationsMap2

prev  |  next  |  chance

val isInRange1: (Int) -> Boolean = { i: Int -> i in 3..6 }

val isInRange2: (Int) -> Boolean = { it < 2 || it > 5 }

fun mapIt2Long(i: Int, func: (Int) -> Boolean): List<Boolean> {
  val list = mutableListOf<Boolean>()
  for (v in (0..i)) {   // (0..i) is inclusive
    val w = func.invoke(v)
    list.add(w)
  }
  return list
}

fun mapIt2Short(i: Int, func: (Int) -> Boolean): List<Boolean> = (0..i).map(func)

Function Call  Return Value
mapIt2Long(4, isInRange1)
mapIt2Long(4, isInRange2)
mapIt2Short(2, isInRange1)
mapIt2Short(2, isInRange2)
mapIt2Short(4) { i: Int -> i > 4 }
mapIt2Short(4) { i: Int -> i < 5 }
mapIt2Short(4) { it == 5 }
mapIt2Short(4) { it == 4 }

Experiment with this code on Gitpod.io or as a Kotlin Playground

⬅ Back