Functional Kotlin
Functional Kotlin
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.
Bundles that include this book
Table of Contents
-
Introduction
- Who is this book for?
- What will be covered?
- The Kotlin for Developers series
- Conventions
- Code conventions
- Exercises and solutions
- Acknowledgments
-
Introduction to functional programming with Kotlin
- Why do we need to use functions as objects?
-
Function types
- Defining function types
- Using function types
- Named parameters
- Type aliases
- A function type is an interface
- Anonymous functions
-
Lambda expressions
- Tricky braces
- Parameters
- Trailing lambdas
- Result values
- Lambda expression examples
- An implicit name for a single parameter
- Closures
- Lambda expressions vs anonymous functions
- Exercise: Function types and literals
- Exercise: Observable value
-
Function references
- Top-level functions references
- Method references
- Extension function references
- Method references and generic types
- Bounded function references
- Constructor references
- Bounded object declaration references
- Function overloading and references
- Property references
- Exercise: Inferred function types
- Exercise: Function references
-
SAM Interface support in Kotlin
- Support for Java SAM interfaces in Kotlin
- Functional interfaces
-
Inline functions
- Inline functions
- Inline functions with functional parameters
- Non-local return
- Crossinline and noinline
- Reified type parameters
- Inline properties
- Costs of the inline modifier
- Using inline functions
- Exercise: Inline functions
-
Collection processing
-
forEach
andonEach
-
filter
-
map
-
mapNotNull
-
flatMap
- Exercise: Implement map
- Exercise: Optimize collection processing
-
fold
-
reduce
-
sum
-
withIndex
and indexed variants -
take
,takeLast
,drop
,dropLast
andsubList
- Exercise: Adding element at position
- Getting elements at certain positions
- Finding an element
- Counting elements
-
any
,all
andnone
- Exercise: Implement shop functions
-
partition
-
groupBy
- Associating to a map
-
distinct
anddistinctBy
- Exercise: Prime access list
-
Sorting:
sorted
,sortedBy
andsortedWith
- Sorting mutable collections
- Maximum and minimum
-
shuffled
andrandom
- Exercise: Top articles
- Exercise: Refactor collection processing
-
zip
andzipWithNext
- Windowing
-
joinToString
-
Map
,Set
andString
processing - Exercise: Passing students list
- Exercise: Best students list
- Exercise: Functional Quick Sort
- Exercise: Powerset
- Exercise: All possible partitions of a set
-
-
Sequences
- What is a sequence?
- Order is important
- Sequences do the minimum number of operations
- Sequences can be infinite
- Sequences do not create collections at every processing step
- When aren’t sequences faster?
- What about Java streams?
- Kotlin Sequence debugging
- Summary
- Exercise: Understanding sequences
-
Type Safe DSL Builders
- A function type with a receiver
- Simple DSL builders
-
Using
apply
- Simple DSL-like builders
- Multi-level DSLs
- DslMarker
- A more complex example
- When should we use DSLs?
- Summary
- Exercise: HTML table DSL
- Exercise: Creating user table row
-
Scope functions
-
let
-
also
-
takeIf
andtakeUnless
-
apply
- The dangers of careless receiver overloading
-
with
-
run
- Using scope functions
- Exercise: Using scope functions
- Exercise: orThrow
-
-
Context parameters
- Extension function problems
- Introducing context parameters
- Use cases
- Concerns
- Named context parameters
- Summary
- Exercise: Logger
-
A birds-eye view of Arrow
- Functions and Arrow Core
- Testing higher-order functions
- Error Handling
- Data Immutability with Arrow Optics
-
Final words
- Final Project: UserService
- Exercise solutions
- Notes
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 earnedover $14 millionwriting, 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