Chapter 10
Functional Programming with Lambdas

Functional-style code is declarative—you focus on what to do and assign the details of how to do it to underlying libraries of functions. In contrast, in the imperative style of programming you have to deal with both the whats and hows. As applications become complicated, having to deal with the hows makes the code highly complex. The functional style is inherently less complex—you can get more accomplished with not only fewer lines of code but also code that is expressive, easier to understand, and a breeze to maintain.

The imperative style of programming has been the mainstream for a few decades now. Languages like Java have offered a combination of imperative style and object-oriented programming. Object-oriented programming is helpful to abstract and encapsulate concepts. The goal of functional programming is not to replace OO, which greatly reduces complexity; the real concern is the imperative style. That’s one of the reasons why Java has moved forward to accommodate the functional style of programming in recent years. Now we can do both imperative and functional styles of programming along with object-oriented programming in Java. While Java has evolved, Kotlin was born that way, as a mixed paradigm language that supports the imperative, functional, and object-oriented paradigms.

In this chapter you’ll learn the benefits of the functional style, how to create and use lambda expressions, or lambdas for short, and the dos and don’ts with lambdas. You’ll also learn about lexical scoping, passing methods where lambdas are expected, and when to create anonymous functions instead of lambdas.

Sometimes appearances can be deceiving, and the functional style may result in poor performance if we’re not careful. We’ll also see how to keep the fluency in code without compromising performance.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset