β€’

tech

How I got into Functional Programming

This is my journey in learning functional programming and its core concepts, using multiple programming languages: from Lisp and Haskell, to object oriented languages like Dart and Kotlin, all the way to Typescript and the web.


Sandro Maglione

Sandro Maglione

Software

For every language I try my first instinct is searching: "Functional programming in [language]"

My journey with functional started with Haskell, with these exact lines below:

inc n = n + 1
length :: [Integer] -> Integer length [] = 0
length (x:xs) = 1 + length xs

I (like most) knew mostly OOP at the time. This was a shock: "what's this mess!?" 🀯

Now (mostly) everything is functional for me πŸ’πŸΌβ€β™‚οΈ

This is how I got here πŸ‘‡


Scheme (Racket) aka Lisp

I started with Lisp, specifically Racket (Scheme):

(lambda (x y) ; this is a comment
    (+ (* x x) (* y y)))

Completely different from anything I have seen before at the time 🀨

This language introduced me to some core ideas:

  • Every program is an expression: computation is based on evaluating expressions (no statements)
  • lambda functions (anonymous functions)
  • Recursion (specifically tail recursion)
  • Closures (higher order functions)
  • Meta-programming (macros)

I'll probably never use Racket for a production app: the real value was learning its core programming ideas

Haskell (purity)

Haskell is a pure functional language.

A pure functional language enforces immutability and no side effects

A new bag of ideas here as well:

  • Absence of side effects (purely functional computations) and how to deal with it (IO)
  • Currying
  • Type system
  • Pattern matching (everywhere)
  • Call-by-need (lazy evaluation)
  • Typeclasses

My focus was to understand why these features are useful πŸ€”

This required exploring other languages to see how they deal with the same problems πŸ‘‡

There is more 🀩

Every week I dive headfirst into a topic, uncovering every hidden nook and shadow, to deliver you the most interesting insights

Not convinced? Well, let me tell you more about it

Dart

I was interested in implementing functional ideas in dart.

Turns how OOP does not adapt too well into pure functional:

abstract class HKT<G, A> {
  const HKT();
}

mixin Functor<G, A> on HKT<G, A> {
  HKT<G, B> map<B>(B Function(A a) f);
}

Nonetheless, the main ideas still remain and work:

  • Managed side effects (IO, Task)
  • Errors as values (Either)
  • Immutability (map, fold)
  • Pattern matching (since dart 3 πŸŽ‰)
  • Lambda functions

This is when I started using dartz, and then implemented the first version of fpdart

Typescript

Typescript is more "functional friendly":

  • More focus on functions than classes
  • Better type inference (especially function return types)
  • More powerful type system (generic types)

My first library was purify-ts:

  • Maybe (aka Option)
  • Either
  • Immutability
  • Currying

Kotlin

Over time most language started adding more functional features: Kotlin is no exception.

I used Arrow:

  • Coroutines (suspend) instead of IO
  • map, fold, zip
  • Option and Either

Kotlin and Dart share a similar OOP origin: the patterns used to achieve functional programming are similar as well

OCaml

Most recently I tried OCaml as well.

OCaml goes full into pattern matching. It looks more like Haskell:

let collect_digits dig str =
  let int_option = int_of_string_opt str in
  match dig, int_option with
  | Empty, None -> Empty
  | Empty, Some n -> Full (n, n)
  | Full (f, l), None -> Full (f, l)
  | Full (f, _), Some n -> Full (f, n)

At these point all the patterns start to repeat: immutability, no side effects, purity, lambda functions and more


Hopefully this gives you an idea of a typical "functional programming journey".

I encourage you to research more about the features and patterns that I mentioned to understand how you can benefit from these ideas.

Take a look also at Effect Systems: these are the next step πŸš€

See you next πŸ‘‹

Start here.

Every week I dive headfirst into a topic, uncovering every hidden nook and shadow, to deliver you the most interesting insights

Not convinced? Well, let me tell you more about it