Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Kotlin for Enterprise Applications using Java EE

You're reading from   Kotlin for Enterprise Applications using Java EE Develop, test, and troubleshoot enterprise applications and microservices with Kotlin and Java EE

Arrow left icon
Product type Paperback
Published in Nov 2018
Publisher Packt
ISBN-13 9781788997270
Length 388 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Raghavendra Rao K Raghavendra Rao K
Author Profile Icon Raghavendra Rao K
Raghavendra Rao K
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Kotlin – A First look FREE CHAPTER 2. Kotlin – The Game Changer 3. An Overview of Java EE and Kotlin 4. Kotlin with JSF and CDI 5. Kotlin with JPA and EJB 6. Enterprise Messaging with Kotlin 7. Developing RESTful Services with JAX-RS 8. Securing JAVA EE Applications with Kotlin 9. Implementing Microservices with Kotlin 10. Performance Monitoring and Logging 11. Design Patterns with Kotlin 12. Other Books You May Enjoy

Lambda expressions in Kotlin

Functional programming can be advantageous when we are performing lots of different operations on data that has a known, fixed amount of variation. It provides a very elegant way to write code with immutability. It also allows you to write pure functions.

A lambda expression is an anonymous function that represents the implementation of single abstract method (SAM) of an interface. Lambda expressions deal with expressions and promote immutability, which turn functions into higher-order functions.

Let's write a lambda expression. Consider the following code for 12_LambdaExpressions_HelloWorld.kts:

val greetingLambda = { println("Hello from Lambda")}
greetingLambda()

The output is as follows:

We can invoke lambda functions directly, as in the example shown in the preceding code, or we can use invoke() as follows—greetingLambda.invoke():

val greetingLambda = { println("Hello from Lambda")}
greetingLambda.invoke()

In the next example, we will write a lambda expression that takes an argument. Consider the following code for 12a_LambdaExpressions.kts:

val greetingLambda = { user: String -> println("Hello ${user}")}
greetingLambda.invoke("Tom")

The output is as follows:

We will now write an inline lambda function to print only even numbers. Consider the following code for 12b_LambdaExpressions.kts:

listOf(0,1,2,3,4,5,6,7,8,9)
.filter{ e -> e % 2 == 0}
.forEach{ e -> println(e)}

Here, we created a list of numbers from 0-9, and then we used filter to retrieve only the even numbers. Finally, we used forEach to iterate over the numbers and print the values to the console.

The output of the preceding code is as follows:

You have been reading a chapter from
Kotlin for Enterprise Applications using Java EE
Published in: Nov 2018
Publisher: Packt
ISBN-13: 9781788997270
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime