Despite the fact that Kotlin is recognized implicitly as an object-oriented language, it is still open to other programming styles and paradigms. Thanks to Kotlin's built-in features, we are able to apply functional programming patterns to our code with ease. Having the possibility to return functions from other functions or to pass a function as a parameter allows us to benefit from a deferred computation. In addition, we are able to return functions, instead of already-computed values, on different layers in the code. This results in the lazy-evaluation feature.
Compared to Scala or other functional programming languages, Kotlin doesn't require us to use dedicated, functional style design patterns. It also lacks some of their out-of-the-box implementations. However, in return, it brings more flexibility to developers as far as software architecture and implementation details are concerned. The Kotlin language and standard library components provide full built-in support for basic functional programming concepts. And more sophisticated ones can always be implemented from scratch or reused from some of the available external libraries. The ones worth giving a try are the Kotlin Arrow (http://arrow-kt.io) and funKTionale (https://github.com/MarioAriasC/funKTionale) projects. However, keep in mind the words of Robert C. Martin—It is perfectly possible to write a program that is both object-oriented and functional. Not only is it possible, it is desirable. There is no "OO vs FP," the two are orthogonal and coexist nicely.It should be understood that functional programming is only one available tool. It should be used wisely and only where it is applicable.
This chapter focuses on explaining functional programming features supported by Kotlin internally. It gives you hands-on experience in solving real-life problems by using state-of-the-art functional programming concepts. By the end of the chapter, you should be familiar with the Kotlin language support for the functional programming approach and standard library components that can help implement it.