Functional Kotlin

Book Description

Learn how to apply Functional Programming with Kotlin to real-life projects with popular libraries like Arrow.

About This Book

  • Focus on the functional aspects of Kotlin and identify the advantages that functional programming brings to the table and the associated coding benefits,
  • Implement common functional programming design patterns and techniques.
  • Learn to combine OOP and Reactive Programming with Functional Programming and how RxKotlin and funkTionale can help you implementing Functional Programming in Kotlin

Who This Book Is For

Kotlin developers who have no functional programming experience, will benefit from this book.

What You Will Learn

  • Learn the Concepts of Functional Programming with Kotlin
  • Discover the Coroutines in Kotlin
  • Uncover Using funkTionale plugin
  • Learn Monads, Functiors and Applicatives
  • Combine Functional Programming with OOP and Reactive Programming
  • Uncover Using Monads with funkTionale
  • Discover Stream Processing

In Detail

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.

Style and approach

Loaded with numerous code examples and real life projects, this book helps you dive deep into Functional Programming with Kotlin as well as applying it with help of Functional Programming Plugins like funkTionale and RxKotlin.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Functional Kotlin
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Kotlin – Data Types, Objects, and Classes
    1. Classes
      1. Properties
      2. Methods
    2. Inheritance
    3. Abstract classes
    4. Interfaces
    5. Objects
      1. Object declarations
      2. Companion objects
    6. Generics
    7. Type alias
    8. Nullable types
      1. Checking for null
      2. Checking for non-null types
      3. Safe calls
      4. The Elvis (?:) operator
      5. The (!!) operator
    9. Kotlin's type system
      1. The Any type
      2. Minimum common types
      3. The Unit type
      4. The Nothing type
    10. Other types
      1. Data classes
        1. Canonical methods
        2. The copy() method
        3. Destructuring methods
      2. Annotations
      3. Enum
    11. Summary
  8. Getting Started with Functional Programming
    1. What is functional programming?
    2. Basics concepts
      1. First-class and higher-order functions
      2. Pure functions
      3. Recursive functions
      4. Lazy evaluation
    3. Functional collections
    4. Implementing a functional list
    5. Summary
  9. Immutability - It's Important
    1. What is immutability?
      1. Implementing immutability in Kotlin
      2. The difference between var and val
      3. Compile time constants
      4. Types of immutability
        1. Immutable reference  (referential immutability)
        2. Immutable values
    2. Immutable collections
    3. The advantages of immutability
      1. Thread safety
      2. Low coupling
      3. Referential transparency
      4. Failure atomicity
      5. Caching
      6. Compiler optimization
      7. Pure functions
    4. The disadvantages of immutability
    5. Summary
  10. Functions, Function Types, and Side Effects
    1. Functions in Kotlin
      1. Returning two values from a function
      2. Extension functions
      3. Default arguments
      4. Nested functions
    2. Function types in functional programming
    3. Lambda
      1. Function as property
    4. High order functions
    5. Pure functions and side effects
      1. Side effects
      2. Pure functions
    6. Summary
  11. More on Functions
    1. Single-expression functions
    2. Parameters
      1. vararg
      2. Lambda
      3. Named parameters
        1. Named parameters on high-order functions
      4. Default parameters
    3. Extension functions
      1. Extension functions and inheritance
        1. Extension functions as members
      2. Extension functions with conflicting names
      3. Extension functions for objects
    4. Infix functions
    5. Operator overloading
      1. Binary operators
      2. Invoke
      3. Indexed access
      4. Unary operators
    6. Type-safe builders
      1. Creating a DSL
    7. Inline functions
      1. Inline restrictions
    8. Recursion and corecursion
    9. Summary
  12. Delegates in Kotlin
    1. Introduction to delegation
      1. Understanding delegation
    2. Delegates in Kotlin
    3. Property delegation (standard delegates)
      1. The Delegates.notNull function and lateinit
      2. The lazy function
      3. Observing property value change with Delegates.Observable
      4. The power of veto – Delegates.vetoable
    4. Delegated map
    5. Custom delegation
    6. Local delegates
    7. Class delegation
    8. Summary
  13. Asynchronous Programming with Coroutines
    1. Introduction to coroutines
      1. Understanding JVM threads
      2. Hello, coroutine world!
    2. Using coroutines in real life
      1. Synchronous implementation
      2. Callbacks
      3. Java Futures
      4. Promises with Kovenant 
      5. Coroutines
      6. Coroutine context
    3. Channels
      1. Channel pipelines
    4. Managing mutable state
      1. Switching contexts
      2. Thread safe structures
      3. Mutexes
      4. Actors
    5. Summary
  14. Collections and Data Operations in Kotlin
    1. An introduction to collections
      1. The advantages of a collections framework
    2. List and MutableList
    3. Set and MutableSet
    4. Map and MutableMap
    5. Data operations in a collection
      1. The map function
      2. The filter function
      3. The flatMap function
      4. The drop functions
      5. The take functions
      6. The zip function
    6. Grouping collections
    7. Summary
  15. Functional Programming and Reactive Programming
    1. Combining FP with OOP
    2. Functional reactive programming
      1. The Reactive Manifesto
      2. Functional reactive frameworks for Kotlin
    3. Getting started with RxKotlin
      1. Downloading and setting up RxKotlin
      2. Comparing the Pull mechanism with the RxJava Push mechanism
    4. Observables
      1. How Observable works
    5. The Observable.create method
    6. The Observable.from methods
    7. Iterator<T>.toObservable
    8. Subscriber – the Observer interface
    9. Subscribing and disposing
    10. Summary
  16. Functors, Applicatives, and Monads
    1. Functors   
    2. Monads
    3. Applicatives
    4. Summary
  17. Working with Streams in Kotlin
    1. Introduction to Streams
    2. Collections versus Streams
    3. Working with Streams
    4. Primitive streams
    5. Stream factory methods
      1. Stream Builder
      2. Creating empty Streams – Stream.empty()
      3. Creating a Stream by passing elements – Stream.of()
      4. Generating Streams – Stream.generate()
    6. Collector and Stream.collect – collecting Streams
      1. The Collectors.toList(), Collectors.toSet(), and Collectors.toCollection() methods
      2. Collecting into Map – Collectors.toMap()
      3. Joining Stream of strings – Collectors.joining()
      4. Grouping elements of Stream – Collectors.groupingBy()
    7. Summary
  18. Getting Started with Arrow
    1. Function composition
    2. Partial application
      1. Binding
    3. Reverse
    4. Pipes
    5. Currying
      1. Differences between the currying and partial application
    6. Logical complement
    7. Memoization
    8. Partial functions
    9. Identity and constant
    10. Optics
      1. Configuring Arrows code generation
      2. Generating lenses
    11. Summary
  19. Arrow Types
    1. Option
    2. Arrow's type hierarchy
    3. Either
    4. Monad transformers
    5. Try
    6. State
      1. Corecursion with State
    7. Summary
  20. Kotlin's Quick Start
    1. Writing and running Kotlin
      1. Kotlin online
      2. On your console
        1. Installing SDKMAN
        2. Installing Kotlin through SDKMAN
        3. Kotlin's REPL
        4. Compiling and executing Kotlin files
      3. Using Gradle
        1. Install Gradle through SDKMAN
        2. Creating a distributable Gradle command
        3. Creating a Gradle project file
        4. Creating our Hello World code
      4. Using IntelliJ IDEA or Android Studio
        1. Importing Gradle files with IntelliJ IDEA
    2. Basic Kotlin syntax
      1. General features
        1. Packages
        2. String concatenation and interpolation
        3. Comments 
      2. Control structures
        1. if expression
        2. when expression
        3. for loop
        4. while and do loops
    3. Going further
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product Information

  • Title: Functional Kotlin
  • Author(s): Rivu Chakraborty, Mario Arias
  • Release date: February 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788476485