Functional Programming
Object-oriented (OO) programming has been used in “serious work” for decades. Many of the most popular languages — Java, C++, C#, Objective-C, Swift, Ruby — are based on object-oriented programming. Other languages, such as JavaScript, also leverage objects, but using somewhat different approaches (“prototype-based” vs. “class-based” object-oriented programming).
It is easy to forget that there are other ways to write a programming language.
One alternative is functional programming. There are plenty of languages, such as Clojure, Lisp, and Scheme, that have their foundations in functional programming. These languages are not nearly as popular as their OO counterparts. However, elements of functional programming are being introduced into other languages, blending functional programming with OO. Kotlin is one such language.
In this chapter, we will explore what functional programming means and how you work with it in Kotlin.
Your App Might Not Be Functional
For ordinary people who speak English, “functional” means “it works as expected”. As such, somebody telling you that your app is not functional might be a bit of an insult.
However, computer programmers might well mean “functional” in the context of functional programming. In that case, most apps are not functional, as functional programming has been a relatively niche technique, though one that is gaining mainstream momentum.
As the name suggests, functional programming’s primary unit of code is a function, just as object-oriented programming’s primary unit of code is an object. As with other forms of programming, a function in functional programming takes some input, performs some work, and returns some output.
The ideal type of function in functional programming is a “pure” function. A pure function has no side effects, such as changing the contents of a database. If you call a pure function one million times with the same input, you should get the same output each of those million times.
Functional programming also emphasizes function composition, using “higher-order” functions. For example, you might have a sort()
function that can sort some collection of data. However, sort()
itself needs be supplied not only with the data but with some other function that knows how to compare two data elements to determine their order. sort()
, therefore, might take two parameters:
- The data to be sorted
- Some identifier or reference to the function to use for order comparison
In this case, sort()
is considered to be a “higher-order” function, as it takes another function as a parameter to help define the overall work to be performed.
When non-functional programming languages start to introduce functional techniques, it is often with an eye towards stream processing. Here, a “stream” could be:
- A collection of objects
- A series of events, such as user input events or database change events
- A literal “stream”, usually referring to some source of bytes of data, such as a file or socket
That is how Kotlin approaches functional programming: making it easy to use functional techniques in certain situations, without necessarily making functional programming the one-and-only approach offered by the language.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.