Tune in to the tools and techniques in the Elm ecosystem.
032: Elm's Universal Pattern
- Joël Quenneville (Twitter)
- Joël's blog post Elm's Universal Pattern
map2
Maybe.map2
Metaphors
Some common metaphors for Elm's Universal Pattern (Applicative Pattern).
Mapping
Combining
Lifting
Wrapping and unwrapping boxes
Blog post on Two ways of looking at map functions
Examples
- Random generators
- Apply mapping functions to vanilla value functions to keep things clean
Tips
Separate branching code from doing code (discussed in-depth in Joël's blog post Problem Solving with Maybe)
Stay at one level of abstraction
Json decoders as combining functions
Scott Wlaschin Railway Oriented Programming
Dillon's blog post Combinators - Inverting Top-Down Transforms
The JSON structure and Elm type don't have to mirror each other - start with your ideal type and work backwards
Applicative pattern
Applicative needs 1) way to construct, 2)
map2
orandMap
Json.Decode.Pipeline.required
function
Record constructors
- Practice writing it with an anonymous function to convince yourself it's equivalent
- Record constructors are just a plain old elm function
map2
doesn't take a type, it takes a function -NoRedInk/elm-json-decode-pipeline
is a useful reference for implementing this kind of api on your own- Applicative Laws in Haskell
- Monomorphic vs polymorphic
- Parser Combinators
elm/parser
episode- Joël's blog posts on the ThoughtBot blog
- Joël's Random generators talk
- Joël's Maybe talk
Some more blog posts by Joël that related to Elm's Universal Pattern:
Joël's journey to building a parser combinator:
- Nested cases - https://ellie-app.com/b9nGmZVp9Vca1
- Extracted Result functions - https://ellie-app.com/b9qtqTf8zYda1
- Introducing a Parser alias and map2 - https://ellie-app.com/b9MwZ3y4t8ra1
- Re-implementing with elm/parser - https://ellie-app.com/b9NZhkTGdfya1
- Getting Unstuck with Elm JSON Decoders - because mapping is universal, you can solve equivalent problems with the same pattern (described in this post)