Optics By Example: Functional lenses in Haskell

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Author(s): Chris Penner
Series: 1
Publisher: Leanpub
Year: 2020

Language: English
Pages: 390
Tags: haskell programming lenses functional accessors records folds traversals

Table of Contents......Page 4
Why should I read this book?......Page 12
Your practice environment......Page 13
About the exercises......Page 15
Strengths......Page 17
Weaknesses......Page 18
Practical optics at a glance......Page 19
Impractical optics at a glance......Page 20
Anatomy......Page 22
Exercises – Optic Anatomy......Page 23
Viewing through lenses......Page 24
Exercises - Lens Actions......Page 25
Lenses subsume the ``accessor'' pattern......Page 26
Building a lens for a record field......Page 27
Exercises - Records Part One......Page 29
Getting and setting with a field lens......Page 30
Modifying fields with a lens......Page 31
Automatically generating field lenses......Page 32
Exercises - Records Part Two......Page 34
Is it a Lens?......Page 35
Is it a Lens? – Answers......Page 37
Why do optics have laws?......Page 40
The Laws......Page 41
Case Study: _1......Page 42
Case Study: msg......Page 43
Case Study: lensProduct......Page 44
Exercises - Laws......Page 47
What's a virtual field......Page 48
Writing a virtual field......Page 49
Breakage-free refactoring......Page 50
Exercises – Virtual Fields......Page 52
Including correction logic in lenses......Page 53
Exercises – Self-Correcting Lenses......Page 55
Simple vs Polymorphic optics......Page 57
Type-changing focuses......Page 58
Changing type variables with polymorphic lenses......Page 60
Exercises – Polymorphic Lenses......Page 61
Composing Lenses......Page 62
How do I update fields in deeply nested records?......Page 63
Composing update functions......Page 65
Composing Lenses......Page 68
How do Lens Types Compose?......Page 69
Exercises – Lens Composition......Page 73
Lens Operators......Page 75
set a.k.a. .......Page 76
Chaining many operations......Page 79
Using % a.k.a. over......Page 80
Learning Hieroglyphics......Page 81
Modifiers......Page 82
When to use operators vs named actions?......Page 83
Exercises – Operators......Page 84
Introduction to Folds......Page 88
Focusing all elements of a container......Page 89
Collapsing the Set......Page 91
Collecting focuses as a list......Page 92
Using lenses as folds......Page 94
Composing folds......Page 95
Foundational fold combinators......Page 96
Exercises – Simple Folds......Page 97
Custom Folds......Page 99
Mapping over folds......Page 101
Combining multiple folds on the same structure......Page 102
Exercises – Custom Folds......Page 103
Writing queries with folds......Page 105
Queries case study......Page 109
Folding with effects......Page 112
Combining fold results......Page 114
Using `view' on folds......Page 116
Customizing monoidal folds......Page 117
Exercises – Fold Actions......Page 119
Taking, Dropping......Page 121
Backwards......Page 126
Exercises – Higher Order Folds......Page 127
Filtered......Page 130
Fold Laws......Page 136
How do Traversals fit into the hierarchy?......Page 137
From fold to traversal......Page 138
Traversing each element of a container......Page 141
More Combinators......Page 143
Traversing multiple paths at once......Page 144
Focusing a specific traversal element......Page 146
Traversal Composition......Page 147
Exercises – Simple Traversals......Page 148
Traversal Actions......Page 149
A Primer on Traversable......Page 150
Traverse on Traversals......Page 153
Infix traverseOf......Page 156
Using Traversals directly......Page 157
Exercises – Traversal Actions......Page 158
Custom traversals......Page 159
Optics look like traverse......Page 160
Our first custom traversal......Page 161
Traversals with custom logic......Page 163
Case Study: Transaction Traversal......Page 164
Exercises – Custom Traversals......Page 167
Law One: Respect Purity......Page 168
Law Two: Consistent Focuses......Page 170
Good Traversal Bad Traversal......Page 171
partsOf......Page 172
Polymorphic partsOf......Page 176
partsOf and other data structures......Page 177
Exercises – partsOf......Page 178
What's an ``indexable'' structure?......Page 180
The Ixed Class......Page 181
Accessing and setting values with ix......Page 182
Indexing monomorphic types......Page 184
Indexing stranger structures......Page 185
Map-like structures......Page 187
Manipulating Sets......Page 189
Exercises – Indexable Structures......Page 190
Custom Ixed: Cyclical indexing......Page 191
Custom At: Address indexing......Page 193
Checking whether updates succeed......Page 196
Fallbacks with `failing'......Page 198
Default elements......Page 199
Checking fold success/failure......Page 201
Exercises – Missing Values......Page 203
Simple Pattern-Matching Prisms......Page 205
Generating prisms with makePrisms......Page 207
Embedding values with prisms......Page 209
Other types of patterns......Page 210
Exercises – Prisms......Page 213
Rebuilding _Just and _Nothing......Page 215
Matching String Prefixes......Page 217
Cracking the coding interview: Prisms style!......Page 218
Exercises – Custom Prisms......Page 220
Laws......Page 221
Law Two: Prism Complement......Page 222
Law Three: Pass-through Reversion......Page 223
Exercises – Prism Laws......Page 225
Case Study: Simple Server......Page 226
Path prefix matching......Page 228
Altering sub-sets of functions......Page 229
Matching on HTTP Verb......Page 233
There and back again......Page 236
Building Isos......Page 237
Flipping isos with from......Page 238
Modification under isomorphism......Page 239
Varieties of isomorphisms......Page 240
Composing isos......Page 243
Exercises – Intro to Isos......Page 245
Projecting Isos......Page 246
Exercises – Projected Isos......Page 249
Coercing with isos......Page 250
Newtype wrapper isos......Page 252
The one and only law: Reversibility......Page 254
Exercises – Iso Laws......Page 255
What are indexed optics?......Page 257
Index Composition......Page 259
Custom index composition......Page 261
Exercises – Indexed Optics......Page 263
Filtering by index......Page 264
Exercises – Index Filters......Page 265
Custom indexed optics......Page 266
Custom IndexedFolds......Page 267
Custom IndexedTraversals......Page 268
Index helpers......Page 270
Exercises – Custom Indexed Optics......Page 272
Index-preserving optics......Page 273
Interpreting expanded optics types......Page 275
First Foe: Level 1 Lenslion......Page 276
Level 2 Tuplicant......Page 277
Level 4 Composicore......Page 278
Level 6 Higher Order Beast......Page 279
Level 7 Traversacula......Page 280
Reader Monad and View......Page 281
State Monad Combinators......Page 282
Magnify & Zoom......Page 286
No duplicate record fields......Page 288
Separating logic and minimizing global knowledge......Page 291
Granular dependencies with makeFields......Page 293
Field requirements compose......Page 295
makeFields vs makeClassy......Page 296
Introspecting JSON......Page 301
Diving deeper into JSON structures......Page 304
Traversing Arrays......Page 305
Traversing Objects......Page 308
Filtering JSON Queries......Page 310
Serializing & Deserializing within an optics path......Page 312
Exercises: Kubernetes API......Page 316
BONUS Questions......Page 319
Optic Compatibility Chart......Page 321
Legend for Getters......Page 322
Legend for Setters/Modifiers......Page 323
Optic Ingredients......Page 324
Optic Anatomy......Page 326
Lens Actions......Page 327
Records Part One......Page 328
Records Part Two......Page 329
Laws......Page 330
Virtual Fields......Page 332
Self-Correcting Lenses......Page 334
Polymorphic Lenses......Page 336
Lens Composition......Page 338
Operators......Page 339
Simple Folds......Page 343
Writing Custom Folds......Page 345
Querying Using Folds......Page 347
Higher Order Folds......Page 350
Filtering......Page 353
Simple Traversals......Page 354
Traversal Actions......Page 355
Custom Traversals......Page 358
Traversal Laws......Page 359
partsOf......Page 360
Custom Indexed Structures......Page 362
Missing Values......Page 363
Prisms......Page 365
Custom Prisms......Page 366
Prism Laws......Page 368
Intro to Isos......Page 370
Projected Isos......Page 371
Iso Laws......Page 372
Indexed Optics......Page 373
Index Filters......Page 374
Custom Indexed Optics......Page 376
Level 2 Tuplicant......Page 378
Level 3 Settersiren......Page 379
Level 5 Foldasaurus......Page 380
Level 6 Foldasaurus......Page 381
Kubernetes API......Page 382
BONUS Questions......Page 384
Upcoming Chapters......Page 387
Patreon Supporters......Page 388
Book Cover......Page 390