The road to FP
At the moment this page is just a dump of raw ideas of what to put in the various chapters and to have a rough organization of the content. The topics below will have to be properly expanded, with references, comments, examples and exercises.
Target of this content
- To learn functional programming applied to Scala
- To be able to understand the existing online materials
- To be able to use the main FP libraries
- To have an intuition of the theory behind FP
- To become fluent in the majority of FP concepts, tools and techniques
Different way of thinking
- Put aside all other paradigms, you’ll need to learn afresh
- Paradigm shift: tons of new concepts, structures, techniques, abstractions
- Declarative programming, cleaner way of thinking
- Abstract over implementation details
- Ease of reasoning with black-box approach
- FP referential transparency makes things cumbersome. Requirements of new tools
- The unfortunate way of learning FP in Scala: Haskell, few books, blog posts
- Composability
- Design and analysis approaches: blackbox, top-down, bottom-up
- Composition of blocks
- Programming languages and modularity
Scala language feature: Case classes
- What they add to normal classes
- Pattern matching with case classes
- How to define values with types
- Build data types with case classes
- Best practices (like sealed traits, …)
Definition of pure functions and referential transparency
- Simple examples of both concepts and counter examples
- Show that loops are not good, they require mutability
- Immutability as values that cannot change
- Local mutabilty can be used
- Importance of pure function signatures
- Working with expressions
- Generating random numbers
- Memoization, or values as functions
- References:
Recursion
- Solves the lack of loops for primitives
- Recursion is the equivalent of induction reasoning
- Rules of induction and how to translate them to programmi
- Examples: list length, take nth element, flatten list, map, flatMap
- Tail recursion. But it doesn’t solve all problems!
- Recursion to iterators, important for safety:
- Recursion from two different functions
- References:
Scala language feature: Lazyness
- Lazy val
- By-name parameters
- Streams as infinite lists
- Lazy collections and how they can improve performance
- Lazyness is like functions as values
- References:
Purely functional data structures
- FP as functions acting on data, no objects around
- Sharing structure
- Techniques to work with immutable data structures
- Example: Linked list, simple tree
- Spatial hash map
- References:
Scala language feature: Higher order functions
- Polymorphic functions
- The type keyword and fixing one type parameter
- Functions as objects that can be passed around
- Composition of function (andThen, orElse)
- Currying shift the way we interpret function parameters
- References:
Functors and the world of containers
- Mapping of values in the container
- Why do I want a container?
- Mapping doesn’t change the shape of the container
- Mapping as lifting of functions
- Simple example with List+map as functor
- Covariant and contravariant functors
- Exceptions and null are evil. Option+map as functor
- References:
Scala language feature: Typeclasses
- The expression problem
- Type constructors
- Forms of polymorphism: type polymorphism, inclusion polymorphism ad-hoc polymorphism
- Simple examples of typeclasses
- The materializer function
- View bound and context bounds
- Proof that a kind exists (as in proof that a type has the X typeclass)
- Creating a typeclass for Functors
- Recursive resolution of type classes (with implicits scopes)
- References:
- http://learnyouahaskell.com/types-and-typeclasses
- https://gigiigig.github.io/tlp-step-by-step/implicits-resolution.html
- https://danielwestheide.com/blog/2013/02/06/the-neophytes-guide-to-scala-part-12-type-classes.html
- http://learnyouahaskell.com/making-our-own-types-and-typeclasses
- https://alvinalexander.com/scala/fp-book/type-classes-signpost
Monoids
- What they are, mempty and mappend
- Examples that generalize some operations like log addition
- Create new instances of monoid with type classes
- Collapse a list with fold and reduce
- Show another example like Int and summing prices of items
- Exercise: create a typeclass for Monoids and functions that work with them
- References:
Applicative functors
- What are applicatives
- Apply as lifting funtions and pipelining
- Validating errors in a pipeline
- Indipendent computations and parallel processing
- Traverse and sequence
- References:
Optics
- Updating data structures by creating copies
- Lenses
- Prisms
- References:
- https://medium.com/zyseme-technology/functional-references-lens-and-other-optics-in-scala-e5f7e2fdafe
- https://www.schoolofhaskell.com/school/to-infinity-and-beyond/pick-of-the-week/a-little-lens-starter-tutorial
- https://en.wikibooks.org/wiki/Haskell/Lenses_and_functional_references
- https://www.scala-exercises.org/monocle/lens
- http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html
Introducing monads
- The need for sequencing elaborations
- A brief history of monads: monads defined, haskell without I/O, eugenio moggi uses monads, philip wadler explains them
- How to keep a log of function calls
- Generate a sequence of random numbers
- Operate on multiple values at the same time
- Generalizing the composition of operations
- Railway oriented programming
- Scala for-comprehensions and syntactic sugar for flatMap, map and filter
- Writing your own LeMonad
- References:
Most common monads
- Intuitive monads: List, Option
- Error handling monads: Option, Either, Try
- Configuration and state monads: Reader and State
- Evaluated-values monads and examples: Reader, Future and the IO monad
- Advanced monads and examples: Trampoline, Cont
- Real world examples of using these monads, writing a more complex application
- Applicatives vs Monads
- References:
Handling errors in FP
- Reviewing Option, Either and Validated
- Scala exceptions and the ErrorMonad
- Examples of the ErrorMonad with IO
Monad transformers
- What problems they solve
- They work by unpacking and repacking containers
- Examples with OptionT and ListT
- Examples with with EitherT and ReaderT
- References
Free applicatives and free monads
- Representing algorithms with structured data
- Building a configuration system with free applicatives
- Composing free monads
- References
- https://degoes.net/articles/modern-fp
- https://stackoverflow.com/a/13388966/1215156
- http://eed3si9n.com/herding-cats/stackless-scala-with-free-monads.html
- http://functionaltalks.org/2013/06/17/runar-oli-bjarnason-dead-simple-dependency-injection/
- https://skillsmatter.com/skillscasts/3244-stackless-scala-free-monads
- https://www.youtube.com/watch?v=U0lK0hnbc4U
Tagless final
- An alternative to free monads to abstract over dependencies and implementation s
- https://rockthejvm.com/articles/tagless-final-in-scala
- https://nrinaudo.github.io/articles/tagless_final.html
- https://www.reddit.com/r/scala/comments/s6ih9p/can_you_give_me_a_brief_understanding_on_tagless/
- Drawbacks and problems
Comonads
- Non-Empty-List comonad
- Store comonad
- Product comonad
- Stream comonad
- Explaining lenses as comonads
- Conway’s game of life using Store
- Cofree Comonads
- References:
Recursion Schemes
- Practical introduction
Introduction to Category Theory
- Types and mathematics
- Definition of a category
- Mapping to types and other “things”
- What “structure” means in mathematics
- Product and Coproduct (union and sum types)
- Algebraic Data Types
- References
Basic concepts of Category Theory
- Functors in categories and relation to Scala objects. Examples of Functors
- Natural transformations and relation to Scala objects.
- Parametric polymorphism
- Algebras, Coalgebras, Catamorphisms
- https://stackoverflow.com/questions/16015020/what-does-coalgebra-mean-in-the-context-of-programming
- https://blog.hablapps.com/2016/10/11/yo-dawg-we-put-an-algebra-in-your-coalgebra/
- https://blog.hablapps.com/2017/02/20/algebras-for-the-masses/
- https://medium.com/@olxc/catamorphisms-and-f-algebras-b4e91380d134
- https://blog.hablapps.com/2016/10/11/yo-dawg-we-put-an-algebra-in-your-coalgebra/
- References:
Revisiting structures and tools using Category Theory
- How to understand Scala type system using category theory
- Representable functors, optimization
- Folding
- Monoids, definition, generalization, examples
- Adjunctions and definition of Monads and Comonads
- Free monoids and free monads
- Free monads and Yoneda
- https://underscore.io/blog/posts/2015/04/14/free-monads-are-simple.html
- https://underscore.io/blog/posts/2017/03/29/free-inject.html
- http://blog.higher-order.com/blog/2013/11/01/free-and-yoneda/
- https://medium.com/@olxc/yoneda-and-coyoneda-trick-f5a0321aeba4
- https://bartoszmilewski.com/2013/05/15/understanding-yoneda/
- https://kubuszok.com/2018/the-f-words-functors-and-friends/#functor
- Lenses and Comonad Coalgebras
Few concepts in Type theory
- Types
- Kinds (as in values <-> types <-> kinds)
- Ranks in Scala
- Kind projector
Type Level Programming
- Overcoming type erasure
- How the compiler does type resolution and infers missing parameters
- Dependent types and the Aux pattern
- Type resolution in scala: implicits, type
- https://www.youtube.com/watch?v=R8GksuRw3VI (great video on dependent types and Aux pattern)
- https://karlcode.owtelse.com/blog/2017/04/11/the-rise-and-hopefully-fall-of-the-aux-pattern-2/?mode=doc#slide-0
- Example of defining Boolean/If with types
- Example with natural numbers
- Shapeless basics: HLists, tuples, generics
- Working with types and HList is working with recursion and induction reasoning
- Map on HLists and polymorphic functions
- Encoder/Decoder derivation with shapeless
- https://www.beyondthelines.net/programming/reducing-type-class-boilerplate-with-shapeless/
- https://stackoverflow.com/questions/22418309/creating-a-shapeless-polymorphic-function-with-a-naked-type-param
- https://blog.free2move.com/shapeless-hlists-and-how-to-traverse-them/
- https://www.beyondthelines.net/programming/reducing-type-class-boilerplate-with-shapeless/
- https://olegpy.com/traversing-hlists/
- More on shapeless
- References: