introduction to monads in scala (1)

45
Monads Part 1 functional programming with scala

Upload: stasimus

Post on 07-May-2015

8.353 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Introduction to Monads in Scala (1)

Monads

Part 1functional programming with scala

Page 2: Introduction to Monads in Scala (1)

Where r the verbs lost?

Page 3: Introduction to Monads in Scala (1)

Thinking in Use Cases

get the coffee meet with colleagues on the kitchen read emails review social media update the workspace write the code

Page 4: Introduction to Monads in Scala (1)

Thinking in Java

CompanyHolder.getStuffFactoryBuilder().buildCurrentState().find(Rooms.KITCHEN, CoffeMachine.Any).get(0).run(new CoffeeJob( me.getHabitsStore().find("coffe").

mapToJobMachnie(), null));

Page 5: Introduction to Monads in Scala (1)

Thinking in Java

Need an update:

new SVNUpdateJob(myProject).go();

• execute• process• run• start• doIt

Page 6: Introduction to Monads in Scala (1)

Thinking in Java

Check Gmail

MySmartProxyHack.go();

go via Hack or Job – action name isn't important

Page 7: Introduction to Monads in Scala (1)

Q

How many times u were not lucky with fact that do is the keyword?

ProjectManager.do() vs manageSecurityProcessor.do() vs process

Page 8: Introduction to Monads in Scala (1)

Guava from G

Bad library: it makes me feeling smell

Preconditions.checkArgument(...)vs

checkArgument(...)

with static import... but this is a way for nonlazy dudes in Java

Page 9: Introduction to Monads in Scala (1)

Patriots in Java

First action is create, get, find, build, ...?

Use a Spring, Guice!!!

Page 10: Introduction to Monads in Scala (1)

Other ways of thinking

C and C++Python

Page 11: Introduction to Monads in Scala (1)

Evil?

We r smart – look into Python, Ruby, Java Script or Perl – gun meat.

Im smart enough to understand AbstractProxyMediator or NotificationStrategyFactory – they even don't have it!

Page 12: Introduction to Monads in Scala (1)

Monad is …

Page 13: Introduction to Monads in Scala (1)

Monad is

Usually articles that start with words like "monad" and "functor" quickly devolve into soup of Greek letters. That's because both are abstract concepts in a branch of mathematics called category theory and explaining them completely is a mathematical exercise.

JAMES IRY

Page 14: Introduction to Monads in Scala (1)

Stateless paradigm

Page 15: Introduction to Monads in Scala (1)

JEE Session Facade stack trace

Page 16: Introduction to Monads in Scala (1)

Functions …

F1(F2(F3(F4(Xinput))))

Page 17: Introduction to Monads in Scala (1)

Jump to Scala

Few syntax details to understand examples

Page 18: Introduction to Monads in Scala (1)

val, List

val act1 = List ("Leonardo" , "Raphael")

Page 19: Introduction to Monads in Scala (1)

List operations, Nil

val act2 = “April” :: "Donatello" :: Nilval act3 = act1 ::: act2

Page 20: Introduction to Monads in Scala (1)

Var, no sugar

var act4:List[String] = act3act4 = "Michelangelo" :: act4

Page 21: Introduction to Monads in Scala (1)

Function

(x: Int) => x * 2

Page 22: Introduction to Monads in Scala (1)

Function as value

val double = (x: Int) => x * 2double(10)

Page 23: Introduction to Monads in Scala (1)

Sugar

List(1,2,3).foreach ( (x:Int) => println ( x ) )List(1,2,3).foreach ( x => println ( x ) )List(1,2,3).foreach ( println _ )List(1,2,3).foreach ( println )

Page 24: Introduction to Monads in Scala (1)

Yield

val result = for (i <- 1 to 5) yield i

Vector(1, 2, 3, 4, 5)

Page 25: Introduction to Monads in Scala (1)

Trait

trait Manager extends Lead{ private var workSchedule:Schedule = … override def schedule = workSchedule}

class College extends Worker with Manager with Wellpaid with HardToFind

Page 26: Introduction to Monads in Scala (1)

1. Monads are Container Types

Option, List, …

Page 27: Introduction to Monads in Scala (1)

Where is my manager?

Company company = getCompany(); Manager manager = company.findManager();

if (manager != null) { manager.getBonusFor(me); } else { System.out.println("As always..."); } Java

Page 28: Introduction to Monads in Scala (1)

Schrödinger's State public interface Option<T> { public T value();

public boolean hasValue(); }

Company company = getCompany(); Option<Manager> manager = company.findManager();

if (manager.hasValue()) { manager.getBonusFor(me); } else { System.out.println(”Aren't news!"); } Java

Page 29: Introduction to Monads in Scala (1)

Optiontrait Manager { def getBonusFor(id: Long) :Option[Double]}

trait Company { def manager:Manager}

val sum = getCompany().manager. getBonusFor(myId)val bonus = sum match { case Some(bonus) => bonus case None => 0} Sc

ala

Page 30: Introduction to Monads in Scala (1)

0 on default

val myBonus = manager.getOrElse(0)

Scala

Page 31: Introduction to Monads in Scala (1)

0 on default

val myBonus = manager.getOrElse(0)

Scala

Page 32: Introduction to Monads in Scala (1)

If in If

Company company = getCompany(); if (company != null) { Manager manager = company.findManager();

if (manager != null) { double bonus = manager.getBonusFor(me);

... } else { System.out.println("Isn't news!"); } } Java

Page 33: Introduction to Monads in Scala (1)

Or Else

val m = company.getManager.getOrElse(ManagmentFactory.newOne)m.getBonusFor(me).getOrElse(…)

Scala

Page 34: Introduction to Monads in Scala (1)

Option is a monad

for ( c <- getCompany; m <- c.getManagerFor(me); b <- m.getBonusFor(me)) b spend

Scala

Page 35: Introduction to Monads in Scala (1)

2. Monads Support Higher Order Functions

val list = List(1, 2, 3)def neg (elem: Int) = -elemval mapResult = list map neg

List(1,2,3) map {-_}

Scala

Page 36: Introduction to Monads in Scala (1)

Doesn’t change the kind of monad, but may change its parameterized type...

val one = Some(1)val oneString = one map {_.toString}assert (oneString == Some("1"))

Scala

Page 37: Introduction to Monads in Scala (1)

3. Monads are Combinable

val opMan : Option[Manager] = company getManagerdef extractBonus(m:Manager) : Option[Double] = ...val result = opMan map extractBonus

Option[Option[Double]]

Scala

Page 38: Introduction to Monads in Scala (1)

List[List[Int]]]

List[List[List[Int]]]]

Page 39: Introduction to Monads in Scala (1)

Flatten

def flatten[A](outer:Option[Option[A]]) : Option[A] = outer match { case None => None case Some(inner) => inner }

If the outer option is None, then result is None. Otherwise the result is the inner Option.

Scala

Page 40: Introduction to Monads in Scala (1)

Join, Flatten etc

Scala does not require you to write flatten explicitly. But it does require that each monad have a method called flatMap.

class M[A] { private def flatten[B](x:M[M[B]]) : M[B] = ... def map[B](f: A => B) : M[B] = ... def flatMap[B](f: A => M[B]) : M[B] = flatten(map(f))}

Scala

Page 41: Introduction to Monads in Scala (1)

Review

val opMan : Option[Manager] = company getManagerdef extractBonus(m:Manager) : Option[Double] = ...val result = opMan flatMap extractBonus

Option[Double]

Scala

Page 42: Introduction to Monads in Scala (1)

4. Monads Can Be Built In Different Ways

”unit,” in Haskell it's called “return”

single argument “constructor” or ”actory”

A become a monad of type M[A]For List: unit(x) == List(x) For Option: unit(x) == Some(x)

Scala

Page 43: Introduction to Monads in Scala (1)

class M[A](value: A) { private def unit[B] (value : B) = new M(value)

…}

Page 44: Introduction to Monads in Scala (1)

Map based on flatMap

Scala does not require a separate "unit" function or method

class M[A](value: A) { private def unit[B] (value : B) = new M(value) def map[B](f: A => B) : M[B] =

flatMap {x => unit(f(x))} def flatMap[B](f: A => M[B]) : M[B] = ...}

Scala

Page 45: Introduction to Monads in Scala (1)

Basis 1Generic Haskell Scala

M data M a or newtype M a or instance Monad (M a)

class M[A]or case class M[A]or trait M[A]

M a M a M[A]

unit v return v new M(v)or M(v)

map f m fmap f m m map f

bind f m m >>= for f =<< m

m flatMap f

join join flatten

do for