- 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:
- 黑客攻防從入門到精通(實戰秘笈版)
- 案例式C語言程序設計
- Android Studio Essentials
- Mastering macOS Programming
- TradeStation交易應用實踐:量化方法構建贏家策略(原書第2版)
- Swift細致入門與最佳實踐
- Arduino家居安全系統構建實戰
- Java系統化項目開發教程
- C語言程序設計
- Kubernetes進階實戰
- Visual Basic程序設計(第三版)
- SSH框架企業級應用實戰
- Python Penetration Testing Essentials
- Learning Alfresco Web Scripts
- Natural Language Processing with Python Cookbook