Practical FP in Scala: A hands-on approach
$39.99
Minimum price
$49.99
Suggested price

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:

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.

  • Share this book

  • Feedback

About the Author

Gabriel Volpe
Gabriel Volpe

Gabriel Volpe is a Software Engineer, specialized in functional programming, from Buenos Aires, Argentina. He has been writing code since 2005, and nowadays writes Haskell & Scala, while keeping reproducible builds via Nix.

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
  • 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
  • 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
  • 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
  • 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
  • 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

The Leanpub 45-day 100% Happiness Guarantee

Within 45 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$10,894,929writing, 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

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF, EPUB and/or MOBI files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub