Max Rosenbaum

Max Rosenbaum

Software developer

Wednesday, 27 October 2021

A review of "Functional Programming in Scala"

Functional Programming in Scala

Do you hate your sanity? Me too!

Come down and checkout Fu̴n̶ctiona̸l̸ ̶ Programming in Scala! Just listen to some of the reviews!

All of my pull requests get rejected now

★★★★★

Nobody likes me anymore and my wife made me sleep on the couch

★★★★★

It inspired me to quit software development and become a goat farmer. Thank you, "Functional Programming in Scala"!

★★★★★

Production doesn't crash as much and I don't look like a hero when I fix it anymore.

★☆☆☆☆

We got Mono̴̲͒i̴͜͝d̴̨͘ś̸̰! We got M̷on̸a̶d̶s̸! And if those were too easy for you, we have ḛ̸̋ͅn̸̥͒d̶̢̲̒ö̶̮̳́F̶̱͆ù̵̥n̶̛̗c̵̥͘t̴̖̔o̵̼̕r̷̥̎s̴͉̈́ ̴̦͆and ̵͇̈́c̴͚͌à̸͕t̸̲͝é̵̫c̵̞̀ȯ̶̧r̷̤̿ỷ̷̮ ̴͚́t̶̜͌ȟ̸ͅe̴͍̊ȯ̴̗ȑ̷̝y̵̖͝!̶̧̍!

Did you hate maths in high school and figure that software development might be a cheeky escape from the fundamental underpinnings of our entire universe? I sure did! Now look at me, my functions are p̵̦̯̥̖͙̮͆̄͛͘͠͠͠ͅu̷͇͛̈́͐̈́̃́͘r̴̡̨̨̜̯͓̘͂̓̚e̸͓̿̾̀̈́ and have mathematical representations! People think that f̵̠̋u̶̙̐ṉ̶̑c̷̺̀t̷͍͗i̶͍͒o̶̠͊n̷̻̅ą̵̿l̵͙̉ ̶̠̓p̷̮͠r̵͚̅ŏ̴͚g̵̈́͜r̴̮͌ȃ̵̪m̴̜̍m̶̢͛î̷͉n̵͍̎g̴̰̕ ̴̥͛ is really hard. Well I can tell you that it isn't. Check out this super easy to understand block of code that will magically handle everything;

class Monad m where
  (>>=)  :: m a -> (  a -> m b) -> m b
  (>>)   :: m a ->  m b         -> m b
  return ::   a                 -> m a

Too easy? I know right! Let's check out how we can multiply a number by ten using a F̶̱͆ù̵̥n̶̛̗c̵̥͘t̴̖̔o̵̼̕r̷̥̎

def do10x[C[_]](container: C[Int])(usingfunctor: Functor[C]) = functor.map(container)(_ * 10)

Using these easy to remember and well named patterns, we are now able to c̵͕̠͑l̵̘̆ḛ̷̛̋a̴̘͕̅́n̵̮̮̊̓ ̴̺́͑ţ̵̯̅ḫ̸͛e̸̩̱͋͝ ̵̨͊ͅi̶̝̲͠m̶͔̀͜p̸͇͑͘ừ̸͈̰r̴̼̹͠ĩ̶̟t̸̹̮̿ỳ̴̱ from our code bases. S̸̢̖͍̜͈͓̲̮͔̗̔̃̾́̽̐͆̕͜͝͠͝t̷͙͗o̶̘̚p̶̩͛ ̷̠̿ẗ̷͙́h̵̰̀e̴͚̿ ̴̹̃s̶̼͊i̶̦̍d̴̝̎ĕ̸̙ ̴̘̇e̷̙͋f̶̤̓f̷̬̉e̴̡̽ĉ̶̗t̵̞͂s̶̜͋. e̷̹̒m̵̤̏b̸̢̀r̶͔͂a̵̟̿c̶̗͛e̶̫̒ ̷̫̐t̶̠͑h̸̢̕e̴̺͝ ̸͈̿p̸̥̋u̵̠͠r̸͕̾ȉ̷̹ț̸͝ỵ̵̐ ̸̗͑ǫ̸̋f̷̳̾ ̶̮̀t̷̘̄ȟ̴͍e̷͉͛ ̷̙̊f̶̱̒û̶͙n̶̨̏c̶͕͂t̴̛̘i̵̞͒o̶̖̿ń̸̫a̸̞͛l̵̕ͅ ̶͈̎v̴̬̔a̷͎͠l̷̤̐h̶͖̀a̴̛̜l̴͕͌l̵̗̈a̸̗̓. J̴o̴i̷n̷ ̴m̶e̵,̷ ̶b̴r̶e̸t̷h̴r̷e̸n̷,̶ ̷ t̴̛̝̣̠̫̹̮͚̪̼̥͗̾̈́̆ö̵͎̓̀̀̃g̵̨̺̟̥͚̻̺̅̈́́̈́̇́̈́́͂͂͛̎͗̒͘͜e̷̡̼̤̼̥͉̟̗̩̜͙͍̓́̇̒̅͋̒̋͒̀͒ͅț̵̻͈͓̯̞̘̪̀͂̂͒̕͘͝͝͠h̵̡̡̛̛͚̙̲̣̥̟̪̬̒͌͆͒͋̉̐̀̔ẽ̴̡̢̧͖̱͙̳͙̟̼̗̗̯̦̗̼̄̇́̈́̐̎̌̀͘͘͝ȓ̶̡̜̺̫̗̠͍̩̰͈̖̺̰͍̎͝ ̴̛̲͙̖̘̬̜̫̆̌̾̃͐̆̃̒̑̚͠w̶̮̠̍̒̏͗͊̄̓̓͐̊̄̕͝͠͝é̶̛̜͙͎̀̚ ̴̢̨̣̹̀̔̔̇̌̋̆͘ẅ̷͖̯̯̺̘̩͍̥̫̟́̉̔͐̅̈́̊͋͂̔͑͐i̸̡̨̝̝͔̬͙̟̯̙͍̪͌́͜͜l̸̨̤̳̳̭̼͇̪̱̹͊̄̀̎̂̏̈͐̉̕͘̚͜͝l̷̛͇̪̲̈̊̆̓̈́̃͐́̂͌͘͝͝ ̸̧̨̱̳̜̬̦͚̩̰͆͊̊̏̈́ḃ̵͓̻̹̺̱̮̘͈̙̲̬̞͔͕̳͜ḛ̷̻̣̮̤͙̣̣̠̹̬̥͉͇͋̑̐͛́́̄̋̂̀͛͒͂͠ ̵͙̠͎̑͐͗̈́͒̚͜͝ą̸̲̻̬̙̫̣͍̰̜̩͊̑͂̈́̽̋͘͜b̶̭̬̖͉̟̱͎̗͆͜l̷̨̨̢̬̲̻̳̣̑́̋̈́͑̐̇̌ę̵̤̬̥̟̺͉̪̜̱͉̤͉̯̂̔̊͋͝ͅ ̷̝̥͈͎͚͇͛̇̓͂͐̿́̒̔͗̈́̎͠͠ṫ̵̨͈͈̹̪͇̦͇̻̳̝̞̹̱͚̊͜o̴̢̼̹̙̹̪͉̙̠̮͉̭̖̿̔͆͆̌̒́͝ͅ ̵̧̡̡̫̼̳̙̈́̅͊͆͑͐̾̉͛̕̚͝͝ȩ̷̛̬̘͚̰͈͖͉͍̗̤̼͙͔͖̮̗̀̔̒͂͋͗͌͑̀͗̀̅̉̚n̴̡̼̝̟͇̻̫̘͇̲̝̮̈́͗̄̇͋͆̎̈́̎̓͂̇͑̃̚͠d̷͇͓̩̫̦̈́ ̶̧̨̰̜͛͐͑̅͛͐́̾͋̿̏͒́̉̐̕͠t̷̞͈͖̤̓̌́̐̀̋͑̚͜͠h̷̜͇͚̼͓͈͓̻̦̼̽͑͊͊́́͌ȩ̴̧̨̛̞͔͙̠͓̣͓̐͑̈́͒̈́̃͝ͅ ̷͈͖̩͔̤̹̼͚͓͒͜į̴̮̻̩̤̠͉͎̪͖̖̬̝̒́͗̈́͜͝m̷͓̯͎͂̋̀p̵̮͈̣̟͉͈̱̝͙̃̄͑e̷̡̨̳͍̪̥̞̭̹̟͙͙̓̀̿̋͋̃̑̏̓̂͠ͅr̶̦̜͉̟̤͍͓̤̘̜͛̋̒͋̒͒̽́͋͘͝͠ä̶͎̟̠́̉͠ẗ̶̢̛́̓̎͒̋͂̈́͒̃́͐̈̆̚͝ḯ̵͖̩͗͑͒́̔̒́̌́̽̌̂v̵̧̨̡̨̢͍͉̪̟̝̩̣̞̥̤̦̈́é̵̡̛̠̭͕̖͈̖̯̺͎̟̪̯̗͚́̀͐̅̑͘ ̸̡̟̘͉̯̟͙̦̜͙̹͈̠̾͋̅͐̇̃ f̷̺͚̫̞͈͙̗̠̫̝̻̼̜̿̃̂͛̏̄͗̕í̴̧͙̪̼̖̗̠̼̟̲͉̪͍͔̻̎̊̋̃͋̅̇̊̀̀̈̚͘̕͠ͅl̵̢̨̲̰͔̠̘̯̝̬͕̩̬̲͕͚͒͐͆̇̌̆̇̓̈́̋̌̽̓̚͜͠t̷͕͖͎͙̘͆̾̍̀́̒̔̊̑̒̋́̓͝ḧ̸̨̧̡̰̱͎̘͖͉̣́͌̂̍̊̓̉̓͘̚͘͝ͅ ̸̱̞̾̐̑̂͗̀̔̀̍͝t̵͖̮͆̏̄͗̂͝ḥ̷̢̗̪̙̠͚͖̻̜͉͔̫̼͇͑͐ͅa̴͕̰̺̱͍̖̗̍̏͑̊͂̋̉̌̓̄̐̆͆̒̽͋͝ţ̴̩͕̰̗̝̈́́̀̈́̐̌̓͊̿̍̾̌̅̌͝͝ ̴̨̥͖̼͇͎͇̳̺̪̫͈̥͈̝͍̀̍̄̋̎̅͗́̌͑̅̊͘͠͝ͅi̴̻̪̣͎̗̞̣͑n̷̨̧̗̳̻̬͉̹̦̻͉̼̗̉̓ͅͅf̵̨̨͇͔̥̠̹̻͇̮̻̈́̄̃͂ę̴͍͙̋̓̂͂̍̋͆̅̍s̶̫͔͉̣̰̤͔͉͎̣̭͚̰̬̹̈́̽̏̊̉̂́t̵̨̧̰̤̖̲̝̩͚̻̮̝͙̹͈͖͒͜ ̵̧̡̘̻̟̯̫̽̑̏̈́o̵̧̡̫̪̹̪̟̠̳̳̦̹̭̱̠̒̆͆̀̐̉̽͜u̸̢̩̫̣̬̱̞̎͐̈̌̚ͅr̷̼̩͎̜̯̻̭̗̣̮̬̺͛͊̿̎̍̍̈̐͛͐͘̕̚͜ͅ ̸̢̛̹̼͔̥̩͑̐̐͛̃̄͐́̏͋͊̏̂ǵ̷̛͖̗͆̈́̿́̇͆̀̈́̈ǐ̷̧̺̭̱͉̫̦͂͐̌͛́̀̀̾̒́̂̑̃̚͠t̸̨̫̠̞̟͙͓̀̄̉͘ ̶̛̄́͐̉́͛̂̕͜͝͠r̶̢̰̠̪̼̞̞͈̳̰̪̺̥̻̥̥̎̒̄̊̌̏̏̈̕͝ȩ̶̗̥̻̼̣̣̩̦̮̻̩̩̞̃̃͌̃̄̐̌͛̿̈́̓͂͜͠p̴̢̯͔͍̮̮̟̱̭̠͉͇̦̀͋̓ǒ̵̹͎̖̳͇̳̙̞̹̓̎̉̀̓̚̕s̸̖͓̩̼͉̣̹͆̕ ̸̨͓̳͎͇̣̺̗̮̣̥͖̜̥͒͊͌̉̅̉̌̔̆͊͜ͅą̶̰̫̳͇͚̱̙͎̞̺̟̯̾͐͌̄̀̚n̶̢̘̺̠͓̝͍̰͑̄͐͋̊̈̾̇̂ď̵̮̊̄ ̶̣̩̘̣̉̃̀̈́͗̈̍̿͛̎̕͜C̶̢͎̯͔̣̬͋̈̓͑͒͆̑̐̀R̸̨̺͔̝̭͓͙͇̥͂͊̋͆̐̌̿̈́̒̊̎͒͠U̵̡̨͈̜̟̟̟̜̞̮̣͕̻̽̉̍̔͆͒͜S̶̝͕͖͒̽̃͂̿̊͜A̴̢̩̺͙̤̦̜͖͉̯͈̟̭̼̘͐̅̓̋̃̈́̃̌͑̋̕͝Ḑ̸̧̖̗̝̪̳̮̠̤̘̘̘̤̄̾͐̽͌̄̓̉͗͐͠E̴͚̙̳͇̹͈͖̺̱̭͋́͂͊̒̆͒̓̾̌̓̋͆ ̷̧͚̥̼͙̽̀͆͘Ȯ̸̰͇͖̫̬̩͚͈̱̟̘̱̣̂N̵̨̤̻̪̥̟͓͎̈́̀͌̽̀͗̽̑̐̉̈̄͘͘͝͝͠W̴̡̡̡͓͙̻̱̰̯͚̭̗̪̟̥̾̎̊̐̋̌͐͐͊̒͊̚̕͠Ḁ̷̪̺̜͎̘̪̝̞͖͍̞̙̗̀̾̃̊R̴̢̛̛̈͊͑͌̂̐̈́͐̊̆͂̊͠Ḋ̵̡̨̦̳̐̂̇̈̒S̷̝̫͈̖̗͉̠̠͓̤̽͗͆̃͊́͗̓̉́͐̊̂͑ͅ! ̵̱͚̯͎̰̹̮͕͍̳͚̭̙̣̱̔́́̊̐̍͊̀̆̋̌̈́͐͜͜ ̸͓̎̊W̸̭͎̮̰̗̻̹̯̘̫̬̭̦̰̪̠͠Ę̸̛̩̗̳͖̔̂̔̍̒͋̔̐́͐ ̷̧̘͕͖̞̹̜̭̺̌̇̋̒̑́̎̆́̃̓̕͜͝W̶̛̛̦̔̐̓͆͠͝I̴͕̞͓̾̏͒L̵̩̺̬̠̈́̈́̈͊̽L̵̛̮͖̯̺̬̽͒̊̎͗́̃̓̍̈̇͆͛̇̕̚ ̴̢̯̣̜̟̖̤̣̤̼̯͔̦̣̎͌̆̀̓̆͘̚B̷̝̠͕̯̞̟̏͑Ẹ̸̥̆͝ ̸̟͕̖͚̋͒̿̍̈́̾̌̃̍̄͘Š̶̩̼͙͑̓̿̎͜A̶͈̓̆̔̋̅͒̀̓͐̀̿͑̽V̵̱̳̥̜̠̠̈́͐͋̋͂͆̑̔̽̄̈́͂͘͠E̷̡͇̬̺̤͉͓̪̺̖̙͉̼̓̃̀̃̄D̷͈͉̯̲̜̉̈́!̸̧̺̲̰͚͓̒̆̔̅́̅͝! ̷̲̘̫̎̿͛̓̒̇͛̏̈́̑̃͝!̶̲̣̝͚̥̱̤̊̿̆͛̇̂͜ͅ!̴̝͎͙̩̠͚̰̌̊͜!̴̨̧͕͇͇͖̺̙̠͉̭̬̲̫̟̈́̈́ͅ!̵̢̞͇̝̤̲͔̫͍͇͍̜̜͆͆̐̈͘͘ͅ!̸̼̍͑̊͒̈́͠

</memes>

<seriousBookReviewTime>

"Functional Programming in Scala" is a really really hard book to get though, I would honestly place this in the category of university text books. A couple of amazon reviews put it perfectly;

... The target is intermediate and advanced. The text for the first 30 pages is simple and easy to follow, but then the accelerator pedal is pushed deep into the carpet. ...

and

... However if you are new to Scala and FP, like I was when I started reading this book, you will probably also find the concepts weird and hard to understand and relate to ...

This isn't a book for beginners, and you're going to need some strong motivation to white-knuckle through the properly laborious parts (especially where things go into the math). In fact, functional programming has taken years for me to even remotely wrap my head around. I can only really compare it to learning programming for the first time or learning object orientated programming for the first time. I had about three years of experience with scala before I even looked at this book and even then it was a slog.

If you're new to scala I wouldn't recommend this book. If you're new to functional programming I wouldn't recommend this book. If you're experienced with something like typescript or erlang, even then, this book is a bit of a tough sell given how strict scala is. If you find yourself in any of those categories and want to learn functional programming I'd recommend a softer path in (like the youtube channel I recommend below!). If you're keen on scala check out something like "Learning Scala" or "Scala cookbook".

If you're experienced with haskell, F# or scala, this book is perfect to re-enforce fundamentals and take your functional program design to next level. If you're comfortable with this book (I'm not lol) I'd wager you'd be comfortable writing a functional framework like Cats or ZIO. This book is really to make that final step to being a truly advanced software developer (not me lol2).

Functional programming takes a step back from programming as a whole and re-invents language design through the lens of category theory and mathematical proofs. The mathematical jargon makes functional programming really dense and scary, and that is probably what those amazon reviewers are having trouble with. I sure did.

Learning functional programming absolutely sucked. And you know what? I don't want to write software any other way.

Without getting too preachy, I honestly believe that functional programming is the way to write software as you must have a strong and deep reasoning about your code which consequently gets embedded directly into your code through (mainly) the type system. Here is an example of how we can add together the result of multiple threads:

  val r = scala.util.Random
  val thread1: Future[Int] = Future {
    // Sleep for a random amount of milliseconds
    Thread.sleep(r.nextInt(5000))
    // This is shorthand for `return 100`
    100
  }
  val thread2: Future[Int] = Future {
    Thread.sleep(r.nextInt(5000))
    150
  }
  val thread3: Future[Int] = Future {
    Thread.sleep(r.nextInt(5000))
    300
  }

  for {
    a <- thread1
    b <- thread2
    c <- thread3
  } yield {
    // this will equal 550
    a + b + c
  }

I know this is a super trivial example, but we are able to compose the results of multiple threads with complete type safety and compiler support. Now think about scenarios where you've had to compose the result of multiple APIs with blocking IO. You can encode the results of a user auth process, a database result and combine a cache race scenario all in a single combinator (the for comprehension). The greatest strength of functional programming is the ability to reason about crazy complicated things (like threads) in the type system and magically add the results together. These kinds of abstractions just, kind of don't exist in Scalas OOP counterpart java.

If you're genuinely keen to learn some functional programming (and in scala!) I would highly recommend this small youtube channel:

Rock the JVM

He works through all the extremely complicated concepts like monads, functors and implicit scopes in a super straight forward and easy-to-understand manner. It helped break the back of those hard concepts.

In summary, "Functional Programming in Scala" is probably one of the hardest books I've worked through, but I feel like a much better software developer for it. This will be a book I revisit in 12 months time to see what really stuck and to continue to sharpen the axe. Maybe even get good at functional programming.

picture of the author
Tuesday, 30 November 2021

Plugin architecture

The Swiss army knife of architectures

medium software-architecture python

Continue reading >>