Connecting...

W1siziisimnvbxbpbgvkx3rozw1lx2fzc2v0cy9zawduawz5lxrly2hub2xvz3kvanbnl2jhbm5lci1kzwzhdwx0lmpwzyjdxq

Scala Real life matters: Starting with Functors, Monads, The Red Book and Cats by Maarten Koopmans

W1siziisijiwmtgvmtavmjivmtqvmjuvntivote1l2jvb2tzlwjvb2stcgfnzxmtcmvhzc1saxrlcmf0dxjllte1otg2ni5qcgvnil0swyjwiiwidgh1bwiilci5mdb4otawxhuwmdnlil1d

We have an exclusive article for you written by Maarten Koopmans! This an amazing article will give you an introduction to Functors, Monads and much more on Scala, check it out below and stay posted for more of Maarten's great blogs. 

 

'This post will give an introduction to Functors, Monads, the famous Red book “Functional programming in Scala” and Cats. Introduction because there are so many words you can put in a blog post. Subsequent blog posts will dive deeper (make your requests to Signify as to the subject). But before we start, if this is all a bit overwhelming and you’re new to Scala, head over to https://alvinalexander.com/scala/functional-programming-simplified-book - it’s a nice story of a journey into functional programming by Alvin Alexander and will certainly lay the groundwork for the more advanced things to come.

So, let’s discuss the famous red book and the Cats library first. “The red book” is fantastic, but dense and less accessible for beginners. If you read it you’ll learn how to write Monads and more, use those abstractions. I love the book, but it’s not an easy read because it builds on all of the exercises. So prepare yourself for some serious programming time while studying this. That said, it’s an excellent resource.

Onto Cats (cats effects is also a great library, and subject for another post) – Cats is a very nice library, and the Cats project has a very nice eBook “Scala with Cats” (PDF). You can pay whatever you want to for it, so the price is from free to… whatever you want to spend to support it. I recommend spending at least $10 – easily your money’s worth and you’ll help support them. The Scala with Cats book is, in my opinion, the most accessible book as they walk you through theory and how to use  cats with exercises, provide case studies and then a separate part with answers to the exercises that really explain the what, why and how

Onto Functors: I’m going to keep it very simple: any type that has a map function defined and preferably an “identity function is a Functor. I’ll leave the deeper explanation to one of the books – but there you have it: Option, List, Future – all Functors. So what about Monads? Well, if the type also has a flatMap function defined and plays by some rules (again the books – they explain it a better and more concise) – you have a Monad you’re using! flatMap is basically used to chain Monads together, and the “flat” reflects the fact that you don’t end up with return types like 'List[Lists[List[Int]]]'. In fact, flatMap can often be implemented on a type using map and flatten, which gives you a better “Gestalt” of what it does.

 

So what does this looks like? A simple example:

def g(x: Int) => List(x+1, x+2, x+3)

List(1,2,3).map(value => g(value)) == List(List(2, 3, 4), List(3, 4, 5), List(4, 5, 6), List(5, 6, 7))

Note the nested lists, and compare this with:
List(1,2,3).flatMap(value => g(value)) ==  List(2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7)

 

Note how the nesting of the lists has gone, i.e. the intermediate list of lists from the first example has been flattened. Writing a lot of maps and flatMaps on a type can make your code hard to read, but luckily Scala has some syntactic sugar using the for….yield comprehension. Given the amount of space I have, I won’t give an example, but any of the books or a Google search will show it to you right away.

A lot of the Scala standard library are monads and/or Functors and I encourage you to explore their presence – basically the collections library, Futures, the Try Monad, Optop, Either, and so on. On top of that Cats provides you with a bunch of other very useful monads or improves on the “default” ones coming with standard Scala.

A final word: in my opinion Monads are often best used in your code, not written yourself. Why? Writing a Monadic type is fun, but often hard to read and understand for fellow Scala programmers that need to jump into a codebase, or worse, maintain it a year after you've left. With the Scala standard library and libraries such as Cats most of your bases will be covered if you use them – and you get well documented, clean, pure FP constructs in return.

Coming up next in the series: more useful Monads from Cats, like State, StateT,  etc and then a post with an intro to cats-effects. Finally, we’ll spend a few posts on akka and akka-http as well.

 

Maarten Koopmans has been working in IT for more than 20 years, and the last 10 years as contractor/freelancer. If you would like to speak to Maarten about opportunities please contact our Senior Consultant Rachael Hedgecock as he is available from 1st November. 

 

Check out Maarten's last article written for Signify here.