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
Written by Dinesh Vasudevan
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Scala
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#