“Leads to deep insights into the nature of computation.” From the Foreword by Martin Odersky, Creator of Scala
Functional Programming in Scala is a serious tutorial for programmers looking to learn FP and apply it to the everyday business of coding. This Video Editions book guides readers from basic techniques to advanced topics in a logical, concise, and clear progression. In it, you’ll find concrete examples that open up the world of functional programming.
Functional programming (FP) is a style of software development emphasizing functions that don’t depend on program state. Functional code is easier to test and reuse, simpler to parallelize, and less prone to bugs than other code. Scala is an emerging JVM language that offers strong support for FP. Its familiar syntax and transparent interoperability with Java make Scala a great place to start learning FP.Inside:
Functional programming concepts
The whys and hows of FP
How to write multicore programs
Checks for understanding
This title assumes no prior experience with functional programming. Some prior exposure to Scala or Java is helpful.
Paul Chiusano and Rúnar Bjarnason are recognized experts in functional programming with Scala and are core contributors to the Scalaz library.
The definitive guide to functional programming for Scala and Java 8 developers! William E. Wheeler, TekSystems
Shows you the approach and mindset to raise your Scala way beyond ‘a better Java’. Fernando Dobladez, Code54
If you’ve had trouble trying to learn Functional Programming (FP), you’re not alone. In this book, Alvin Alexander — author of the Scala Cookbook and former teacher of Java and Object-Oriented Programming (OOP) classes — writes about his own problems in trying to understand FP, and how he finally conquered it.
What he originally learned is that experienced FP developers are driven by two goals: to use only immutable values, and write only pure functions. What he later learned is that they have these goals as the result of another larger goal: they want all of their code to look and work just like algebra.
While that sounds simple, it turns out that these goals require them to use many advanced Scala features — which they often use all at the same time. As a result, their code can look completely foreign to novice FP developers. As Mr. Alexander writes, “When you first see their code it’s easy to ask, ‘Why would anyone write code like this?’”
Mr. Alexander answers that “Why?” question by explaining the benefits of writing pure functional code. Once you understand those benefits — your motivation for learning FP — he shares five rules for programming in the book:
All fields must be immutable (‘val’ fields).
All functions must be pure functions.
Null values are not allowed.
Whenever you use an ‘if’ you must also use an ‘else’.
You won’t create OOP classes that encapsulate data and behavior; instead you’ll design data structures using Scala ‘case’ classes, and write pure functions that operate on those data structures.
In the book you’ll see how those five, simple rules naturally lead you to write pure, functional code that reads like algebra. He also shares one more Golden Rule for learning:
Always ask “Why”?
Lessons in the book include:
How and why to write only pure functions
Why pure function signatures are much more important than OOP method signatures
Why recursion is a natural tool for functional programming, and how to write recursive algorithms
Because the Scala ‘for’ expression is so important to FP, dozens of pages explain the details of how it works
In the end you’ll see that monads aren’t that difficult because they’re a natural extension of the Five Rules
The book finishes with lessons on FP data modeling, and two main approaches for organizing your pure functions
As Mr. Alexander writes, “In this book I take the time to explain all of the concepts that are used to write FP code in Scala. As I learned from my own experience, once you understand the Five Rules and the small concepts, you can understand Scala/FP.”
Please note that because of the limits on how large a printed book can be, the paperback version does not include all of the chapters that are in the Kindle eBook. The following lessons are not in the paperback version:
Grandma’s Cookies (a story about pure functions)
The ScalaCheck lessons
The Type Classes lessons
The appendices
Because those lessons didn’ fit in the print version, they have been made freely available online.
(Alvin Alexander (alvinalexander.com) wrote the popular Scala Cookbook for O’Reilly, and also self-published two other books, How I Sold My Business: A Personal Diary, and A Survival Guide for New Consultants.)
About the Author
Alvin took the circuitous route to software development. He managed to get a degree in Aerospace Engineering from Texas A&M University, while all he was really trying to do was play baseball. Once he became a practicing engineer, he realized he liked software and programming more than engineering. So in approximate order he taught himself Fortran, C, Unix, network administration, sed, awk, Lisp, Perl, Java, JavaScript, Python, Ruby, JRuby, PHP, and Scala. During this process he started a software consulting firm, grew it to fifteen people, sold it, and moved to Alaska. After returning to the “Lower 48,” he self-published two books (“How I Sold My Business: A Personal Diary”, and “Zen and the Art of Consulting”), and then wrote the “Scala Cookbook” for O’Reilly. He also created alvinalexander.com, which receives millions of page views every year.
Update: In July, 2021, the price of this Kindle book has been drastically reduced so you can learn Scala as inexpensively as possible.
In his latest book, Alvin Alexander, author of the Scala Cookbook and Functional Programming, Simplified, brings you a swift introduction to the Scala programming language. In a little over 200 fast-paced pages, Mr. Alexander demonstrates that Scala is a beautiful, modern, expressive programming language. The book is broken down into 56 short chapters to help you easily find what you need. Lessons include:
An introduction to Scala’s two types of variables, `val` and `var`
Scala control structures, including powerful `for` expressions and `match` expressions
An overview of Scala collections classes and methods
Object-oriented programming (OOP), including features of Scala classes and methods
Functional programming (FP), including pure functions, using functions as variables, case classes, match expressions, functional error handling, and more
Modular programming with traits
How to build Scala projects with SBT
How to write TDD and BDD unit tests with ScalaTest
Programming concurrency with Akka actors and Scala futures
To help get you started with Scala as fast as possible, the book shares many source code examples, including several open source Github projects that you can run immediately. All examples in the book have been written with Scala 2.12, and represent 2018’s “best practices” for Scala programming.
About the Author
Alvin took the circuitous route to software development. He managed to get a degree in Aerospace Engineering from Texas A&M University, while all he was really trying to do was play baseball. Once he became a practicing engineer, he realized he liked software and programming more than engineering. So in approximate order he taught himself Fortran, C, Unix, network administration, sed, awk, Lisp, Perl, Java, JavaScript, Python, Ruby, JRuby, PHP, and Scala. During this process he started a software consulting firm, grew it to fifteen people, sold it, and moved to Alaska. After returning to the “Lower 48,” he self-published two books (“How I Sold My Business: A Personal Diary”, and “Zen and the Art of Consulting”), and then wrote the “Scala Cookbook” for O’Reilly. He also created alvinalexander.com, which receives millions of page views every year.
Table of Contents:
Table of Contents
Copyright
Preface
Prelude: A Taste of Scala
Getting started
The Scala Programming Language
Hello, World
Hello, World (Version 2)
The Scala REPL
Two Types of Variables
The Type is Optional
A Few Built-In Types
Two Notes About Strings
Command-Line I/O
Control Structures
The if/then/else Construct
for Loops
for Expressions
match Expressions
try/catch/finally Expressions
Classes
Auxiliary Class Constructors
Supplying Default Values for Constructor Parameters
A First Look at Methods
Enumerations (and a Complete Pizza Class)
Traits and Abstract Classes
Using Traits as Interfaces
Using Traits Like Abstract Classes
Abstract Classes
Collections Classes
ArrayBuffer Class
List Class
Vector Class
Map Class
Set Class
Anonymous Functions
Common Methods on Sequences
Common Map Methods
A Few Miscellaneous Items
Tuples
Scala and Swing
An OOP Example
A Scala + JavaFX Example
SBT and ScalaTest
The Scala Build Tool (SBT)
Using ScalaTest with SBT
Writing BDD-style tests with ScalaTest and SBT
Functional Programming
Pure Functions
Passing Functions Around
No Null Values
Companion Objects
Case Classes
Case Objects
Functional Error Handling
Concurrency
Akka Actors
Akka Actor Examples
Futures
Summary
Preface:
Preface
Have you ever fallen in love with a programming language? I still remember when I first saw the book, The C Programming Language, and how I fell in love with its simple syntax and the ability to interact with a computer at a low level. In 1996 I loved Java because it made OOP simple. A few years later I found Ruby and loved its elegance.
Then in 2011 I was aimlessly wandering around Alaska and stumbled across the book, Programming in Scala, and I was stunned by its remarkable marriage of Ruby and Java:
The syntax was as elegant and concise as Ruby
It feels dynamic, but it’s statically typed
It compiles to class files that run on the JVM
You can use the thousands of Java libraries in existence with your Scala code
In the first edition of the book, Beginning Scala, David Pollak states that Scala will change the way you think about programming, and that’s a good thing. Learning Scala has not only been a joy, but it’s led me on a journey to appreciate concepts like modular programming, immutability, referential transparency, and functional programming, and most importantly, how those ideas help to dramatically reduce bugs in my code.
Is Scala DICEE?
DICEE is an acronym that was coined by Guy Kawasaki, who became famous as a developer evangelist for the original Apple Macintosh team. He says that great products are DICEE, meaning Deep, Indulgent, Complete, Elegant, and Emotive:
Deep: The product doesn’t run out of features and functionality after a few weeks of use. Its creators have anticipated what you’ll need once you come up to speed. As your demands get more sophisticated, you won’t need a different product.
Indulgent: A great product is a luxury. It makes you feel special when you buy it (and use it).
Complete: A great product is more than a physical thing. Documentation counts. Customer service counts. Tech support counts.
Elegant: A great product has an elegant user interface. Things work the way you’d think they would. A great product doesn’t fight you, it enhances you.
Emotive: A great product incites you to action. It is so deep, indulgent, complete, and elegant that it compels you to tell other people about it. You’re bringing the good news to help others, not yourself.
Two years after discovering Scala — way back in 2013 — I came to the conclusion that it meets the definition of DICEE, and I think it’s just as true today:
Scala is deep: After all these years I continue to learn new techniques to write better code.
Scala is indulgent: Just like Ruby, I feel special and fortunate to use a language that’s so well thought out.
Scala is complete: The documentation is excellent, terrific frameworks exist, and the support groups are terrific.
Scala is elegant: Once you grasp its main concepts you’ll fall in love with how it works just like you expect it to.
Scala is emotive: Everyone who works with it wants to tell you how special it is. Myself, I had never written a programming book in my life, but by 2012 I was eagerly mailing people at O’Reilly to tell them how much I wanted to write the Scala Cookbook.
As I write this book many years later I hope to share not just the nuts and bolts of the Scala language, but also its elegance and the joy of using it.
The introduction of functional programming concepts in Java SE 8 was a drastic change for this venerable object-oriented language. Lambda expressions, method references, and streams fundamentally changed the idioms of the language, and many developers have been trying to catch up ever since. This cookbook will help. With more than 70 detailed recipes, author Ken Kousen shows you how to use the newest features of Java to solve a wide range of problems.
For developers comfortable with previous Java versions, this guide covers nearly all of Java SE 8, and includes a chapter focused on changes coming in Java 9. Need to understand how functional idioms will change the way you write code? This cookbook—chock full of use cases—is for you.
Recipes cover:
The basics of lambda expressions and method references
Interfaces in the java.util.function package
Stream operations for transforming and filtering data
Comparators and Collectors for sorting and converting streaming data
Combining lambdas, method references, and streams
Creating instances and extract values from Java’s Optional type
New I/O capabilities that support functional streams
The Date-Time API that replaces the legacy Date and Calendar classes
Mechanisms for experimenting with concurrency and parallelism
About the Author
Ken Kousen is an independent consultant and trainer specializing in Spring, Hibernate, Groovy, and Grails. He holds numerous technical certifications, along with degrees in Mathematics, Mechanical and Aerospace Engineering, and Computer Science.
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.
Functional Programming in Kotlin is a reworked version of the bestselling Functional Programming in Scala, with all code samples, instructions, and exercises translated into the powerful Kotlin language. In this authoritative guide, you’ll take on the challenge of learning functional programming from first principles, and start writing Kotlin code that’s easier to read, easier to reuse, better for concurrency, and less prone to bugs and errors.
about the technology
Kotlin is a new JVM language designed to interoperate with Java and offer an improved developer experience for creating new applications. It’s already a top choice for writing web services, and Android apps. Although it preserves Java’s OO roots, Kotlin really shines when you adopt a functional programming mindset. By learning the core principles and practices of functional programming outlined in this book, you’ll start writing code that’s easier to read, easier to test and reuse, better for concurrency, and less prone to bugs.
about the book
Functional Programming in Kotlin is a serious tutorial for programmers looking to learn FP and apply it to the everyday business of coding. Based on the bestselling Functional Programming in Scala, this book guides intermediate Java and Kotlin programmers from basic techniques to advanced topics in a logical, concise, and clear progression. In it, you’ll find concrete examples and exercises that open up the world of functional programming. The book will deliver practical mastery of FP using Kotlin and a valuable perspective on program design that you can apply to other languages.
what’s inside
Functional programming techniques for real-world applications
Write combinator libraries
Identify common structures and idioms in functional design
Code for simplicity, modularity, and fewer bugs
about the reader
For intermediate Kotlin and Java developers. No experience with functional programming is required.
about the author
Marco Vermeulen has almost two decades of programming experience on the JVM, with much of that time spent on functional programming using Scala and Kotlin.
Rúnar Bjarnason and Paul Chiusano are the authors of Functional Programming in Scala, on which this book is based. They are internationally-recognized experts in functional programming and the Scala programming language.
See: Functional Python Programming: Discover the power of functional programming, generator functions, lazy evaluation, the built-in itertools library, and monads, 2nd Edition 2nd Revised edition