Tải bản đầy đủ - 0 (trang)
2 Monoids, Functors, and Monads

2 Monoids, Functors, and Monads

Tải bản đầy đủ - 0trang

5.2 Monoids, Functors, and Monads


that a monoid is based on an associative function. However, if we define a trait for

monoids we can require that there is a function for the type, but we cannot require

that the function is associative. So, the satisfaction of this requirement is left to the


5.2.1 Monoids

This name comes from category theory [10], and in such context they correspond to

a type of algebra.

In the context of object-oriented programming and Scala, they are types that include a binary associative function and an identity element for this function. Examples

of monoids include:

Integers, with the sum and zero.

Integers, with the product and one.

Strings, with the concatenation (“+”) and the empty string.

Lists, with the concatenation of lists (“++”) and the empty list.

Positive numbers, with min and zero.

Monoids are important because given a sequence of the elements of the type, we

can combine them with the function in any order. This is because of the associativity of the function. Therefore, we can use the higher-order functions foldLeft,

foldRight, fold, and aggregate1 with any monoid. The result will be always

the same.

5.2.2 Functors

We have seen in previous sections that one may define functions and classes that

are parametric with respect to one or more types. This type of generalization can be

pushed even forward.

Let us consider types as List[Int], Array[String], and Something[A]

all containing different elements of a basic type (i.e., Int, String, and A) and implementing the function map. The function map given a function from the basic type

to another (i.e., f: A=>B) transforms a Something[A] into Something[B].

We say that such type is a functor.

When applying functors from a set A to a set B, we expect that if on the set A

there is an identity i A and a function f A : (A, A) → A, and if on the set B there is

an identity i B and a function f B : (B, B) → B, then

1 We

will see fold and aggregate in Chap. 8.


5 Types and Classes Revisited: Polymorphism

• f (i A ) = i B ,

• f ( f A (a, b)) = f B ( f (a), f (b)) for all a in A and b in B.

We give an example of the definition of functor in terms of a trait, and two examples

of objects satisfying the requirement of this trait.

trait Functor[GenericTypeOf[_]] {

def map[A,B](gtype: GenericTypeOf[A])(f: A=>B): GenericTypeOf[B]


object ListFunctor extends Functor[List] {

def map[A,B](list: List[A])(f: A=>B): List[B] = list.map(f)


object SetFunctor extends Functor[Set] {

def map[A,B](s: Set[A])(f: A=>B): Set[B] = s.map(f)


Then, naturally, we can apply SetFunctor.map as follows (the map

fromStringToNum was defined in Sect. 2.9.2).

val fromStringToNum:Map[String,Int]= Map(



We can also extend Functor with new methods defined from the map. See e.g.

the following

trait FunctorExtension[GenericTypeOf[_]] extends

Functor[GenericTypeOf] {

def composeMaps[A,B,C](gtype: GenericTypeOf[A])

(fA: A=>C)(fC: C=>B): GenericTypeOf[B] = {




This can then be used by any object or class that extends FunctorExtension

once we define map. See e.g. the following example

object ListFunctorExtension extends FunctorExtension[List] {

def map[A,B](list: List[A])(f: A=>B): List[B] = list.map(f)


Which can be applied as follows.




5.2.3 Monads

A monad is another type that is useful in functional programming. The term also

comes from category theory. Monads can be seen as an abstraction of the following


5.2 Monoids, Functors, and Monads


Let us consider a list of integers, its decomposition into prime numbers, and then

the list of all primes. We will have something like:

List(2,10,15,45) => List(List(2),List(2,5),List(3,5),List(3,3,5))

=> List(2,2,5,3,5,3,3,5)

We can see this computation in terms of a function f that given an integer returns

the list of factors, and then a function flatten that given a list of lists of elements

returns just a list of elements. If we generalize these types we have that we can use

any type M[A] instead of the original list of integers, and the output can be any type

M[B]. We use two types A and B as the function f could transform the type of the


A monad is a generalization of this process considering the type M[A] (in our case

a List[Int]), a function f from A to M[B] (in our case from Int to List[Int],

and then the function flatten that given a M[M[B]] returns a M[B].

A monad can be seen as a combination of a functor (we have M[A] with a map

that permits to apply f to each element in M[A] and obtain M[M[B]]) and a monoid

(that permits to flatten M[M[B]] into M[B] by means of the associative operator:

e.g., concatenation in the case of lists).

In practice, monads are not defined in these terms but it is customary to define

them in terms of the following two functions.

• unit. A function that given an element of type A returns an element of M[A].

• flatMap. A higher-order function that given a M[A] and a function from A to

M[B] returns the data transformed into M[B]. That is,

flatMap: M[A] => (A => M[B]) => M[B]

The function flatMap is also known by bind. Scala has this function implemented. We could just apply


if we have decomposeNumberInPrimes to decompose a number into its factors.

Monads are expected to satisfy a few properties. They are known as the Kleisli

laws. For example that flatMap of m with a function (x)=>unit(x) returns the

same m. However, we cannot force these properties into Scala.

Chapter 6

Scala: OOL and FP

In this Chapter we discuss a few issues related to the interaction between objectoriented aspects in Scala and functional programming ones. We also discuss some

aspects related to efficiency in computation.

6.1 Tail-Recursive Functions

A recursive function is tail-recursive when the last action done by the function is a

call to itself. Let us recall the recursive definition of the factorial.

val fact:(Int=>Int) = (n:Int) => {

if (n==0) {1} else {n*fact(n-1)}}

This example is not tail-recursive because when n = 0, the function calls itself,

but after doing so and obtaining the corresponding result it multiplies this result by

n. We give below an alternative that is tail-recursive.

The solution computes the factorial by means of a tail-recursive auxiliary function.

Let us focus on the auxiliary function. We call it facttr. The auxiliary function has

two parameters. One that accumulates partial results. We call this parameter acc.

We will proceed multiplying n by n − 1, by n − 2 and so on.

The other parameter is n1. It indicates what is still missing in the computation.

We have that acc accumulates the products from n1 + 1 to n and what is missing

corresponds to the factorial of n1. In other words,


acc =



and, thus, for any n1 the following holds (this is an invariant of the function as it

holds for any n1)

n! = acc ∗ n1!.


© Springer International Publishing AG 2016

V. Torra, Scala: From a Functional Programming Perspective, LNCS 9980

DOI: 10.1007/978-3-319-46481-7_6



6 Scala: OOL and FP

Taking this into account, the function facttr(n1,acc) will call recursively

to itself as follows facttr(n1-1,n1*fact). Note that


facttr(n1, acc) = acc ∗ n1! = n1! ∗




= facttr(n1 − 1, n1 ∗ acc) = (n1 − 1)! ∗

i = n!


Loop and recursion invariant A loop invariant is a logical/mathematical expression that is true in each iteration. Similarly, a recursion invariant is a logical/mathematical expression that is true in each

call. Invariants permits us to reason on the correctness of programs.

In addition to the recursive call, the tail recursive function needs a base case. The

base case is when n1 = 0 (this case means that no computation is pending) and in

this case we have that




acc =


i = n!.


Therefore, the base case returns acc.

Writing all together, we have the following definition.

val facttr:((Int,Int)=>Int) = (n1:Int, acc:Int) => {

if (n1==0) { acc } else { facttr (n1-1, n1*acc) }


We can use this function to compute the factorial of any number n. We just need

to call it as facttr(n,1). Nevertheless, in order to avoid any misuse, we define

a function fact that calls the tail recursive function, and make this tail recursive

function local. The complete definition is therefore.

val fact:(Int=>Int) = (n:Int) => {

lazy val facttr:((Int,Int)=>Int) = (n1:Int, acc:Int) => {

if (n1==0) { acc } else { facttr (n1-1, n1*acc) }




Note that in this definition we have added lazy to the local definition facttr.

We need to use lazy otherwise the interpreter gives us an error as the function is local

and recursive.

The importance of tail-recursive functions is that they can be optimized very

easily replacing recursion by a loop. That is, the compiler does not need to allocate

6.1 Tail-Recursive Functions


space for the stack (for the variables involved in the call). See e.g. [15] for a detailed

discussion on tail-recursion.

6.1.1 Some Scala Technicalities

We can inform Scala that a function is tail-recursive using an annotation. We should

use @annotation.tailrec before the function. Then, the compiler issues an

error if Scala cannot transform your function using a loop.

However, if we add @annotation.tailrec in the definition above, Scala

gives us an error because “lazy vals are not tailcall transformed”. At the same time,

as stated above, without a lazy eval, the local definition does work.

A way to solve this problem is that our local definition is a method instead of a

function. The definition follows.

val fact:(Int=>Int) = (n:Int) => {


def facttr (n1: Int, acc:Int):Int = {

if (n1==0) { acc } else { facttr (n1-1, n1*acc) }




If you want to know execution times to compare implementations, you can use

the function System.nanoTime and define e.g. function executionTime (that

returns the execution time of a function f in seconds) and meanET (i.e., mean

execution time of n executions of a function f) as follows.

def executionTime[A](f: => A) = {

val s = System.nanoTime

val ret = f

val et = (System.nanoTime-s)/1e6



def meanET[A](n:Int, f: => A) = {

(((1 to n).map((i)=>executionTime(f)._2)).



With these functions we can compare different alternative definitions for the factorial. For this comparison we use BigInt so that we can compute larger factorials.

The first one is with def. The second and third are with val and recursive (not

tail-recursive). Difference is in the brackets. The last one uses tail recursion.


6 Scala: OOL and FP

def factBId (n:BigInt): BigInt =

if (n==0) 1 else n*factBId(n-1)

val factBIv: (BigInt => BigInt) =

(n) => if (n==0) 1 else n*factBIv(n-1)

val factBIvc:(BigInt => BigInt) =

(n) => { if (n==0) 1 else n*factBIvc(n-1) }

val factBItr:(BigInt=>BigInt) = (n:BigInt) => {


def facttr (n1: BigInt, acc:BigInt):BigInt = {

if (n1==0) { acc } else { facttr (n1-1, n1*acc) }




We can obtain their average execution time of 1000 executions for the factorial

of 2000 using the following code.





6.1.2 Additional Examples of Tail-Recursive Functions

The following definition gives an implementation of the greatest common divisor

(gcd) using Euclid’s algorithm, which is recursive. As you can see, this definition is

tail-recursive because the last actions done in the call is the recursive call. You can

test the function calling e.g. gcd(10,20).

val gcd:((Int, Int)=>Int) = (a,b) => {

if (a == b) { a }

else { if (a > b) { gcd(a-b, b) }

else { gcd(a, b-a) }


Let us consider again the Fibonacci series (see Exercise 2.2 and Sect. 3.6). Recall

that F0 = 0, F1 = 1 and that Fi = Fi−1 + Fi−2 . However, instead of using a recursive

definition using this last expression (which results into a rather inefficient and not

tail-recursive implementation – Exercise 2.2), we will give a tail-recursive version.

To do so, we will use an auxiliary function that receives two consecutive elements

of the series (f1 and f2 below), in each call if we have not yet reached the desired

one, we build a new one and discard the smallest one.

You can see that this function is tail-recursive because the last action in the function

is the recursive call. You can test this function using e.g. fib(6).

6.1 Tail-Recursive Functions


val fib:(Int=>Int) = (n) => {

def fibtr (n:Int, f1:Int, f2:Int):Int = {

if (n==0) { f1 }

else { fibtr (n-1, f2, f1+f2) }}



Exercise 6.1. Compare the execution times of the tail recursive and the straightforward recursive versions of Fibonacci.

6.2 Functions in Scala and Object-Oriented Programming

We have stated that in Scala functions are objects. In fact, functions are a particular

type of objects which have implemented the method apply. The application of

a function to an object corresponds to the execution of the method apply of this


See for example the following definition (ignoring for the moment the meaning of

Function2). This expression creates an object that implements the method apply

that given two integers returns another one.

val sum2 = new Function2[Int,Int,Int] {

def apply(a:Int, b:Int) = a+b


Then, we can call the method apply of this object as follows.


As we have stated, the application of a function corresponds to the application of

the method apply, and that such objects can be understood as a function. Therefore,

sum2 can be used as follows.


Scala documentation describes [24] that anonymous functions are a shorthand of

the creation of a new function following the above example. In particular, it states

that (x: Int) => x + 1 is a shorthand of the following

new Function1[Int, Int] {

def apply(x: Int): Int = x + 1


All objects are instances of a class. In Scala, functions are instances of anonymous classes. In particular, they are instances of anonymous classes which extend FunctionN traits, where N is a number. There are traits Function1,


6 Scala: OOL and FP

Function2, …, Function22. Function1 is for functions with one parameter,

Function2 with two parameters, and so on till functions with 22 parameters.

See e.g. that the following anonymous function leads to an error because it has

too many arguments.
















a04:Int, a05:Int,

a09:Int, a10:Int,

a14:Int, a15:Int,

a19:Int, a20:Int,

=> a01+a022

Therefore, as a summary, if we create an object as an instance of FunctionN

and it has implemented apply, it will be a function and behave like a function. If

we try to create an instance of FunctionN but without a method apply, it will

not work because the trait FunctionN requires that this method is implemented.

E.g., the following code

val sum2 = new Function2[Int,Int,Int] {

def other(a:Int, b:Int) = a+b


gives an error

:9: error: object creation impossible,

since method apply in trait Function2 of type

(v1: Int, v2: Int)Int is not defined

val sum2 = new Function2[Int,Int,Int] {


Alternatively, we can consider just the definition of a class with the method apply.

See e.g.







something {

ourVar = 10

apply (i:Int) = 2*i

aMethod (i:Int) = i*ourVar

changeOurVar (i:Int) = { ourVar = i}

In this case we can have objects that can be called function-like but are just

objects. See e.g. the following code. If we execute this code, the expression

fakeFunction(2) returns 4.

val fakeFunction = new something



6.3 Defining Functions Revisited: val and def


6.3 Defining Functions Revisited: val and def

We have seen that both val and def are for declarations. We have seen that def

permits us to define methods. First, it is important to underline that methods are not

functions. Nevertheless, methods can be used as functions when needed. See, for

example, their use in the higher-order function map below.

def add1 (n: Int) = n+1

(1 to 10).map(add1)

When we use def, the expression assigned with def is executed every time the

definition is invoked. We can show that this is the case with the following definition.

def ffdef = { println("execution"); (x: Int) => x }

This definition has a side-effect. When we make the declaration, the Scala interpreter only returns the type of ffdef but the side-effect is not seen. The string

execution is not printed. Then, every time we apply the method, the expression

is evaluated and this causes that the string is printed on the screen. Observe the


scala> def ffdef = { println("execution"); (x: Int) => x }

ffdef: Int => Int

scala> ffdef(2)


res8: Int = 2

scala> ffdef(2)


res9: Int = 2

val permits us to assign an object to an identifier. As functions are objects

(instance of a particular type of class), we can assign them by means of val. This is

the approach we have followed in this text as it has a functional programming flavor.

Recall that when we assign with val, values cannot be changed. We can use var

instead if we want to change the value.

When an expression is associated by means of val to an identifier, the expression

is evaluated. We need to underline that the expression is only evaluated once, and

this evaluation is at the time we establish the binding. In fact, we already saw this

issue when discussing lazy evaluation (see Sect. 3.2).

Let us define the following, analogous to ffdef above.

val ffval = { println("execution"); (x: Int) => x }

In this case, when we declare ffval, the expression

{ println("execution"); (x: Int) => x }


6 Scala: OOL and FP

is evaluated, which implies that (as a side-effect) execution is printed on the

screen, and then the object function (x: Int) => x is associated to ffval.

When we apply this function, there are no (more) side-effects, as the function solely

consists of (x: Int) => x. Observe the following execution, and compare it

with the one above with def.

scala> val ffval = { println("execution"); (x: Int) => x


ffval: Int => Int =


scala> ffval(2)

res10: Int = 2

scala> ffval(2)

res11: Int = 2

6.4 Data Types and Efficiency

We have seen in Sect. 2.9 that Scala implements both mutable and immutable classes.

In particular, we have seen lists as an example of immutable objects and arrays as an

example of mutable objects.

Fig. 6.1 Array(1,2,3,4)

assigned to variables x and y

(top) and the same variables

after executing x(1)=20














First, let us recall that it is common to implement1 lists by means of linked cells.

Each cell contains an element and points to the next cell in the list. In the previous

example, x will point to the first cell of the list (the one that contains the number 1).

In contrast, arrays are usually implemented by means of contiguous positions of

memory. We provide an illustration for the examples for both lists and arrays.

1 Details on the implementation of lists are outside of the scope of this course. It is explained in books

related to data structures (e.g. [1] and [4]). Look for topics on linked lists and linked structures.

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

2 Monoids, Functors, and Monads

Tải bản đầy đủ ngay(0 tr)