Practical FP in Scala
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.
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 $13 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