
 
   
  
   HigherOrderFunctions&Lambda.gif
   
  
  
   
    
   
    
 
   
   
  
  
   
    
   
    
 
   
   
  
 
........
摘自:
《Kotlin 极简教程》
源代码:
package com.light.sword.coursera
val lengthFun = fun(s: String): Int = s.length //lengthFun is a fun variable
val isOddFun = fun(x: Int): Boolean = x % 2 != 0
fun compose(length: (String) -> Int, isOdd: (Int) -> Boolean): (String) -> Boolean {
    return { x -> isOdd(length(x)) }
}
fun main(args: Array<String>) {
    val words = listOf("Hello", "U", "Kotlin", "Java")
    val result = words.filter(compose(lengthFun, isOddFun)) // Use lengthFun directly
    println(result) // [Hello, U]
} 
  
   
    
   
    
 
   
   
  
 
Kotlin 开发者社区
国内第一Kotlin 开发者社区公众号,主要分享、交流 Kotlin 编程语言、Spring Boot、Android、React.js/Node.js、函数式编程、编程思想等相关主题。
 
  
   
    
   
    
 
   
  
   开发者社区 QRCode.jpg
   
  
                










