Functional lenses in Haskell
Minimum price
$20.00
$40.00
EPUB
1,401
Readers
420
Pages
101,268Words
About
Hi! I'm Chris, the author of Optics By Example!
Optics By Example is a one-stop comprehensive guide to mastering optics in functional programming languages. It covers everything you need to know to go from complete beginner to advanced. Each topic is accompanied by copious examples and exercises!
See the Sample for a complete list of chapters, but here are some highlights:
- Complete introductions to Lenses, Folds, Traversals, Prisms and Isos
- Learn the magic of Indexed optics
- Composing Optics
- Lenses & Records
- Fixing "The Record Problem"
- Application design techniques using Classy optics
- Breakdowns of laws and limitations for each optic type
- Virtual lenses
- Validation lenses
- A guide to fully polymorphic optics
- A guide to "operator-style"
- Learn to query, filter, and aggregate data with surgical precision
- Learn how to use higher-order optics effectively
- Learn to write custom versions of all optics types for your own applications
- Manipulate data types with a clean and consistent interface
- Learn the monadic DSLs for using lenses in real monad stacks
WHY OPTICS?
Optics have become a wonderful and powerful new way of working with immutable data structures. They're almost a necessity at this point; and the benefits they bring in terms of re-usability, simplification of code maintenance & refactoring, and expressivity are immense! However, learning to use optics can be tricky or even intimidating at first, it's not always clear how to build up a large complicated mutation or query from the building blocks of optics. Optics by example is here to help!
This book is great as a first guide to optics, as a reference guide for discovering and mastering the optics you need for day-to-day problems, and as a springboard into new ideas and tools which you may not have even heard of.
Author
Testimonials
Instabuy. Chris is one of the best FP mentors I've ever met. When I heard about the book I stopped reading anything else about optics. This is all I'm gonna need!

Riccardo Odone
Instabuy
I'm really happy Optics by Example is out! I've been programming in Haskell for almost two years and I've been mostly staying away from optics/lens until now due to their infamous errors and lack of good tutorials/references. This book is a great and thorough introduction, the pacing's great, and it's packed with fun exercises. I recommended it to all my colleagues, and I recommend it to anyone doing Haskell/FP!

Evie Ciobanu
Great Pacing
Optics by Example opened my eyes to the benefits of Lenses et al. First I was blind but now I see!

Fintan Halpenny
Eye Opener
I used to be an optics skeptic. Not skeptical that optics are useful, but skeptical that you could write maintainable _and_ widely approachable code using them. This book changed my mind. I now think that it _is_ possible to make optics, even the more powerful parts, approachable to people of all skill levels. Chris told me at some point that he had been teaching novices to be competent with lens, and at the time, I just didn't believe him. Now I do.

Chessai
Contents
1Obligatory Preamble
- 1.1Why should I read this book?
- 1.2How to read this book
- 1.3Chosen language and optics encodings
- 1.4Your practice environment
- 1.5Following examples
- 1.6About the type signatures
- 1.7About the exercises
2Optics
- 2.1What are optics?
- 2.2Strengths
- 2.3Weaknesses
- 2.4Practical optics at a glance
- 2.5Impractical optics at a glance
3Lenses
- 3.1Introduction to Lenses
- Anatomy
- Exercises – Optic Anatomy
- 3.2Lens actions
- Viewing through lenses
- Setting through a lens
- Exercises - Lens Actions
- 3.3Lenses and records
- Lenses subsume the “accessor” pattern
- Building a lens for a record field
- Exercises - Records Part One
- Getting and setting with a field lens
- Modifying fields with a lens
- Automatically generating field lenses
makeLenses- Exercises - Records Part Two
- 3.4Limitations
- Is it a Lens?
- Is it a Lens? – Answers
- 3.5Lens Laws
- Why do optics have laws?
- The Laws
- Breaking Laws
- Case Study:
_1 - Case Study:
msg - Case Study:
lensProduct - Exercises - Laws
- 3.6Virtual Fields
- What’s a virtual field
- Writing a virtual field
- Breakage-free refactoring
- Exercises – Virtual Fields
- 3.7Data correction and maintaining invariants
- Including correction logic in lenses
- Exercises – Self-Correcting Lenses
4Polymorphic Optics
- 4.1Introduction to polymorphic optics
- Simple vs Polymorphic optics
- 4.2When do we need polymorphic lenses
- Type-changing focuses
- Changing type variables with polymorphic lenses
- Exercises – Polymorphic Lenses
- 4.3Composing Lenses
- How do I update fields in deeply nested records?
- Composing update functions
- Composing Lenses
- How do Lens Types Compose?
- Exercises – Lens Composition
5Operators
- 5.1Lens Operators
- 5.2
viewa.k.a.^. - 5.3
seta.k.a..~ - 5.4Chaining many operations
- 5.5Using
%~a.k.a.over - 5.6Learning Hieroglyphics
- 5.7Modifiers
- 5.8When to use operators vs named actions?
- 5.9Exercises – Operators
6Folds
- 6.1Introduction to Folds
- Focusing all elements of a container
- Decomposing Problems
- Collapsing the Set
- Collecting focuses as a list
- Using lenses as folds
- Composing folds
- Foundational fold combinators
- Exercises – Simple Folds
- 6.2Custom Folds
- Mapping over folds
- Combining multiple folds on the same structure
- Exercises – Custom Folds
- 6.3Fold Actions
- Writing queries with folds
- Queries case study
- Folding with effects
- Combining fold results
- Using ‘view’ on folds
- Customizing monoidal folds
- Exercises – Fold Actions
- 6.4Higher Order Folds
- Taking, Dropping
- Backwards
- TakingWhile, DroppingWhile
- Exercises – Higher Order Folds
- 6.5Filtering folds
- Filtered
- Exercises – Filtering
- 6.6Fold Laws
7Traversals
- 7.1Introduction to Traversals
- How do Traversals fit into the hierarchy?
- A bit of Nostalgia
- From fold to traversal
- 7.2Traversal Combinators
- Traversing each element of a container
- More Combinators
- Traversing multiple paths at once
- Focusing a specific traversal element
- 7.3Traversal Composition
- Exercises – Simple Traversals
- 7.4Traversal Actions
- A Primer on Traversable
- Traverse on Traversals
- Infix traverseOf
- Using Traversals directly
- Exercises – Traversal Actions
- 7.5Custom traversals
- Optics look like
traverse - Our first custom traversal
- Traversals with custom logic
- Case Study: Transaction Traversal
- Exercises – Custom Traversals
- 7.6Traversal Laws
- Law One: Respect Purity
- Law Two: Consistent Focuses
- Good Traversal Bad Traversal
- Exercises – Traversal Laws
- 7.7Advanced manipulation
- partsOf
- Polymorphic partsOf
- partsOf and other data structures
- Exercises – partsOf
8Indexable Structures
- 8.1What’s an “indexable” structure?
- 8.2Accessing and updating values with ‘Ixed’
- The
IxedClass - Accessing and setting values with
ix - Indexed Structures
- Indexing monomorphic types
- Indexing stranger structures
- 8.3Inserting & Deleting with ‘At’
- Map-like structures
- Manipulating Sets
- Exercises – Indexable Structures
- 8.4Custom Indexed Data Structures
- Custom Ixed: Cyclical indexing
- Custom At: Address indexing
- Exercises – Custom Indexed Structures
- 8.5Handling missing values
- Checking whether updates succeed
- Fallbacks with ‘failing’
- Default elements
- Checking fold success/failure
- Exercises – Missing Values
9Prisms
- 9.1Introduction to Prisms
- How do Prisms fit into the hierarchy?
- Simple Pattern-Matching Prisms
- Checking pattern matches with prisms
- Generating prisms with makePrisms
- Embedding values with prisms
- Other types of patterns
- Exercises – Prisms
- 9.2Writing Custom Prisms
- Rebuilding
_Justand_Nothing - Matching String Prefixes
- Cracking the coding interview: Prisms style!
- Exercises – Custom Prisms
- 9.3Laws
- Law One: Review-Preview
- Law Two: Prism Complement
- Law Three: Pass-through Reversion
- Summary
- Exercises – Prism Laws
- 9.4Case Study: Simple Server
- Path prefix matching
- Altering sub-sets of functions
- Matching on HTTP Verb
10Isos
- 10.1Introduction to Isos
- How do Isos fit into the hierarchy?
- There and back again
- 10.2Building Isos
- 10.3Flipping isos with
from - 10.4Modification under isomorphism
- 10.5Varieties of isomorphisms
- Composing isos
- Exercises – Intro to Isos
- 10.6Projecting Isos
- Exercises – Projected Isos
- 10.7Isos and newtypes
- Coercing with isos
- Newtype wrapper isos
- 10.8Laws
- The one and only law: Reversibility
- Exercises – Iso Laws
11Indexed Optics
- 11.1What are indexed optics?
- 11.2Index Composition
- Custom index composition
- Exercises – Indexed Optics
- 11.3Filtering by index
- Exercises – Index Filters
- 11.4Custom indexed optics
- Custom IndexedFolds
- Custom IndexedTraversals
- Index helpers
- Exercises – Custom Indexed Optics
- 11.5Index-preserving optics
12Dealing with Type Errors
- 12.1Interpreting expanded optics types
- 12.2Type Error Arena
- First Foe: Level 1 Lenslion
- Level 2 Tuplicant
- Level 3 Settersiren
- Level 4 Composicore
- Level 5 Foldasaurus
- Level 6 Higher Order Beast
- Level 7 Traversacula
13Optics and Monads
- 13.1Reader Monad and View
- 13.2State Monad Combinators
- 13.3Magnify & Zoom
14Classy Lenses
- 14.1What are classy lenses and when do I need them?
- No duplicate record fields
- Separating logic and minimizing global knowledge
- Granular dependencies with
makeFields - Field requirements compose
- 14.2
makeFieldsvsmakeClassy
15JSON
- 15.1Introspecting JSON
- 15.2Diving deeper into JSON structures
- Tracking down mistakes
- 15.3Traversing into multiple JSON substructures
- Traversing Arrays
- Traversing Objects
- 15.4Filtering JSON Queries
- 15.5Serializing & Deserializing within an optics path
- 15.6Exercises: Kubernetes API
- BONUS Questions
16Uniplate - Manipulating recursive data
- 16.1A Brief History
- 16.2Control.Lens.Plated
- Children
- Rewrite
- Universe
- Transform
- Deep
- 16.3Overriding
plate - 16.4The magic of
biplate - 16.5Exercises – Uniplate
17generic-lens
- 17.1Generic Lenses
- Record Fields
- Positional Lenses
- Typed Lenses
- Altogether Now
- Subtype Lenses
- 17.2Generic Traversals
- Types Traversal
- Parameter Traversals
- 17.3Generic Prisms
- Constructor Prisms
- Typed Prisms
- 17.4Exercises – Generic Lens
18Appendices
- 18.1Optic Composition Table
- 18.2Optic Compatibility Chart
- 18.3Operator Cheat Sheet
- Legend for Getters
- Legend for Setters/Modifiers
- 18.4Optic Ingredients
19Answers to Exercises
- 19.1Optic Anatomy
- 19.2Lens Actions
- 19.3Records Part One
- 19.4Records Part Two
- Laws
- 19.5Virtual Fields
- 19.6Self-Correcting Lenses
- 19.7Polymorphic Lenses
- 19.8Lens Composition
- 19.9Operators
- 19.10Simple Folds
- 19.11Writing Custom Folds
- 19.12Querying Using Folds
- 19.13Higher Order Folds
- 19.14Filtering
- 19.15Simple Traversals
- 19.16Traversal Actions
- 19.17Custom Traversals
- 19.18Traversal Laws
- 19.19partsOf
- 19.20Indexable Structures
- 19.21Custom Indexed Structures
- 19.22Missing Values
- 19.23Prisms
- 19.24Custom Prisms
- 19.25Prism Laws
- 19.26Intro to Isos
- 19.27Projected Isos
- 19.28Iso Laws
- 19.29Indexed Optics
- 19.30Index Filters
- 19.31Custom Indexed Optics
- 19.32Type Errors
- First Foe: Level 1 Lenslion
- Level 2 Tuplicant
- Level 3 Settersiren
- Level 4 Composicore
- Level 5 Foldasaurus
- Level 6 Foldasaurus
- Level 7 Traversacula
- 19.33Kubernetes API
- BONUS Questions
- 19.34Uniplate
- 19.35Generic Lens
20Thanks
- 20.1Patreon Supporters
- 20.2Book Cover
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.
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 earned over $15 million writing, publishing and selling 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
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 and/or EPUB files and then publish with one click!) It really is that easy.