Yesterday my mate asked me: “I have a List[String]
and a Map[String, Int]
and I want
a List[Int]
where its values are those of the Map
whose keys match the List[String]
elements,
maintaining the order. Should I use pattern matching?”. I know, the sentence is a bit convoluted but the
code will make it clear, hopefully. Anyway, I replied: “No, you don’t need pattern matching, you just need this”:
1 2 3 4 5 6 7 8 

Hold on, how does it work? If you look at the definition of the collect
method you’ll see it accepts a
PartialFunction
, instead I passed a Map
to it.
Well, it turns out that Map
is a PartialFunction
.
Since this peculiarity surprised him I decided to write a small post showing how Scala’s Map
,
List
(actually Seq
) and Set
can be viewed as functions.
Before starting: functions vs partial functions
In short, a function is a mapping A => B
that relates each value of type A
to a
value of type B
–modulo bottom. A
and B
are called domain and codomain, respectively. If you’re not a math
addict, roughly speaking, the domain is the set of all values that you may provide as input to your function,
while the codomain is the result of the function application to the input, that is your function output.
On the other hand a partial function from A
to B
is not defined for some inputs of type A
. E.g.:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 

Note that the PartialFunction
definition is the following:
1


That is a PartialFunction
is a Function
that will just throw for those inputs the partial function is not
defined at. So you can use a PartialFunction
wherever a Function
is expected. Just keep in mind you’ll get
an exception for some input values.
Seq[A] as PartialFunction[Int, A]
Being List
an indirect subclass of collection.Seq
and given that the latter has the following definition, you
can see clearly that every Seq[A]
is also a PartialFunction[Int, A]
:
1


Here’s an example:
1 2 3 4 5 6 7 8 

Of course I could have used xs
directly without the assignment to f1
:
1 2 

I assigned the list to f1
just to emphasise the fact that it’s a partial function. It corresponds to the
indexbased lookup.
Performance concern: Take into account that the indexbased lookup on List
has a cost of O(n)
. For this type
of access you may consider using a Vector
which has constanttime access cost. Anyway this post is not about
performance concerns about the collection API so I won’t dig into this topic.
Map[A, B] as PartialFunction[A, B]
If you look at the Map
definition you’ll see that it extends MapLike
which, in turn, extends PartialFunction
.
So you can use it as follows:
1 2 3 4 5 6 7 8 

Set[A] as A => Boolean
Here’s the definition of Set
:
1


It, evidently, extends A => Boolean
which, as you probably already know, is just syntactic sugar for the more
verbose Function[A, Boolean]
. Example:
1 2 3 4 5 6 7 8 

So, for instance, you can use a set to filter a list:
1 2 3 4 5 6 7 8 

Conclusions
As a final consideration take into account that Seq
s and Map
s are partial functions while Set
is a
function. Partial functions could introduce insidious bugs.
For instance, consider the very first example of this post.
If the Map
hadn’t contained all the elements of the List
and I had used
the map
method instead of collect
I would have introduced a bug:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 

This is because map
accepts a function and providing a partial function instead you get the exception for not valid
inputs as I said in the functions vs partial functions section.
From now on, whenever you have a collection hanging around, consider looking at it as a function. This could help to solve your problem without using pattern matching or other boilerplate.