- Kotlin for Enterprise Applications using Java EE
- Raghavendra Rao K
- 173字
- 2021-06-10 18:49:24
Functional paradigms
Kotlin supports a functional style of programming that allows you to write more elegant, concise, and expressive code.
In Kotlin, we don't have to write lengthy code to provide simple functionality. Pure functions and higher-order functions avoid mutating the states, thereby reducing the complexity of the code and improving its readability.
Lambda expressions are anonymous functions that represent the implementation of a Single Abstract Method(SAM) interface. We can pass lambda expressions to functions. In doing this, we are not passing objects, but instead we are passing behaviors to the functions that are evaluated without mutating the state of an object. This turns a function into a higher-order function.
Consider the following lambda expression:
val greetingLambda = { println("Greet from inline lambda") }
This can be invoked using the following:
greetingLambda() or
greetingLambda.invoke()
The output of the preceding lambda expression is as follows:
Let's write an inline lambda expression to print even numbers:
listOf(0,1,2,3,4,5,6,7,8,9)
.filter{ e -> e % 2 == 0}
.forEach{ e -> println(e)}
The output will be as follows:
- Learning Neo4j
- 大學計算機基礎(第二版)
- Puppet 4 Essentials(Second Edition)
- Oracle Exadata性能優(yōu)化
- R語言數(shù)據(jù)可視化之美:專業(yè)圖表繪制指南
- C語言程序設計
- Learn WebAssembly
- jQuery開發(fā)基礎教程
- C和C++游戲趣味編程
- AIRIOT物聯(lián)網(wǎng)平臺開發(fā)框架應用與實戰(zhàn)
- Java圖像處理:基于OpenCV與JVM
- 零代碼實戰(zhàn):企業(yè)級應用搭建與案例詳解
- ASP.NET Web API Security Essentials
- 玩轉(zhuǎn).NET Micro Framework移植:基于STM32F10x處理器
- Learning D