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.
You will also learn about common design patterns such as managing state, error handling and anti-patterns, all accompanied by clear examples. Furthermore, at the end of the book, we will dive into some advanced concepts such as MTL, Classy Optics and Typeclass derivation.
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
Got some feedback? Please email the author here: pfpscala at gmail dot com.
Table of Contents
- Preface
- Acknowledgments
- Dependency version
- Prerequisites
-
How to read this book
- Conventions used in this book
-
1. Chapter 1: Shopping Cart project
-
1.1 Business requirements
- 1.1.1 Third-party Payments API
- 1.1.2 Identifying the domain
- 1.1.3 Identifying Http endpoints
- 1.2 Architecture
-
1.3 Technical stack
- 1.3.1 A note on Cats Effect
-
1.1 Business requirements
-
2. Chapter 2: Design patterns
-
2.1 Strongly-typed functions
- 2.1.1 Value classes
- 2.1.2 Newtypes
- 2.1.3 Refinement types
-
2.2 Encapsulating state
- 2.2.1 In-memory counter
-
2.3 Sequential vs concurrent state
- 2.3.1 State Monad
- 2.3.2 Atomic Ref
-
2.4 Shared state
- 2.4.1 Regions of sharing
- 2.4.2 Leaky state
-
2.5 Anti-patterns
- 2.5.1 Seq: a base trait for sequences
- 2.5.2 About Monad Transformers
-
2.6 Error handling
- 2.6.1 MonadError and ApplicativeError
- 2.6.2 Either Monad
- 2.6.3 Classy prisms
-
2.1 Strongly-typed functions
-
3. Chapter 3: Tagless final encoding
-
3.1 Algebras
- 3.1.1 Naming conventions
-
3.2 Interpreters
- 3.2.1 Building interpreters
- 3.3 Programs
-
3.4 Implicit vs explicit parameters
- 3.4.1 Achieving modularity
- 3.4.2 Implicit convenience
-
3.1 Algebras
-
4. Chapter 4: Business logic
- 4.1 Identifying algebras
- 4.2 Data access and storage
-
4.3 Defining programs
- 4.3.1 Checkout
- 4.3.2 Retrying effects
-
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 Running server
-
5.8 Entity codecs
- 5.8.1 JSON codecs
- 5.8.2 Validation
-
5.9 HTTP Client
- 5.9.1 Payment client
- 5.9.2 Creating a client
-
6. Chapter 6: Persistent layer
-
6.1 Skunk & Doobie
- 6.1.1 Session Pool
- 6.1.2 Queries
- 6.1.3 Commands
- 6.1.4 Interpreters
- 6.1.5 Streaming & Pagination
-
6.2 Redis for Cats
- 6.2.1 Connection
- 6.2.2 Interpreters
- 6.3 Blocking operations
- 6.4 Health check
-
6.1 Skunk & Doobie
-
7. Chapter 7: Testing
- 7.1 Functional test suite
- 7.2 Generators
-
7.3 Business logic
- 7.3.1 Happy path
- 7.3.2 Empty cart
- 7.3.3 Unreachable payment client
- 7.3.4 Recovering payment client
- 7.3.5 Failing orders
- 7.3.6 Failing cart deletion
- 7.4 Http routes
-
7.5 Integration tests
- 7.5.1 Resource allocation
- 7.5.2 Postgres
- 7.5.3 Redis
-
8. Chapter 8: Assembly
- 8.1 Logging
- 8.2 Configuration
- 8.3 Modules
- 8.4 Resources
- 8.5 Main
-
9. Chapter 9: Deploying
-
9.1 Docker image
- 9.1.1 Optimizing image
- 9.1.2 Run it locally
-
9.2 Continuous Integration
- 9.2.1 Dependencies
- 9.2.2 CI build
- 9.2.3 Furthermore
- 9.3 Summary
-
9.1 Docker image
-
10. Chapter 10: Advanced techniques
- 10.1 Tagless Final plugin
-
10.2 MTL (Monad Transformers Library)
- 10.2.1 Managing state
- 10.2.2 Accessing context
-
10.3 Classy optics
- 10.3.1 Lenses
- 10.3.2 Prisms
- 10.3.3 Classy lenses
- 10.3.4 Classy prisms
-
10.4 Typeclass derivation
- 10.4.1 Kinds
- 10.4.2 Concrete types
- 10.4.3 Higher-kinded types
- 10.4.4 Higher-order functors
-
10.5 Effectful streams
- 10.5.1 Concurrency
- 10.5.2 Resource safety
- 10.5.3 Interruption
Authors have earned$9,882,857writing, 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
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
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++ Best Practices
Jason TurnerLevel up your C++, get the tools working for you, eliminate common problems, and move on to more exciting things!
Functional Design and Architecture
Alexander GraninSoftware Design in Functional Programming, Design Patterns and Practices, Methodologies and Application Architectures. How to build real software in Haskell with less efforts and low risks. The first complete source of knowledge.
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.
C++20
Rainer GrimmC++20 is the next big C++ standard after C++11. As C++11 did it, C++20 changes the way we program modern C++. This change is, in particular, due to the big four of C++20: ranges, coroutines, concepts, and modules.
I am a Software Engineer and I am in Charge
Alexis Monville and Michael DoyleI am a Software Engineer and I am in Charge is a real-world, practical book that helps you increase your impact and satisfaction at work no matter who you work with.
In the book, we will follow Sandrine, a fictional character who learns to think in a new way enabling her to take a different course of action.
Invest In Digital Health - The Medical Futurist's Guide
Dr. Bertalan MeskoArtificial Intelligence and Digital Health are booming. In this book, we explain why now it's a good time to invest in Digital Health and give recommendations on where to invest by looking at the top 24 technological trends we find the most promising.
Atomic Kotlin
Bruce Eckel and Svetlana IsakovaFor both beginning and experienced programmers! From the author of the multi-award-winning Thinking in C++ and Thinking in Java together with a member of the Kotlin language team comes a book that breaks the concepts into small, easy-to-digest "atoms," along with exercises supported by hints and solutions directly inside IntelliJ IDEA!
The Hundred-Page Machine Learning Book
Andriy BurkovEverything you really need to know in Machine Learning in a hundred pages.
Mastering STM32
Carmine NovielloWith more than 600 microcontrollers, STM32 is probably the most complete ARM Cortex-M platform on the market. This book aims to be the first guide around that introduces the reader to this exciting MCU portfolio from ST Microelectronics and its official CubeHAL.
Top Bundles
- #1
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... - #2
The Future of Digital Health
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 and how technology giants such as Amazon or Google want to conquer... - #3
Cisco CCNA 200-301 Complet
4 Books
Ce lot comprend les quatre volumes du guide préparation à l'examen de certification Cisco CCNA 200-301. - #4
CCDE Practical Studies (All labs)
3 Books
CCDE lab - #5
"The C++ Standard Library" and "Concurrency with Modern C++"
2 Books
Get my books "The C++ Standard Library" and "Concurrency with Modern C++" in a bundle. The first book gives you the details you should know about the C++ standard library; the second one dives deeper into concurrency with modern C++. In sum, you get more than 600 pages full of modern C++ and about 250 source files presenting the standard library... - #6
Modern Management Made Easy
3 Books
Read all three Modern Management Made Easy books. Learn to manage yourself, lead and serve others, and lead the organization. - #7
Programming with Ease
3 Books
Alle drei Bände der Serie Programming with Ease in einem Paket. Darin findest du alles, was ich dir zu den wichtigsten Phasen der Softwareentwicklung im Hinblick auf Clean Code Development für langfristig hohe Produktivität sagen kann.Im Band Slicing findest du die Anforderungsanalyse im Rahmen eines iterativ-inkrementellen Vorgehensmodells aus... - #8
Vagrant Ansible
2 Books
Unveil the power of Ansible and Vagrant with this bundle at a special price. You'll have everything you need to get started with Vagrant - learn the basics and how to create your virtual development environments, using Ansible as provisioner! About Vagrant Cookbook Vagrant Cookbook is a complete guide to get started with Vagrant and create your... - #9
Mastering Containers
2 Books
Docker and Kubernetes are taking the world by storm! These books will get you up-to-speed fast! Docker Deep Dive is over 400 pages long, and covers all objectives on the Docker Certified Associate exam.The Kubernetes Book includes everything you need to get up and running with Kubernetes! - #10
Growing Agile: The Complete Coach's Guide
7 Books
Growing Agile: Coach's Guide Series This bundle provides a collection of training and workshop plans for a variety of agile topics. The series is aimed at agile coaches, trainers and ScrumMasters who often find themselves needing to help teams understand agile concepts. Each book in the series provides the plans, slides, handouts and activity...