# functors, applicatives and monads in scala

Post on 07-Jan-2017

1.597 views

Category:

## Software

Embed Size (px)

TRANSCRIPT

Pallavi SinghSoftware ConsultantKnoldus Software LLP

Objectives:

Functors

Applicatives

Demo

Problem :

We have a simple value

We apply a function to it

Lets extend it , the value can be in a context,Now when we apply a functionto this value,we get results depending on the context.

FunctorsWhen a value is wrapped in a context, you cant apply a normal function to the value.We need a map. The map knows how to apply functions to values that are wrapped in a context.

Functors

Definition : Functor is a type class. A Functor is any data type that defines how map applies to it.

Speaking in-formally you apply a function to a wrapped value using map. The map knows how to apply the function.

Functors

We have a Constructor C[_] and two types A and B,we want to apply functions of type C[A]=>C[B], so we need adequate transformations

( A=>B ) => ( C[A]=>C[B] )

And we need to define a map

def map[A,B](A=>B):(F[A]=>F[B] )

Functors

Example: Options , Streams

Object OptionFunctor extends Functor[Option] { def map[A, B](f: A B): Option[A] Option[B] = option option map f }

Problem :Given a function

what happens when you apply a function to another function , we have another function.

Functions are functors too ! A map on a function is function composition.

Problem :

We have a value wrapped in context

And functions are wrapped in a context too!

ApplicativesWhen a value and function both are wrapped in a context. We cant apply it as we apply a simple function. We need an apply. It knows how to apply a function wrapped in a context to a value wrapped in a context.

56rt67

Applicatives

Def : Applicative is typeclass. Applicative is any data type that defines how apply applies to it.

Apply takes a functor that has a function in it and another functor and extracts that function from the first functor and then maps it over the second one.

Speaking in-formally you apply a function wrapped in context to a value wrapped in context.

Applicatives

We have a Constructor C[_] and two types A and B, we want to apply functions of type C[A]=>C[B],so we need adequate transformations

(C[A=>B] ) => ( C[A]=>C[B] )

And we need to define a apply

def apply[A,B](F[A=>B]):(F[A]=>F[B] )

Problem :Given a function

what happens if we feed it awrapped value?

Monads apply a function that returns a wrapped value to a wrapped value.

Definition: Monad is a type class. A monad is a data type that implements the flatMap.

Speaking in-formally you apply a function that returns a wrapped value, to a wrapped value.

We have a Constructor C[_] and two types A and B,we want to apply functions of type C[A]=>C[B],so we need adequate transformations

( A=>C[B] ) => ( C[A]=>C[B] )

And we need to define a flatMap

def flatMap[A,B](A=>F[B]):(F[A]=>F[B] )

Example: List , Set , Option and Future all are Monads

Future is a wrapper over some asynchronous operation. Once the future has been completed you can do whatever it is you need to do with its result.

Difference b/w Monad and Monoids ?

Monoid : Given a type T, a binary operation Op:(T,T)=>T and instance Zero:T then the triple(T, Op , Zero) is called a Monoid if it has the following properties: Neutral Element and Associativity.

Monad instance simply wraps the value of type A within the given context and expose a certain set of methods to operate on them,

Monoid instance already knows how to combine these values of type A within the given context.

Applicatives and monads both model running computations in sequence. But Monads are more powerful because with applicatives you can sequence the computations, but monads allow you to sequence computations with the additional property that the result of subsequent computations can depend on the result of previous computation.

Demo

Questions

References