Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Functional Kotlin
Functional Kotlin

Functional Kotlin: Extend your OOP skills and implement Functional techniques in Kotlin and Arrow

eBook
$29.99 $43.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Functional Kotlin

Getting Started with Functional Programming

Functional programming has been making big waves in the software industry for the last five years, and everyone wants to jump on the bandwagon. Functional programming is a lot older, starting in the 1950s with Lisp being considered the first programming language (or at least, the first one to introduce functional features) which still exists as Common Lisp, and other dialects such as Scheme and Clojure.

In this chapter, we'll cover the following topics:

  • What is functional programming?
  • Basic concepts
  • Functional collections
  • Implementing a functional list

What is functional programming?

Functional programming is a paradigm (a style of structuring your programs). In essence, the focus is on transforming data with expressions (ideally such expressions should not have side effects). Its name, functional, is based on the concept of a mathematical function (not in sub-routines, methods, or procedures). A mathematical function defines a relation between a set of inputs and outputs. Each input has just one output. For example, given a function, f(x) = X2; f(5) is always 25.

The way to guarantee, in a programming language, that calling a function with a parameter always returns the same value, is to avoid accessing to mutable state:

fun f(x: Long) : Long { 
return x * x // no access to external state
}

The f function doesn't access any external state; therefore, calling f(5) will always return 25:

fun main(args: Array<String...

Basics concepts

Functional programming is composed of a few well-defined concepts. A short introduction of these concepts will follow and, later on, each concept will be covered in depth, in the next chapters.

First-class and higher-order functions

The most foundational concept of functional programming is first-class functions. A programming language with support for first-class functions will treat functions as any other type; such languages will allow you to use functions as variables, parameters, returns, generalization types, and so on. Speaking of parameters and returns, a function that uses or returns other functions is a higher-order function.

Kotlin has support for both concepts.

Let's try a simple function ...

Functional collections

Functional collections are collections that offer a way to interact with its elements through high-order functions. Functional collections have common operations with names such as filter, map, and fold; these names are defined by convention (similar to a design pattern) and are being implemented in several libraries and languages.

Don't get confused with purely functional data structures—a data structure implemented in a pure functional language. A purely functional data structure is immutable and uses the lazy evaluation and other functional techniques.

Functional collections can but needn't necessarily be purely functional data structures. We have already covered how imperative implementations of algorithms can be faster than functional ones. 

Kotlin comes with an excellent functional collection library. Let's have a look at...

Implementing a functional list

With everything that we've learned in the first two chapters, we can implement a pure functional list:

sealed class FunList<out T> {
object Nil : FunList<Nothing>()

data class Cons<out T>(val head: T, val tail: FunList<T>) : FunList<T>()
}

The FunList class is a sealed class; just two possible subclasses exist—Nil, an empty list (in other books you can see this defined as Null or Empty) and Cons (a construct, name inherited from Lisp, that holds two values).

The T type is marked out; this is for variance, which we'll cover variance in future chapters.

Nil is an object (we don't need different instances of Nil) extending FunList<Nothing> (remember that Nothing is the bottom of Kotlin's type hierarchy).

The Cons value contains two values—head, a single T, and...

Summary

In this chapter, we covered the basics of functional programming, including high-order functions, pure functions, recursion functions, and lazy evaluation. We also covered functional collections and we implemented a functional collection, using a functional programming style.

In the next chapter, we'll cover a foundational stone of functional programming—immutability.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • 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

Description

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.

Who is this book 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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 23, 2018
Length: 350 pages
Edition : 1st
Language : English
ISBN-13 : 9781788476485
Vendor :
Google
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Feb 23, 2018
Length: 350 pages
Edition : 1st
Language : English
ISBN-13 : 9781788476485
Vendor :
Google
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 152.97
Hands-On Microservices with  Kotlin
$48.99
Learning Concurrency in Kotlin
$48.99
Functional Kotlin
$54.99
Total $ 152.97 Stars icon

Table of Contents

15 Chapters
Kotlin – Data Types, Objects, and Classes Chevron down icon Chevron up icon
Getting Started with Functional Programming Chevron down icon Chevron up icon
Immutability - It&#x27;s Important Chevron down icon Chevron up icon
Functions, Function Types, and Side Effects Chevron down icon Chevron up icon
More on Functions Chevron down icon Chevron up icon
Delegates in Kotlin Chevron down icon Chevron up icon
Asynchronous Programming with Coroutines Chevron down icon Chevron up icon
Collections and Data Operations in Kotlin Chevron down icon Chevron up icon
Functional Programming and Reactive Programming Chevron down icon Chevron up icon
Functors, Applicatives, and Monads Chevron down icon Chevron up icon
Working with Streams in Kotlin Chevron down icon Chevron up icon
Getting Started with Arrow Chevron down icon Chevron up icon
Arrow Types Chevron down icon Chevron up icon
Kotlin&#x27;s Quick Start Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
(1 Ratings)
5 star 0%
4 star 0%
3 star 100%
2 star 0%
1 star 0%
Yassine Jan 28, 2020
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
En general muy superficial, demasiado centrado en el lenguaje en si. Esperaba que se centrara mas en como usar Kotlin funcionalmente en vez de explicar el lenguaje en si. Solo los dos ultimos capitulos merecen la pena y son mas cortos que el resto.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.