- 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:
- 趣學Python算法100例
- oreilly精品圖書:軟件開發者路線圖叢書(共8冊)
- Learn Programming in Python with Cody Jackson
- Apache Karaf Cookbook
- Reactive Android Programming
- 數據結構案例教程(C/C++版)
- Nginx Lua開發實戰
- 西門子S7-200 SMART PLC編程從入門到實踐
- 零基礎學C語言第2版
- Mastering Python Design Patterns
- C語言程序設計與應用(第2版)
- 玩轉.NET Micro Framework移植:基于STM32F10x處理器
- Instant Apache Camel Messaging System
- Web開發的平民英雄:PHP+MySQL
- C語言程序設計實驗指導教程