Skip to content

Instantly share code, notes, and snippets.

@patrickjcurran
patrickjcurran / Algebras.md
Last active July 1, 2024 15:01
Vistors, F-Algebras, and Transducers

Vistors, F-Algebras, and Transducers

So Doug made an interesting PR, which uses the visitor pattern described in Haoyi's post.

As I was trying to understand the vistor pattern, I wanted to see how it compared to using F-algebras.

Haoyi describes the benefits of the vistor pattern to include:

  • Composability
  • Not creating intermediate datastructures
@patrickjcurran
patrickjcurran / monads.md
Last active January 6, 2020 03:14
Functions, Products, Coproducts, Functors, and Monads

There are a couple of basic constructions from math the end up being data types in (for example) cats in scala. Unfortunately, the names of the data types tend to obscure the underlying mathematical construction IMHO. This is meant to be a short description of a few data types in terms of products, co-products and functions.

Writer

In scala the Tuple2 aka (?,?) is the canoncical product of two types. This is a covariant functor in both of its type parameters. Here's map in the second parameter (for (A,?)).

def map[A,B,C](x: (A,B), f: B => C): (A,C) = (x._1, f(x._2))

If the first parameter is a semigroup then we can define flatten and it is therefore a monad. The writer monad.

def flatten[A: Semigroup, B](x: (A, (A, B))): (A, B) = (x._1 |+| x._1._1, x._1._2)