開発環境
- macOS Mojave - Apple (OS)
- Windows 10 Pro (OS)
- IntelliJ IDEA CE(Community Edition) (IDE(統合開発環境))
- Kotlin (プログラミング言語)
Head First Kotlin: A Brain-Friendly Guide (Dawn Griffiths(著)、David Griffiths(著)、O'Reilly Media)のChapter 11(lambdas and higher-order functions - Treating Code Like Data)、Mixed Messages(335)の解答を求めてみる。
コード
fun main() {
val x = 20
val y = 2.3
val lam1 = { x: Int -> x }
println(lam1(x + 6) == 26)
val lam2: (Double) -> Double
lam2 = { (it * 2) + 5 }
println(lam2(y) == 9.6)
val lam3: (Double, Double) -> Unit
lam3 = { x, y -> println(x + y) }
// 4.6
lam3.invoke(y, y)
var lam4 = { y: Int -> (y / 2).toDouble() }
println(lam4(x) == 10.0)
lam4 = { it + 6.3 }
println(lam4(7) == 13.3)
}入出力結果
true true 4.6 true true Process finished with exit code 0
0 コメント:
コメントを投稿