Last Updated: February 25, 2016
·
401
· dinks

Functional v/s Object Oriented

Cat catches a Bird and eats it

Object Oriented

class Bird
class Cat {
    def catch(b:Bird): Unit = ...
    def eat(): Unit = ...
}
val cat = new Cat
val bird = new Bird
cat.catch(bird)
cat.eat

Functional

trait Cat
trait Bird
trait Catch
trait TummyFull

def catch(hunter: Cat, prey: Bird): Cat with Catch
def eat(consumer: Cat with Catch): Cat with TummyFull

val story = (catch _) andThen (eat _)
story(new Cat, new Bird)

Courtesy Scala in Depth