Book Image

Introduction to JVM Languages

Book Image

Introduction to JVM Languages

Overview of this book

Anyone who knows software development knows about the Java Virtual Machine. The Java Virtual Machine is responsible for interpreting Java byte code and translating it into actions. In the beginning, Java was the only programming language used for the JVM. But increasing complexity of the language and the remarkable performance of the JVM created an opening for a new generation of programming languages. If you want to build a strong foundation with the Java Virtual Machine and get started with popular modern programming languages, then this book is for you. The book will begin with a general introduction of the JVM and its features, which are common to the JVM languages, helping you get abreast with its concepts. It will then dive into explaining languages such as Java, Scala, Clojure, Kotlin, and Groovy and will show how to work with each language, their features, use cases, and pros and cons. By writing example projects in those languages and focusing on each language’s strong points, it will help you find the programming language that is most appropriate for your particular needs. By the end of the book, you will have written multiple programs that run on the Java Virtual Machine and know about the differences between the various languages.
Table of Contents (21 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Functional programming in Scala


As said earlier, functional programming requires a different mindset to imperative programming. We will look at several functional-programming-related topics here:

  • Iterating through collections using functions
  • The map, filter, and reduce design pattern
  • Currying

Iterating through collections using functions

In functional programming, it is unusual to use both for or while loops to iterate through arrays and collections and process each item inside the loop's body. Instead, a method is called on the array or collection instance, which internally iterates through the array or collection. The method takes a lambda function as a parameter and ensures that the function is called for each item:

    var a = List[Int](5, 10, 15, 20, 25)
    a.foreach((x: Int) => println("%03d".format(x)))

This will print 005, 010, 015, 020, and 025. It uses the format method of Java's java.lang.String class to ensure that the printed integer has up to three leading zeroes.

The map, filter...