Functional Programming with Kotlin
Marco Vermeulen, Rnar Bjarnason, and Paul Chiusano
Foreword by Raul Raja
To comment go to liveBook
Manning
Shelter Island
For more information on this and other Manning titles go to
www.manning.com
Copyright
For online information and ordering of these and other Manning books, please visit www.manning.com. The publisher offers discounts on these books when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 761
Shelter Island, NY 11964
Email: orders@manning.com
2021 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Mannings policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
| Manning Publications Co. 20 Baldwin Road Technical PO Box 761 Shelter Island, NY 11964 |
Development editor: | Marina Michaels |
Technical development editor: | Mark Elston, John Guthrie, Joshua White |
Review editor: | Aleksandar Dragosavljevi |
Production editor: | Keri Hales |
Copy editor: | Tiffany Taylor |
Proofreader: | Katie Tennant |
Technical proofreader: | Jean-Franois Morin |
Typesetter: | Dennis Dalinnik |
Cover designer: | Leslie Haimes |
ISBN: 9781617297168
front matter
foreword
Functional programming has profoundly changed the way we approach and solve problems through software. Functional programming emphasizes immutability, function purity, and composition of functions as a means to build correct and predictable programsproperties in our code that have changed how we look at distributed systems, applications, and software engineering in recent decades.
Whereas a few years ago, functional programmings success was questioned and it was frequently compared or linked to other paradigms, todays adoption has extended to most language compilers and standard libraries and impacts how communities and teams approach architecture and system design.
Kotlin is a multiparadigm, multiplatform language that includes functional features such as continuations, nullable types, interfaces, pattern matching, algebraic data types, and many others that make it unique, fun, and one of the most performant languages with which to practice functional programming.
Since every language has its own notion of how functional programming manifests itself, you may be wondering what the essence of functional programming is and whether you can learn it in a principled way. Marco brings us this adaptation of the Red Book, Functional Programming in Scala, this time using Kotlin as a vehicle to illustrate these patterns from the ground up. Functional Programming in Kotlin breaks down basic concepts around function composition and algebraic design and then invites you to practice and learn through exercises and examples that illustrate how you can implement functional patterns and data types from scratch.
From common scenarios like error handling to more complex cases like streaming, Functional Programming in Kotlin complements the functional programmers learning journey and provides a foundational approach to learning core functional abstractions and patterns.
Raul Raja
Arrow maintainer
CTO 47 Degrees
preface
Writing good software is no easy task. We write code to provide machine-executable instructions that should execute with ease and efficiency. More importantly, code has another fundamental purpose: it exists as a means of clear communication with others who interact with our code now and in the future. And so, code has transcended its role as a tool for instructing machines and become one used for collaboratively instructing machines.
I have been coding since a relatively young age. It all began when I wrote BASIC on my ZX Spectrum and, later, machine language on my Commodore 64. After a long, winding road, this culminated in me becoming a passionate Java developer many years later. Up to that point, everything I had learned was of a procedural nature, a sequence of imperative steps to instruct the computer. In the early 2000s, Java blew my mind by introducing me to the concept of object orientation. I could now model real-world concepts in the memory of my computer as objects! Java also taught me the value of having a static type system that enforces specific rules at compilation time, resulting in fewer runtime issues.
As time went by, I discovered another way of thinking about software. This realization dawned on me when I started programming in another statically typed language, Scala. Functional programming was a completely new paradigm that shifted my mind from thinking about classes and objects to emphasizing the functions and methods within them. When learning Scala, I was lucky enough to have some fantastic resources at my disposal. The first was the excellent Functional Programming Principles in Scala video course by Martin Odersky on Coursera. The other was the famous Red Book, Functional Programming in Scala (Manning, 2014), into which Paul Chiusano and Rnar Bjarnason had poured many years of their experience and effort. Both of these shaped my thinking tremendously and altered the way that I perceive programming to this day.
When I began looking at Kotlin, I realized that despite the language having capabilities similar to Scalas, there was a significant focus on pragmatism, but not so much on the academic aspects of functional programming (FP) using the type system. After completing the Kotlin for Java Developers course on Coursera with a group of friends, we spoke about carrying on the study group using the material in the Red Book as a basis for our studies in typed FP while using Kotlin. Sadly, these further studies never materialized, but I carried on by myself and presented the concept of a new Kotlin FP book to Manning. My initial idea was to provide the same material as Paul and Rnar had written but with all code translated to Kotlin instead of Scala. I was delighted when Manning accepted the proposal. Once the work began, it took on a life of its own, changing a lot from the original, not only in code but also in text. Having said that, the structure of the book is still very true to the original.
Next page