Practical FP in Scala: A hands-on approach
Practical FP in Scala: A hands-on approach
About the Book
Practical FP in Scala: A hands-on approach, is a book for intermediate to advanced Scala developers. Aimed at those who understand functional effects, referential transparency and the benefits of functional programming to some extent but who are missing some pieces to put all these concepts together to build a large application in a time-constrained manner.
Throughout the chapters we will design, architect and develop a complete stateful application serving an API via HTTP, accessing a database and dealing with cached data, using the best practices and best functional libraries available in the Cats ecosystem such as Cats Effect, Fs2, Http4s, Skunk, Refined & others.
You will also learn about common design patterns such as managing state, error handling and anti-patterns, all accompanied by clear examples. Furthermore, in the Bonus Chapter, we will dive into some advanced concepts such as MTL and Optics, and will explore Fs2 streams with a few interesting examples.
The book is supplemented by two Scala projects:
- Shopping Cart application: https://github.com/gvolpe/pfps-shopping-cart
- Standalone examples: https://github.com/gvolpe/pfps-examples
Looking for a printed version? You can get a Softcover (paperback) here.
Got some feedback? Please email the author here: pfpscala at gmail dot com.
Bundles that include this book
Table of Contents
- Preface
-
Acknowledgments
- People
- Software
- Fonts
- Dependency version
- Prerequisites
-
How to read this book
- Conventions used in this book
-
1. Chapter 1: Best practices
-
1.1 Strongly-typed functions
- 1.1.1 Value classes
- 1.1.2 Newtypes
- 1.1.3 Refinement types
- 1.1.4 Runtime validation
-
1.2 Encapsulating state
- 1.2.1 In-memory counter
-
1.3 Sequential vs concurrent state
- 1.3.1 State Monad
- 1.3.2 Atomic Ref
-
1.4 Shared state
- 1.4.1 Regions of sharing
- 1.4.2 Leaky state
-
1.5 Anti-patterns
- 1.5.1 Seq: a base trait for sequences
- 1.5.2 About Monad Transformers
- 1.5.3 Boolean blindness
-
1.6 Error handling
- 1.6.1 MonadError & ApplicativeError
- 1.6.2 Either Monad
- 1.6.3 Classy prisms
- 1.7 Summary
-
1.1 Strongly-typed functions
-
2. Chapter 2: Tagless final encoding
-
2.1 Algebras
- 2.1.1 Naming conventions
-
2.2 Interpreters
- 2.2.1 Building interpreters
- 2.3 Programs
-
2.4 Implicit vs explicit parameters
- 2.4.1 Achieving modularity
- 2.4.2 Implicit convenience
- 2.4.3 Capability traits
-
2.5 Why Tagless Final?
- 2.5.1 Parametricity
- 2.5.2 Comparison
- 2.6 Summary
-
2.1 Algebras
-
3. Chapter 3: Shopping Cart project
-
3.1 Business requirements
- 3.1.1 Third-party payments API
- 3.1.2 Identifying the domain
- 3.1.3 Identifying HTTP endpoints
-
3.2 Technical stack
- 3.2.1 A note on Cats Effect
- 3.3 Summary
-
3.1 Business requirements
-
4. Chapter 4: Business logic
- 4.1 Identifying algebras
-
4.2 Data access and storage
- 4.2.1 Health check
-
4.3 Defining programs
- 4.3.1 Checkout
- 4.3.2 Retrying effects
- 4.4 Architecture
- 4.5 Summary
-
5. Chapter 5: HTTP layer
- 5.1 A server is a function
- 5.2 HTTP Routes #1
-
5.3 Authentication
- 5.3.1 JWT Auth
- 5.4 HTTP Routes #2
- 5.5 Composing routes
-
5.6 Middlewares
- 5.6.1 Compositionality
- 5.7 HTTP server
- 5.8 Entity codecs
-
5.9 HTTP Client
- 5.9.1 Payment client
- 5.9.2 Creating a client
- 5.10 Summary
-
6. Chapter 6: Typeclass derivation
- 6.1 Standard derivations
-
6.2 JSON codecs
- 6.2.1 Map codecs
- 6.3 Orphan instances
-
6.4 Identifiers
- 6.2.1 GenUUID & IsUUID
- 6.2.2 Custom derivation
- 6.5 Validation
- 6.6 Http4s derivations
- 6.7 Higher-kinded derivations
- 6.8 Summary
-
7. Chapter 7: Persistent layer
-
7.1 Skunk & Doobie
- 7.1.1 Session Pool
- 7.1.2 Connection check
- 7.1.3 Queries
- 7.1.4 Commands
- 7.1.5 Interpreters
- 7.1.6 Streaming & Pagination
-
7.2 Redis for Cats
- 7.2.1 Connection
- 7.2.2 Interpreters
- 7.3 Health check
- 7.4 Blocking operations
-
7.5 Transactions
- 7.5.1 Compositionality
- 7.6 Summary
-
7.1 Skunk & Doobie
-
8. Chapter 8: Testing
- 8.1 Functional test framework
-
8.2 Generators
- 8.2.1 About forall
- 8.2.2 Application data
-
8.3 Business logic
- 8.3.1 Happy path
- 8.3.2 Expectations
- 8.3.3 Empty cart
- 8.3.4 Unreachable payment client
- 8.3.5 Recovering payment client
- 8.3.6 Failing orders
- 8.3.7 Failing cart deletion
-
8.4 HTTP
- 8.4.1 Routes
- 8.4.2 Clients
- 8.5 Law testing
-
8.6 Integration tests
- 8.6.1 Shared resources
- 8.6.2 Postgres
- 8.6.3 Redis
- 8.7 Summary
-
9. Chapter 9: Assembly
- 9.1 Logging
-
9.2 Tracing
- 9.2.1 Ecosystem
- 9.3 Configuration
- 9.4 Modules
- 9.5 Resources
- 9.6 Main
- 9.7 Summary
-
10. Chapter 10: Ship it!
-
10.1 Docker image
- 10.1.1 Optimizing image
- 10.1.2 Run it locally
-
10.2 Continuous integration
- 10.2.1 Dependencies
- 10.2.2 CI build
- 10.2.3 Nix shell
- 10.2.4 Furthermore
- 10.3 Summary
-
10.1 Docker image
-
11. Bonus Chapter
-
11.1 MTL (Monad Transformers Library)
- 11.1.1 Managing state
- 11.1.2 Accessing context
-
11.2 Optics
- 11.2.1 Lenses
- 11.2.2 Prisms
-
11.3 Aspect Oriented Programming
- 11.3.1 Tofu's Mid
-
11.4 Concurrency
- 11.4.1 Producer-Consumer
- 11.4.2 Effectful streams
- 11.4.3 Interruption
- 11.4.4 Multiple subscriptions
- 11.4.5 (Un)Cancelable regions
- 11.4.6 Resource safety
- 11.4.6 Finite State Machine
- 11.5 Summary
-
11.1 MTL (Monad Transformers Library)
Other books by this author
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.
See full terms
Do Well. Do Good.
Authors have earned$11,595,069writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
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), EPUB (for phones and tablets) and MOBI (for 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
Top Books
C++20 - The Complete Guide
Nicolai M. JosuttisAll the new language and library features of C++20 (for those who know previous versions).
The book presents all new language and library features of C++20. Learn how this impacts day-to-day programming, to benefit in practice, to combine new features, and to avoid all new traps.
Buy early, pay less, free updates.
Other books:
Jetpack Compose internals
Jorge CastilloJetpack Compose is the future of Android UI. Master how it works internally and become a more efficient developer with it. You'll also find it valuable if you are not an Android dev. This book provides all the details to understand how the Compose compiler & runtime work, and how to create a client library using them.
SignalR on .NET 6 - the Complete Guide
Fiodar SazanavetsLearn everything there is to learn about SignalR and how to integrate it with the latest .NET 6 and C# 10 features. Learn how to connect any type of client to SignalR, including plain WebSocket client. Learn how to build interactive applications that can communicate with each other in real time without making excessive calls.
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
R Programming for Data Science
Roger D. PengThis book brings the fundamentals of R programming to you, using the same material developed as part of the industry-leading Johns Hopkins Data Science Specialization. The skills taught in this book will lay the foundation for you to begin your journey learning data science. Printed copies of this book are available through Lulu.
The easiest way to learn design patterns
Fiodar SazanavetsLearn design patterns in the easiest way possible. You will no longer have to brute-force your way through each one of them while trying to figure out how it works. The book provides a unique methodology that will make your understanding of design patterns stick. It can also be used as a reference book where you can find design patterns in seconds.
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
CCIE Service Provider Version 4 Written and Lab Exam Comprehensive Guide
Nicholas RussoThe service provider landscape has changed rapidly over the past several years. Networking vendors are continuing to propose new standards, techniques, and procedures for overcoming new challenges while concurrently reducing costs and delivering new services. Cisco has recently updated the CCIE Service Provider track to reflect these changes; this book represents the author's personal journey in achieving that certification.
Cronache di Domain-Driven Design
Francesco Strazzullo, Matteo Baglini, Gianluca Padovani, Emanuele DelBono, Marco Consolaro, Alessandro Colla, Uberto Barbini, Alberto Acerbis, Julie Camosseto, DDD Open, and Alberto BrandoliniCronache di Domain-Driven Design: un libro corale in italiano fatto di storie indipendenti tra loro, che sono il risultato dell’applicazione di Domain-Driven Design su progetti reali.
Functional event-driven architecture: Powered by Scala 3
Gabriel VolpeExplore the event-driven architecture (EDA) in a purely functional way, mainly powered by Fs2 streams in Scala 3!
Leverage your functional programming skills by designing and writing stateless microservices that scale, powered by stateful message brokers.
Top Bundles
- #1
Practical FP in Scala + Functional event-driven architecture
2 Books
Practical FP in Scala (A hands-on approach) & Functional event-driven architecture, aka FEDA, (Powered by Scala 3), together as a bundle! The content of PFP in Scala is a requirement to understand FEDA so why not take advantage of this bundle!? - #2
Software Architecture for Developers: Volumes 1 & 2 - Technical leadership and communication
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #3
All the Books of The Medical Futurist
6 Books
We put together the most popular books from The Medical Futurist to provide a clear picture about the major trends shaping the future of medicine and healthcare. Digital health technologies, artificial intelligence, the future of 20 medical specialties, big pharma, data privacy, digital health investments and how technology giants such as Amazon... - #4
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #6
Modern C++ Collection
3 Books
Get All about Modern C++C++ Standard Library, including C++20Concurrency with Modern C++, including C++20C++20Each book has about 200 complete code examples. Updates are included. When I update one of the books, you immediately get the updated bundle. You can expect significant updates to each new C++ standard (C++23, C++26, .. ) and also... - #7
Pattern-Oriented Memory Forensics and Malware Detection
2 Books
This training bundle for security engineers and researchers, malware and memory forensics analysts includes two accelerated training courses for Windows memory dump analysis using WinDbg. It is also useful for technical support and escalation engineers who analyze memory dumps from complex software environments and need to check for possible...