(Draft) Functional Programming

Page content

Best entry video

Functional Programming in 40 Minutes • Russ Olsen • GOTO 2018

You can find the slide here.

Mindset

  • We don’t need to put our knowledge aside when learning functional programming.
  • It’s more of a refactoring.

Functions in mathmatics

  • It’s just maps from set to set.
  • Functions donn’t compute somthing in math. It just is. A thing.
  • Once the function was defined, the input-output correspondence never changes by other factors, which means, there is no side effects.

Borrow the concept from mathmatics

Programmers borrow the comcept of the functions from mathmetics.

  • Make all of the data structures immutable. To do so, we can easily follow the complex code base. (it’s radical solution.)
  • What if you want to mutate the value? -> copy and change (create new data)
  • How to reduce a lot of copy -> copy&store only differences ~ persistent data structure (like copy on write, copy on modification)

Of course, immutable datastructures are not bad idea, but all our daily works (the customer cares) are side effects (mutate)! So, we need a bridge between pure functional idea and real “dirty” world.

Mutable state in FP (Not fully understand as of 15.Jan.2021)

An inpure FP language allows mutable directly. And in pure FP language, we can mutate with the concept like monad.

  • In Clojure, it is solved by “Atoms”.
  • For database operation or REST API, Clojure use Agents/Actors.
  • Most of those solutions are like “putting the functions in a queue (the queue is the “dirty” world), and execute sequentially”.

FPI is not a magic.

Monad

I reviewed monad after I learned Option in Rust, and after than, everything was much easiler to understand before.

After several investigation, the most comprehensive material for me was the Wikipedia page.

In functional programming, a monad is a type that wraps another type and gives some form of quality to the underlying type.

Here is how I built the knowledge:

  1. Suppose a type T.
  2. A monad consists of three components.
    1. A type constructor M.
    2. A type converter unit : T -> M T.
    3. A combinator/bind (>>=) : (M T, T -> M U)
  3. A monad constructor M create a new type from a type T, called M T. This type is called a monadic type. The value of the type M T is called a monadic value.
  4. A type converter wraps the original value of type T in a new monadic value of type M T. The converter is often called unit or return: unit: T -> M T.
  5. A monad also has a capability of feeding a value of type T in a monadic function f. A “monadic function” is the function which returns a monad. This is the role of a combinator, and it is called bind or flatMap. What combinator do is unwrapping a monadic variable, then inserting it into a monadic function/expression, and finally resulting in a new monadic value: mx : M T and f : T → M U, then (mx >>= f) : M U.

Combinator

  • Why the name is “combinator”? Because it combines two monadic functions. $$ \lambda: a \rightarrow f(a) \underbrace{>>=}_{combinator} \lambda: b \rightarrow g(b) $$ , where $f: T \rightarrow M U$ and $g: U \rightarrow MV$.

  • The actual behavior of combinator is depend on implementation which fullfill associativity and unit, corresponding to its monad.

  • “How to unwrap the monadic value from $f$” is one of responsibility of combinator.

  • About combinator: https://www.youtube.com/watch?v=ZhuHCtR3xq8

  • Monad by example Maybe in Haskell (or Option in Rust): https://www.youtube.com/watch?v=t1e8gqXLbsU

Two sideways (should be reviewed):

Old notes

Overview of FP

Good entry point how to think FP:

https://medium.com/@olxc/switching-from-oop-to-functional-programming-4187698d4d3

related topics of the post above

Funcional style in Python (official)

https://docs.python.org/3/howto/functional.html