Leanpub Header

Skip to main content

Functional Kotlin

This book is about functional programming features in Kotlin. It first covers the essentials and then builds on them: it presents important and practical topics like collection processing, scope functions, and DSL usage and creation, context receivers.

The author is letting you choose the price you pay for this book!

Pick Your Price...
PDF
EPUB
WEB
About

About

About the Book

This book is about functional programming features in Kotlin. It first covers the essentials, and then it builds on them: it presents important and practical topics like collection processing, function references, scope functions, DSL usage and creation, and context receivers.

This position is perfect for developers who know the basics of Kotlin and want to learn well a bit more advanced features. It is a natural continuation of Kotlin for developers: Essentials.

It is based on the second day of the Kotlin for developers workshop and serves as one of the official workbooks for the workshop.

If you are interested in paperback, you can purchase it here.

Share this book

Categories

Price

Pick Your Price...

Minimum price

$20.00

$25.00

You pay

$25.00

Author earns

$20.00
$

All prices are in US $. You can pay in US $ or in your local currency when you check out.

EU customers: prices exclude VAT, which is added during checkout.

...Or Buy With Credits!

Number of credits (Minimum 2)

2
The author will earn $24.00 from your purchase!
You can get credits monthly with a Reader Membership

Author

About the Author

Marcin Moskała

Marcin Moskala is an experienced developer and Kotlin trainer. He is the founder of the kt.academy, Kotlin GDE, an official JetBrains partner for teaching Kotlin, and author of the books Effective Kotlin, Kotlin Coroutines, and Android Development with Kotlin.

Leanpub Podcast

Episode 254

An Interview with Marcin Moskała

Translations

Translations

Languages

Contents

Table of Contents

Introduction

  1. Who is this book for?
  2. What will be covered?
  3. The Kotlin for Developers series
  4. Conventions
  5. Code conventions
  6. Exercises and solutions
  7. Acknowledgments

Introduction to functional programming with Kotlin

  1. Why do we need to use functions as objects?

Function types

  1. Defining function types
  2. Using function types
  3. Named parameters
  4. Type aliases
  5. A function type is an interface

Anonymous functions

Lambda expressions

  1. Tricky braces
  2. Parameters
  3. Trailing lambdas
  4. Result values
  5. Lambda expression examples
  6. An implicit name for a single parameter
  7. Closures
  8. Lambda expressions vs anonymous functions
  9. Exercise: Function types and literals
  10. Exercise: Observable value

Function references

  1. Top-level functions references
  2. Method references
  3. Extension function references
  4. Method references and generic types
  5. Bounded function references
  6. Constructor references
  7. Bounded object declaration references
  8. Function overloading and references
  9. Property references
  10. Exercise: Inferred function types
  11. Exercise: Function references

SAM Interface support in Kotlin

  1. Support for Java SAM interfaces in Kotlin
  2. Functional interfaces

Inline functions

  1. Inline functions
  2. Inline functions with functional parameters
  3. Non-local return
  4. Crossinline and noinline
  5. Reified type parameters
  6. Inline properties
  7. Costs of the inline modifier
  8. Using inline functions
  9. Exercise: Inline functions

Collection processing

  1. forEach and onEach
  2. filter
  3. map
  4. mapNotNull
  5. flatMap
  6. Exercise: Implement map
  7. Exercise: Optimize collection processing
  8. fold
  9. reduce
  10. sum
  11. withIndex and indexed variants
  12. take, takeLast, drop, dropLast and subList
  13. Exercise: Adding element at position
  14. Getting elements at certain positions
  15. Finding an element
  16. Counting elements
  17. any, all and none
  18. Exercise: Implement shop functions
  19. partition
  20. groupBy
  21. Associating to a map
  22. distinct and distinctBy
  23. Exercise: Prime access list
  24. Sorting: sorted, sortedBy and sortedWith
  25. Sorting mutable collections
  26. Maximum and minimum
  27. shuffled and random
  28. Exercise: Top articles
  29. Exercise: Refactor collection processing
  30. zip and zipWithNext
  31. Windowing
  32. joinToString
  33. Map, Set and String processing
  34. Exercise: Passing students list
  35. Exercise: Best students list
  36. Exercise: Functional Quick Sort
  37. Exercise: Powerset
  38. Exercise: All possible partitions of a set

Sequences

  1. What is a sequence?
  2. Order is important
  3. Sequences do the minimum number of operations
  4. Sequences can be infinite
  5. Sequences do not create collections at every processing step
  6. When aren’t sequences faster?
  7. What about Java streams?
  8. Kotlin Sequence debugging
  9. Summary
  10. Exercise: Understanding sequences

Type Safe DSL Builders

  1. A function type with a receiver
  2. Simple DSL builders
  3. Using apply
  4. Simple DSL-like builders
  5. Multi-level DSLs
  6. DslMarker
  7. A more complex example
  8. When should we use DSLs?
  9. Summary
  10. Exercise: HTML table DSL
  11. Exercise: Creating user table row

Scope functions

  1. let
  2. also
  3. takeIf and takeUnless
  4. apply
  5. The dangers of careless receiver overloading
  6. with
  7. run
  8. Using scope functions
  9. Exercise: Using scope functions
  10. Exercise: orThrow

Context parameters

  1. Extension function problems
  2. Introducing context parameters
  3. Use cases
  4. Concerns
  5. Named context parameters
  6. Summary
  7. Exercise: Logger

A birds-eye view of Arrow

  1. Functions and Arrow Core
  2. Testing higher-order functions
  3. Error Handling
  4. Data Immutability with Arrow Optics

Final words

  1. Final Project: UserService

Exercise solutions

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub