Optics By Example
Optics By Example
Minimum price
Suggested price
Optics By Example

Last updated on 2019-12-11

About the Book

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


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.

About the Author

Chris Penner
Chris Penner

Hey there! I'm a programmer, coffee lover and acrobat!

I love functional programming and I love writing, seems only natural to bring the two together (especially since it's a bit tougher to match coding and acrobatics!)

Hope you enjoy my book!

Reader Testimonials

Riccardo Odone
Riccardo Odone


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!

Vladimir Ciobanu
Vladimir Ciobanu

Great Pacing

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!

Fintan Halpenny
Fintan Halpenny

Eye Opener

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

Table of Contents

  • 1 Obligatory Preamble
    • About this book
    • Why should I read this book?
    • How to read this book
    • Chosen language and optics encodings
    • Your practice environment
    • Following examples
    • About the type signatures
    • About the exercises
  • 2 Optics
    • What are optics?
    • Strengths
    • Weaknesses
    • Practical optics at a glance
    • Impractical optics at a glance
  • 3 Lenses
    • Introduction to Lenses
      • Anatomy
      • Exercises – Optic Anatomy
    • Lens actions
      • Viewing through lenses
      • Setting through a lens
      • Exercises - Lens Actions
    • Lenses 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
      • Exercises - Records Part Two
    • Limitations
      • Is it a Lens?
      • Is it a Lens? – Answers
    • Lens Laws
      • Why do optics have laws?
      • The Laws
      • Case Study: _1
      • Case Study: msg
      • Case Study: lensProduct
      • Exercises - Laws
    • Virtual Fields
      • What’s a virtual field
      • Writing a virtual field
      • Breakage-free refactoring
      • Exercises – Virtual Fields
    • Data correction and maintaining invariants
      • Including correction logic in lenses
      • Exercises – Self-Correcting Lenses
  • 4 Polymorphic Optics
    • Introduction to polymorphic optics
      • Simple vs Polymorphic optics
    • When do we need polymorphic lenses
      • Type-changing focuses
      • Changing type variables with polymorphic lenses
      • Exercises – Polymorphic Lenses
    • Composing Lenses
      • How do I update fields in deeply nested records?
      • Composing update functions
      • Composing Lenses
      • How do Lens Types Compose?
      • Exercises – Lens Composition
  • 5 Operators
    • Lens Operators
    • view a.k.a. ^.
    • set a.k.a. .~
    • Chaining many operations
    • Using %~ a.k.a. over
    • Learning Hieroglyphics
    • Modifiers
    • When to use operators vs named actions?
    • Exercises – Operators
  • 6 Folds
    • Introduction to Folds
      • Focusing all elements of a container
      • Collapsing the Set
      • Collecting focuses as a list
      • Using lenses as folds
      • Composing folds
      • Foundational fold combinators
      • Exercises – Simple Folds
    • Custom Folds
      • Mapping over folds
      • Combining multiple folds on the same structure
      • Exercises – Custom Folds
    • Fold Actions
      • Writing queries with folds
      • Queries case study
      • Folding with effects
      • Combining fold results
      • Using ‘view’ on folds
      • Customizing monoidal folds
      • Exercises – Fold Actions
    • Higher Order Folds
      • Taking, Dropping
      • Backwards
      • TakingWhile, DroppingWhile
      • Exercises – Higher Order Folds
    • Filtering folds
      • Filtered
      • Exercises – Filtering
    • Fold Laws
  • 7 Traversals
    • Introduction to Traversals
      • How do Traversals fit into the hierarchy?
      • A bit of Nostalgia
      • From fold to traversal
    • Traversal Combinators
      • Traversing each element of a container
      • More Combinators
      • Traversing multiple paths at once
      • Focusing a specific traversal element
    • Traversal Composition
      • Exercises – Simple Traversals
    • Traversal Actions
      • A Primer on Traversable
      • Traverse on Traversals
      • Infix traverseOf
      • Using Traversals directly
      • Exercises – Traversal Actions
    • Custom traversals
      • Optics look like traverse
      • Our first custom traversal
      • Traversals with custom logic
      • Case Study: Transaction Traversal
      • Exercises – Custom Traversals
    • Traversal Laws
      • Law One: Respect Purity
      • Law Two: Consistent Focuses
      • Good Traversal Bad Traversal
      • Exercises – Traversal Laws
  • 8 Indexable Structures
    • What’s an “indexable” structure?
    • Accessing and updating values with ‘Ixed’
      • The Ixed Class
      • Accessing and setting values with ix
      • Indexed Structures
      • Indexing monomorphic types
      • Indexing stranger structures
    • Inserting & Deleting with ‘At’
      • Map-like structures
      • Manipulating Sets
      • Exercises – Indexable Structures
    • Custom Indexed Data Structures
      • Custom Ixed: Cyclical indexing
      • Custom At: Address indexing
      • Exercises – Custom Indexed Structures
  • 9 Prisms
    • Introduction 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
    • Writing Custom Prisms
      • Rebuilding _Just and _Nothing
      • Matching String Prefixes
      • Cracking the coding interview: Prisms style!
      • Exercises – Custom Prisms
    • Laws
      • Law One: Review-Preview
      • Law Two: Prism Complement
      • Law Three: Pass-through Reversion
      • Summary
      • Exercises – Prism Laws
    • Case Study: Simple Server
      • Path prefix matching
      • Altering sub-sets of functions
      • Matching on HTTP Verb
  • 10 Isos
    • Introduction to Isos
      • How do Isos fit into the hierarchy?
      • There and back again
    • Building Isos
    • Flipping isos with from
    • Modification under isomorphism
    • Varieties of isomorphisms
      • Composing isos
      • Exercises – Intro to Isos
    • Projecting Isos
      • Exercises – Projected Isos
    • Isos and newtypes
      • Coercing with isos
      • Newtype wrapper isos
    • Laws
      • The one and only law: Reversibility
      • Exercises – Iso Laws
  • 11 Indexed Optics
    • What are indexed optics?
    • Index Composition
      • Custom index composition
      • Exercises – Indexed Optics
    • Filtering by index
      • Exercises – Index Filters
    • Custom indexed optics
      • Custom IndexedFolds
      • Custom IndexedTraversals
      • Index helpers
      • Exercises – Custom Indexed Optics
    • Index-preserving optics
  • 12 Optics and Monads
    • Reader Monad and View
    • State Monad Combinators
    • Magnify & Zoom
  • 13 Classy Lenses
    • What 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
    • makeFields vs makeClassy
  • 14 Appendices
    • Optic Composition Table
    • Optic Compatibility Chart
    • Operator Cheat Sheet
      • Legend for Getters
      • Legend for Setters/Modifiers
    • Optic Ingredients
  • 15 Answers to Exercises
    • Optic Anatomy
    • Lens Actions
    • Records Part One
    • Records Part Two
      • Laws
    • Virtual Fields
    • Self-Correcting Lenses
    • Lens Composition
    • Polymorphic Lenses
    • Operators
    • Simple Folds
    • Writing Custom Folds
    • Querying Using Folds
    • Higher Order Folds
    • Filtering
    • Simple Traversals
    • Traversal Actions
    • Custom Traversals
    • Traversal Laws
    • Indexable Structures
    • Custom Indexed Structures
    • Prisms
    • Custom Prisms
    • Prism Laws
    • Intro to Isos
    • Projected Isos
    • Iso Laws
    • Indexed Optics
    • Index Filters
    • Custom Indexed Optics
  • 16 Upcoming Chapters
  • 17 Thanks
    • Patreon Supporters
    • Book Cover
  • Notes

Authors have earned$8,266,888writing, 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. Free App. 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), MOBI (for Kindle) and in the free Leanpub App (for Mac, Windows, iOS and Android). 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. It really is that easy.

Learn more about writing on Leanpub