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

 

 

Collection TransformationsFilter2

prev  |  next  |  chance

val inRange1: (String) -> Boolean = { s: String -> s.startsWith("H") }

val inRange2: (String) -> Boolean = { it.lowercase().endsWith("at") }

fun filterIt2Long(vals: List<String>, func: (String) -> Boolean): List<String> {
  val list = mutableListOf<String>()
  for (v in vals) {
    if (func.invoke(v))
      list.add(v)
  }
  return list
}

fun filterIt2Short(vals: List<String>, func: (String) -> Boolean): List<String> = vals.filter(func)

Function Call  Return Value
filterIt2Long(listOf("hi", "HA"), inRange1)
filterIt2Long(listOf("PAT", "at"), inRange2)
filterIt2Short(listOf("hi", "HA"), inRange1)
filterIt2Short(listOf("BAT", "HA"), inRange2)
filterIt2Short(listOf("BAT", "HA")) { s: String -> s.uppercase().endsWith("AT") }
filterIt2Short(listOf("BAT", "HA")) { it.lowercase().contains("a") }
filterIt2Short(listOf("BAT", "HA")) { it.capitalize().startsWith("B") }

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

⬅ Back