/**
* Scala developer @Ebiznext,
* SNUG co-organizer.
* Exalted about FP and maintaining
* A list of great ressources !
*/
val me = SoftwareDeveloper(
firstname = "Martin",
name = "Menestret",
email = "here",
twitter = "@mmenestret"
)
by Myself
I will try to group here, in an anatomy atlas, basic notions of functional programming that I find myself explaining often lately into a series of articles.
The idea here is to have a place to point people needing explanations and to increase my own understanding of these subjects by trying to explain them the best I can. I’ll try to focus more on making the reader feel an intuition, a feeling about the concepts rather than on the perfect, strict correctness of my explanations.
In this article, I’ll try to give you an intuition about what is a functor and what do they look like in Scala. Then the ones being curious about theory can keep on reading because we’ll take a quick glance at category theory and what are functors in category theory terms. Then we’ll try to bridge the gap between category theory and pure FP in Scala and finally take a look back at our functors !
There’s a nice answer by Bartosz Milewski on Quora from which I’ll keep some parts:
I like to think of a functor as a generalization of a container. A regular container contains zero or more values of some type. A functor may or may not contain a value or values of some type (…) .
So what can you do with such a container? You might think that, at the minimum, you should be able to retrieve values. But each container has its own interface for accessing values. If you try to specify that interface, you’re Balkanizing containers. You’re splitting them into stacks, queues, smart pointers, futures, etc. So value retrieval is too specific.
It turns out that the most general way of interacting with a container is by modifying its contents using a function.
So, to summarize, a functor is a kind of container that can be mapped over by a function.
But functors have to respect some rules, called functor’s laws…
A quick note about functor / container parallel: the analogy is convenient to get the intuition, but not all functors will not fit into that model, keep it in a corner of you mind so that you’re not taken off guard.
Along the next sections, the examples and code snippets I’ll provide will be in Scala.
We’re going to play with concrete containers of Int
values to try to grasp the concept.
val halve: Int => Float = x => x.toFloat / 2
Here we defined the function from Int
to Float
that we are going to use to map over our containers
Our first guinea pig is Option[Int]
, which is a container of (0 or 1) Int
.
val intOpt: Option[Int] = Some(99)
val mappedResult1: Option[Float] = intOpt.map(halve)
We can see that an Option[Int]
turns into an Option[Float]
, the inner value of the container being modified from Int
to Float
when mapped over with a function from Int
to Float
…
Our second guinea pig is List[Int]
, which is a container of (0 or more) Int
.
val intList: List[Int] = List(1, 2, 3)
val mappedResult2: List[Float] = intList.map(halve)
We can see that an List[Int]
turns into a List[Float]
, the inner values of the container are modified from Int
to Float
when mapped over with a function from Int
to Float
…
Our third is a hand made UselessContainer[Int]
, which is a container of exactly 1 Int
.
final case class UselessContainer[A](innerValue: A)
val intContainer: UselessContainer[Int] = UselessContainer(99)
val mappedResult3: UselessContainer[Float] = intContainer.map(halve)
We can see that an UselessContainer[Int]
turns into an UselessContainer[Float]
, the inner value of the container being modified from Int
to Float
when mapped over with a function from Int
to Float
… (I’ve deliberately hidden an implementation detail here for clarity, I’ll cover it later)
So we can observe that pattern we described earlier:
A functor, let’s call it F[A]
, is a structure containing a value of type A
and which can be mapped over by a function of type A => B
, getting back a functor F[B]
containing a value of type B
.
Functors are usually represented by a type class.
As a reminder, a type class is a group of types that all provide the same abilities (interface), which make them part of the same class (group, “club”) of same abilities providing types (see my article about type classes here).
This is the functor type class implementation:
trait Functor[F[_]]{
def map[A, B](fa: F[A], func: A => B): F[B]
}
F[_]
, our container types)map
function taking a container F[A]
of values of type A
, a function of type A => B
and return a F[B]
, a container of values of type B
: the pattern we just described.A note about type constructors: A type constructor is a type to which you have to supply an other type to get back a new type. You can think of it just as functions that take values to produce values. And that makes sense, since we have to supply to our container type the type of values it will “hold” !
Most used concrete type constructors are
List[_]
,Option[_]
,Either[_,_]
,Map[_, _]
and so on.
To illustrate what it means in your Scala code let’s make our UselessContainer
a functor:
implicit val ucFunctor = new Functor[UselessContainer] {
override def map[A, B](fa: UselessContainer[A],
func: A => B): UselessContainer[B] =
UselessContainer(func(fa.innerValue))
}
Be careful, if you attempt to create your own functor, it is not enough. You have to prove that your functor instance respects the functor’s laws we stated earlier (usually via property based tests), hence that:
uc
of type UselessContainer
:
ucFunctor.map(uc, identity) == uc
uc
of type UselessContainer
and for any two functions f
of type A => B
and g
of type B => C
:
ucFunctor.map(uc, g compose f) == ucFunctor.map(ucFunctor.map(uc, f), g)
However, you can safely use functor instances brought to you by Cats or Scalaz because their implementations lawfulness are tested for you.
(You can find the Cats functor laws here and their tests here. They are tested with discipline.)
Now that you know what a functor is and how it’s implemented in Scala, let’s talk a bit about category theory !
During this article, we only talked about the most widely known kind of functors, the co-variant endofunctors. Don’t mind the complicated name, they are all you need to know to begin having fun in functional programming.
However if you’d like to have a grasp a little bit of theory behind functors, keep on reading.
Functors are structure-preserving mappings between categories.
Category theory is the mathematical field that study how things relate to each others in general and how their relations compose.
A category is composed of:
a
to an object b
and an arrow from the object b
to an object c
, it exists an arrow that goes from a
to c
and the order of composition does not matter (given 3 morphisms that are composable f
, g
, h
then (h
. g
) . f
) == h
. (g
. f
))A
, B
, C
are this category’s objectsf
and g
are its arrows or morphismsg . f
is f
and g
composition since f
goes from A
to B
and g
goes from B
to C
(and it MUST exist to satisfy composition law, since f
and g
exist)1A
, 1B
and 1C
are the identity arrows of A
, B
and C
In the context of purely functional programming in Scala, we can consider that we work in a particular category that we are going to call it S
(I won’t go into theoretical compromises implied by that parallel, but there are some !):
S
objects are Scala’s typesS
morphisms are Scala’s functions
S
objects is the identity functionIndeed, if we consider the object a
(the type A
) and the object b
(the type B
), Scala functions A => B
are morphisms between a
and b
.
Given our morphism from a
to b
, if it exists an object c
(the type C
) and a morphism between b
and c
exists (a function B => C
):
a
to c
which is the composition of the two. And it does ! It is (pseudo code):
g: B => C
and f: A => B
, g compose f
(h compose g) compose f
is the same as h compose (g compose f)
Moreover for every object (every type) it exists an identity morphism, the identity function, which is the type parametric function:
def id[A](a: A) = a
We can now grasp how category theory and purely functional programming can relate !
Now that you know what a category is, and that you know about the category S
we work in when doing functional programming in Scala, re-think about it.
A functor F
being a structure-preserving mapping between two categories means that it maps objects from category A
to objects from category F(A)
(the category which A
is mapped to by the functor F
) and morphisms from A
to morphisms of F(A)
while preserving their relations.
Since we always work with types and with functions between types in Scala, a functor in that context is a mapping from and to the same category, between S
and S
, and that particular kind of functor is called an endofunctor.
Let’s explore how Option
behaves (but we could have replaced Option
by any functor F
):
Objects
Objects in S (types) |
Objects in F(S) |
---|---|
A |
Option[A] |
Int |
Option[Int] |
String |
Option[String] |
So Option
type construtor maps objects (types) in S
to other objects (types) in S
.
Morphisms
Let’s use our previously defined:
def map[A, B](fa: F[A], func: A => B): F[B]
.If we partially apply map
with a function f
of type A => B
like so (pseudo-code): map(_, f)
, then we are left with a new function of type F[A] => F[B]
.
Using map
that way, let’s see how morphisms behave:
Morphisms in S (function between types) |
Morphisms in F(S) |
---|---|
A => A (identity) |
Option[A] => Option[A] |
A => B |
Option[A] => Option[B] |
Int => Float |
Option[Int] => Option[Float] |
String => String |
Option[String] => Option[String] |
So Option
’s map
maps morphisms (functions from type to type) in S
to other morphisms (functions from type to type) in S
.
We won’t go into details but we could have shown how Option
functor respects morphism composition and identity laws.
S
, the category of Scala types, functions and under function composition. The functors we use are then endofunctors (from S
to S
) because they map Scala types and functions between them to other Scala types and functions between themIn programming terms, (endo)functors in Scala allow us to move from origin types (A
, B
, …), to new target types (F[A]
, F[B]
, …) while safely allowing us to re-use the origin functions and their compositions on the target types.
To continue with our Option
example, Option
type constructor “map” our types A
and B
into Option[A]
and Option[B]
types while allowing us to re-use functions of type A => B
thanks to Options
’ map
, turning them into Option[A] => Option[B]
and preserving their compositions.
But that is not over ! Let’s leave abstraction world we all love so much and head back to concrete world.
Concrete functors instances enhance our origin types with new capacities. Indeed, functor instances are concrete data structures with particularities (the one we said we did not care about at the beginning of that article), the abilty to represent empty value for Option
, the ability to suspend an effectful computation for IO
, the ability to hold multiple values for List
and so on !
Ok, so, to sum up, why functors are awesome ? The two main reasons I can think of are:
Option
allow you to bring null
into a concrete value, making code a lot healthier (and functions purer)Either
allow you to bring computation errors into concrete values, making dealing with computation errors a lot healthier (and functions purer)IO
allow you to turn a computations into a values, allowing better compositionality and referential transparencyI hope I made a bit clearer what functors are in the context of category theory and how that translates to pure FP in Scala !
If you want to keep diving deeper, some interesting stuff can be found on my FP resources list and in particular:
To sum up, we saw:
I’ll try to keep that blog post updated. If there are any additions, imprecision or mistakes that I should correct or if you need more explanations, feel free to contact me on Twitter or by mail !
Edit: Thanks Jules Ivanic for the review :).
tags: Scala - Functional programming