[2023] Kotlin Tutorial
Article directory
Part II Object Oriented and Functional Programming
Chapter 13 The Cornerstone of Functional Programming—Higher-order Functions and Lambda Expressions
Although the idea of functional programming is as old as object-oriented, the computer language that supports functional programming is only a matter of recent years. These languages include Swift, Python, Java 8, and C++ 11. As a new language, Kotlin also supports functional programming.
13.1 Introduction to Functional Programming
Functional programming (functional programming) is a programming paradigm like object-oriented programming, functional programming, also known as function-oriented programming. Everything in functional programming is a function .
The core concept of functional programming is as follows:
- Functions are " first-class citizens ": It means that functions are the same as other data types and are on an equal footing . Functions can be passed in as arguments to other functions and returned as return values from other functions.
- Use expressions, not statements: Functional programming is concerned with input and output, namely: parameters and return values. Expressions used in programs can have return values, while statements do not. For example: both the if and when structures in the control structure are expressions.
- Higher-order functions: Functional programming supports higher-order functions, which means that a function can be used as a parameter or return value of another function .
- No side effects: It means that the function execution process will return a result and will not modify external variables. This is a "pure function", and the same input parameters will definitely have the same output result.
The Kotlin language supports functional programming, providing function types, higher-order functions, and Lambda expressions.