Book Image

Functional Kotlin

Book Image

Functional Kotlin

Overview of this book

Functional programming makes your application faster, improves performance, and increases your productivity. Kotlin supports many of the popular and advanced functional features of functional languages. This book will cover the A-Z of functional programming in Kotlin. This book bridges the language gap for Kotlin developers by showing you how to create and consume functional constructs in Kotlin. We also bridge the domain gap by showing how functional constructs can be applied in business scenarios. We’ll take you through lambdas, pattern matching, immutability, and help you develop a deep understanding of the concepts and practices of functional programming. If you want learn to address problems using Recursion, Koltin has support for it as well. You’ll also learn how to use the funKtionale library to perform currying and lazy programming and more. Finally, you’ll learn functional design patterns and techniques that will make you a better programmer.By the end of the book, you will be more confident in your functional programming skills and will be able to apply them while programming in Kotlin.
Table of Contents (22 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

List and MutableList


List is one of the most commonly used collection data types. It is an implementation of the Collection interface used to work with a group of ordered data.

Note

The data in a list may be ordered based on when it was added (like if we add 3 after 4 to an Int List, then 4 will appear in the list before 3, much like an array) or may even be ordered based on other ordering algorithms.

As we mentioned earlier, Kotlin distinguishes between mutable and read-only collection types; so, the List interface, which is immutable, contains only the read-only functions, as follows:

  • fun get(index: Int):E: This method is used to get an element from the list at the given index.
  • fun indexOf(element: @UnsafeVariance E):Int: This method is used to identify the index of an element in the list. This method will search for the specified element inside the whole list and return the position of the element if it's in the list. Otherwise, it will return -1.
  • fun listIterator(): ListIterator<E>:...