SlideShare a Scribd company logo
Ittay Dror
             ittayd@tikalk.com
             @ittayd
             http://www.tikalk.com/blogs/ittayd




    Functional Programming
     From OOP perspective

26-Oct-11
What is FP?



     2        WWW.TIKALK.COM
“a programming paradigm that
    treats computation as the
   evaluation of mathematical
 functions and avoids state and
          mutable data”


               3             WWW.TIKALK.COM
• Functions as values
• Immutable data structures
• Mathematical models
• Referential transparency




                    4         WWW.TIKALK.COM
Why Should We Care?



         5        WWW.TIKALK.COM
Not everything is an
      object


         6             WWW.TIKALK.COM
May Forget


                                         Nothing to
service.init                             return?

val data = service.get
service.close             May forget /
                          call twice
println(data)




                     7                                WWW.TIKALK.COM
IOC



 8    WWW.TIKALK.COM
class Service {
  def withData[T](f: Data => T) {
    init
    if (thereIsData)
      f(getData)
    close
  }
}

service.witData(println)

                     9              WWW.TIKALK.COM
Simplification



      10         WWW.TIKALK.COM
In OOP:
 trait DataConsumer[T] {
   def withData(data: Data): T
 {


In FP:
 Data => T



                     11          WWW.TIKALK.COM
Factories are partially
  applied functions


           12         WWW.TIKALK.COM
val factory_: File => String => User =
  file => name => readFromFile(name)

val factory: String => User = factory_(usersFile)




                    Look ma, no interfaces
                        13                 WWW.TIKALK.COM
Security



   14      WWW.TIKALK.COM
Immutable data is
   sharable


        15          WWW.TIKALK.COM
Instead of modifying,
     create new


          16        WWW.TIKALK.COM
Abstraction



     17       WWW.TIKALK.COM
Functors, Applicatives,
      Monads


           18         WWW.TIKALK.COM
Usual way of describing:


                  WTF??

            19        WWW.TIKALK.COM
OO way: Design Patterns



           20       WWW.TIKALK.COM
Values in a context



         21           WWW.TIKALK.COM
Option[X]
List[X]
Future[X]


            22   WWW.TIKALK.COM
trait Future[A] {
          def get: A
        {
def findUser(name: String): Future[User] = …

…
val future = findUser(userName)
val user = future.get
println(user)

                      23               WWW.TIKALK.COM
trait Future[A] {
      def onReady(f: A => Unit)
    }


val future = findUser(userName)
future.onReady{user => println(user)}


                      24                WWW.TIKALK.COM
Functor




   25     WWW.TIKALK.COM
trait Future[A] {
  def map[B](f: A => B): Future[B]
{



val user = findUser(userName)
val age: Future[Int] = user.map{user => user.age}




                        26                 WWW.TIKALK.COM
val result = new ArrayList(list.size)
for (i <- 0 to (list.size - 1)) {
  result += compute(list(i))
}
result


                    Vs.
list.map(compute)


                      27                WWW.TIKALK.COM
class ComputeTask extends RecursiveTask {
  def compute = // split and call
invokeAll...
{
val pool = new ForkJoinPool()
val task = new ComputeTask()
pool.invoke(task)

                 Vs.
list.par.map(compute)


                        28           WWW.TIKALK.COM
trait Future[A] {
  def get: A

    def map[B](f: A => B) = new Future[B] {
      def get = f(Future.this.get)
    {
{




                       29              WWW.TIKALK.COM
def marry(man: User, woman: User): Family = ⠄⠄⠄

val joe = findUser("joe")
val jane = findUser("jane")




   Get Joe and Jane married


                        30                WWW.TIKALK.COM
Future[Family]



                                    User => Family


joe.map{joe => jane.map{jane => marry(joe, jane)}}


                           User => Future[Family]



                   Future[Future[Family]]


                           31                        WWW.TIKALK.COM
Attempt I
trait Future[A] {
  def apply[B, C](other: Future[B], f: (A, B) =>
C): Future[C]
}


 joe.apply(jane, marry)




                          32               WWW.TIKALK.COM
It is easier to work with
single argument functions


             33          WWW.TIKALK.COM
Curried Functions
val func: Int => Int => Int = a => b => a + b




  (marry _).curried


                        34                 WWW.TIKALK.COM
Attempt II

trait Future[A] {
  ...

    def apply[B](f: Future[A => B]): Future[B]
{




                          35                 WWW.TIKALK.COM
Future[User => Family]



                         User => User => Family


joe.apply(jane.map((marry _).curried))


               User => Future[Family]




                    36                        WWW.TIKALK.COM
We can do better



       37          WWW.TIKALK.COM
val marry: Future[User => User => Family] = Future(marry)

val partial: Future[User => Family] = futureMarry.apply(joe)

val family: Future[Family] = partial.apply(jane)



Future(marry).apply(joe).apply(jane)

Future(marry)(joe)(jane)




            Using apply compiles iff A is a function
                              38                    WWW.TIKALK.COM
Applicative Functors



         39            WWW.TIKALK.COM
• Put value in context
• Apply function in a context to value in
a context




                     40                     WWW.TIKALK.COM
trait Future[A] {
  def apply[B, C](b: Future[B])
                 (implicit ev: A <:< (B => C))
                 : Future[C]
{



object Future {
  def apply[A](a: A): Future[A]
}




       Future((marry _).curried)(joe)(jane)
                        41                 WWW.TIKALK.COM
trait Future[A] {
  def apply[B, C](b: Future[B])
                 (implicit ev: A <:< (B => C)) =
    new Future[C] {
      def get = Future.this.get(b.get)
    {
{
object Future {
  def apply[A](a: A) = new Future[A] {
      def get = a
    }
  }
}


                        42                 WWW.TIKALK.COM
Composing Functions that
Return Value in a Context


            43        WWW.TIKALK.COM
Composing Special
           Functions
def findUser(name: String): Future[User]
def findProfile(user: User): Future[Profile]




findProfile(findUser("joe"))


                        44                 WWW.TIKALK.COM
Monads



  45     WWW.TIKALK.COM
trait Future[A] {
  ...
  def flatMap[B](f: A => Future[B]): Future[B]
}




findUser("joe").flatMap(findProfile)




                        46                 WWW.TIKALK.COM
trait Future[A] {
  ...
  def flatMap[B](f: A => Future[B]): Future[B] =
    new Future[B] {
      def get = f(Future.this.get).get
    }
  }
}




                        47                 WWW.TIKALK.COM
All Together

trait Context[A] {
  def map[B](f: A => B): Context[B]
  def apply[B, C](b: Context[B])
                 (implicit ev: A <:< (B => C)): Context[C]
  def flatMap[B](f: A => Context[B]): Context[B]
}


                            48                    WWW.TIKALK.COM
Laws



 49    WWW.TIKALK.COM
Idiomatic “interface” for
    context classes


            50         WWW.TIKALK.COM
Monoid
• For type A to be (have) a Monoid:
   • Binary operation „•„: (A, A) => A
   • Identity element „∅‟: A




                     51                  WWW.TIKALK.COM
• String is a Monoid (2 ways):
   • Binary operation: append or prepend
   • Identity element: “”




• Int is a Monoid (2 ways):
   • Binary operation: + or *
   • Identity element: 0 or 1



                       52                  WWW.TIKALK.COM
• Future[A] is a monoid, if A is a monoid
   • Binary operation: Future(A#•)
      • (Future as applicative)
   • Identity element: Future(A#identity)




                       53                   WWW.TIKALK.COM
Monoids generalize folds



           54        WWW.TIKALK.COM
Folds: reduce values to
          one
• List(x,y,z) => ∅ • x • y • z
• Option(x) => if Some ∅ • x
                else ∅




                       55        WWW.TIKALK.COM
Unfolds: Create values
 from initial value and
        function
• 1, if (a < 3) Some(a, a+1) else None
   • Some(1, 2), Some(2, 3), None
   • ∅•1•2•3
   • If our monoid is a List:
       • Nil ++ List(1) ++ List(2) ++ List(3)
       • List(1,2,3)

                        56                      WWW.TIKALK.COM
Since many things are
monoids, we can have
many generic algorithms


           57       WWW.TIKALK.COM
Problems with subtyping:
 • Namespace pollution
 • Need to control the class
 • Describes behavior, not „is-a‟
 • Sometimes ability is conditional:
    • Future is a monoid if A is a monoid
 • Sometimes there are different definitions
    • E.g., 2 monoids for integers
 • Maybe a class has several facets
    • E.g. context of 2 values
                     58                   WWW.TIKALK.COM
Typeclass

• Define an API
• Create instance for each class that
can conform to the API




                    59                  WWW.TIKALK.COM
Java example:
 Comparator


      60        WWW.TIKALK.COM
trait Functor[F[_]] {
  def map[A, B](fa: F[A], f: A => B): F[B]
{
implicit object FutureHasFunctor
  extends Functor[Future] {
  def map[A, B](t: Future[A], f: A => B) =
    new Future[B] {
      def get = f(t.get)
    }
  }
{
def age(name: String) = {
  val functor = implicitly[Functor[Future]]
  functor.map(findUser(name), {u: User => u.age})
}
                        61                   WWW.TIKALK.COM
def age(name: String)
       (implicit functor: Functor[Future]) {
  functor.map(findUser(name), {u: User => u.age})
}




                        62                 WWW.TIKALK.COM
def age[F[_] : Functor](name: String) {
  functor.map(findUser(name), {u: User => u.age})
}




                        63                 WWW.TIKALK.COM
Generic Programming



         64       WWW.TIKALK.COM
def sequence[A, AP[_] : Applicative]
            (apas: List[AP[A]]): AP[List[A]]




val futureOfList = sequence(listOfFutures)

                        65                   WWW.TIKALK.COM
trait Applicative[AP[_]] {
  def pure[A](a: A) : AP[A]
  def apply[A, B](ap: AP[A => B], a: AP[A]): AP[B]
{




                        66                 WWW.TIKALK.COM
It is NOT important if you
don’t understand the next
             slide


             67         WWW.TIKALK.COM
def sequence[A, AP[_] : Applicative](apas: List[AP[A]]):
  AP[List[A]] = {

    val applicative = implicitly[Applicative[AP]]
    import applicative._
    val cons = pure{x: A => xs: List[A] => x :: xs}

    apas match {
      case Nil => pure(Nil)
      case apa :: apas =>
        val recursion = sequence(apas) // AP[List[A]]
        val partial = apply(cons, apa) // AP[List[A]=>List[A]]
        apply(partial, recursion)
    }
}


                               68                    WWW.TIKALK.COM
Beyond



  69     WWW.TIKALK.COM
val m = Map[Int, Int]()
m.map{case (k, v) => k + v}




                     70       WWW.TIKALK.COM
def map[B, That](f: A => B)
    (implicit bf: CanBuildFrom[Repr, B, That])
    : That




                        71                 WWW.TIKALK.COM
?
72   WWW.TIKALK.COM

More Related Content

PDF
Functional Scala 2020
ODP
Patterns for slick database applications
PDF
Using Scala Slick at FortyTwo
PDF
Cbse question paper class_xii_paper_2000
PDF
The Ring programming language version 1.5.3 book - Part 30 of 184
PDF
Hive function-cheat-sheet
PPTX
Running Free with the Monads
PPTX
Taking your side effects aside
Functional Scala 2020
Patterns for slick database applications
Using Scala Slick at FortyTwo
Cbse question paper class_xii_paper_2000
The Ring programming language version 1.5.3 book - Part 30 of 184
Hive function-cheat-sheet
Running Free with the Monads
Taking your side effects aside

What's hot (20)

PDF
How to extend map? Or why we need collections redesign? - Scalar 2017
ODP
WorkingWithSlick2.1.0
PDF
Typelevel summit
PPT
Oop lecture9 13
PDF
Herding types with Scala macros
PDF
Json and SQL DB serialization Introduction with Play! and Slick
PDF
Basic data structures in python
PDF
Scala.io
PDF
The Ring programming language version 1.2 book - Part 19 of 84
PPTX
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
PDF
The Ring programming language version 1.5.2 book - Part 29 of 181
PDF
The Ring programming language version 1.8 book - Part 43 of 202
PPTX
Seminar PSU 10.10.2014 mme
PPTX
Introduction to Monads in Scala (2)
PDF
Python speleology
DOCX
CLUSTERGRAM
PDF
Perm winter school 2014.01.31
PDF
The Ring programming language version 1.5 book - Part 8 of 31
PDF
The Ring programming language version 1.9 book - Part 46 of 210
PDF
The Ring programming language version 1.4 book - Part 8 of 30
How to extend map? Or why we need collections redesign? - Scalar 2017
WorkingWithSlick2.1.0
Typelevel summit
Oop lecture9 13
Herding types with Scala macros
Json and SQL DB serialization Introduction with Play! and Slick
Basic data structures in python
Scala.io
The Ring programming language version 1.2 book - Part 19 of 84
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
The Ring programming language version 1.5.2 book - Part 29 of 181
The Ring programming language version 1.8 book - Part 43 of 202
Seminar PSU 10.10.2014 mme
Introduction to Monads in Scala (2)
Python speleology
CLUSTERGRAM
Perm winter school 2014.01.31
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.9 book - Part 46 of 210
The Ring programming language version 1.4 book - Part 8 of 30
Ad

Similar to Functional Programming from OO perspective (Sayeret Lambda lecture) (20)

PPTX
ZIO: Powerful and Principled Functional Programming in Scala
PDF
Functor, Apply, Applicative And Monad
PPTX
Introduction to kotlin + spring boot demo
PDF
Coding in Style
KEY
Introducing AlloyUI DiagramBuilder
PDF
ADG Poznań - Kotlin for Android developers
PDF
The Ring programming language version 1.5.1 book - Part 36 of 180
PDF
Big picture of category theory in scala with deep dive into contravariant and...
PDF
Big picture of category theory in scala with deep dive into contravariant and...
PPTX
Why learn new programming languages
PDF
Berlin meetup
PDF
Monadologie
PDF
The What, Why And How of ClojureScript
PPTX
Monads and friends demystified
PDF
G*ワークショップ in 仙台 Grails(とことん)入門
PPTX
K is for Kotlin
PPTX
Programmation fonctionnelle Scala
PPTX
Joy of scala
PDF
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
PDF
Cocoaheads Meetup / Alex Zimin / Swift magic
ZIO: Powerful and Principled Functional Programming in Scala
Functor, Apply, Applicative And Monad
Introduction to kotlin + spring boot demo
Coding in Style
Introducing AlloyUI DiagramBuilder
ADG Poznań - Kotlin for Android developers
The Ring programming language version 1.5.1 book - Part 36 of 180
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...
Why learn new programming languages
Berlin meetup
Monadologie
The What, Why And How of ClojureScript
Monads and friends demystified
G*ワークショップ in 仙台 Grails(とことん)入門
K is for Kotlin
Programmation fonctionnelle Scala
Joy of scala
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Cocoaheads Meetup / Alex Zimin / Swift magic
Ad

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Approach and Philosophy of On baking technology
The AUB Centre for AI in Media Proposal.docx
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Understanding_Digital_Forensics_Presentation.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
NewMind AI Monthly Chronicles - July 2025
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectral efficient network and resource selection model in 5G networks
Advanced Soft Computing BINUS July 2025.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Machine learning based COVID-19 study performance prediction
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Dropbox Q2 2025 Financial Results & Investor Presentation
Diabetes mellitus diagnosis method based random forest with bat algorithm
NewMind AI Weekly Chronicles - August'25 Week I
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Approach and Philosophy of On baking technology

Functional Programming from OO perspective (Sayeret Lambda lecture)

  • 1. Ittay Dror [email protected] @ittayd http://www.tikalk.com/blogs/ittayd Functional Programming From OOP perspective 26-Oct-11
  • 2. What is FP? 2 WWW.TIKALK.COM
  • 3. “a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data” 3 WWW.TIKALK.COM
  • 4. • Functions as values • Immutable data structures • Mathematical models • Referential transparency 4 WWW.TIKALK.COM
  • 5. Why Should We Care? 5 WWW.TIKALK.COM
  • 6. Not everything is an object 6 WWW.TIKALK.COM
  • 7. May Forget Nothing to service.init return? val data = service.get service.close May forget / call twice println(data) 7 WWW.TIKALK.COM
  • 8. IOC 8 WWW.TIKALK.COM
  • 9. class Service { def withData[T](f: Data => T) { init if (thereIsData) f(getData) close } } service.witData(println) 9 WWW.TIKALK.COM
  • 10. Simplification 10 WWW.TIKALK.COM
  • 11. In OOP: trait DataConsumer[T] { def withData(data: Data): T { In FP: Data => T 11 WWW.TIKALK.COM
  • 12. Factories are partially applied functions 12 WWW.TIKALK.COM
  • 13. val factory_: File => String => User = file => name => readFromFile(name) val factory: String => User = factory_(usersFile) Look ma, no interfaces 13 WWW.TIKALK.COM
  • 14. Security 14 WWW.TIKALK.COM
  • 15. Immutable data is sharable 15 WWW.TIKALK.COM
  • 16. Instead of modifying, create new 16 WWW.TIKALK.COM
  • 17. Abstraction 17 WWW.TIKALK.COM
  • 18. Functors, Applicatives, Monads 18 WWW.TIKALK.COM
  • 19. Usual way of describing: WTF?? 19 WWW.TIKALK.COM
  • 20. OO way: Design Patterns 20 WWW.TIKALK.COM
  • 21. Values in a context 21 WWW.TIKALK.COM
  • 22. Option[X] List[X] Future[X] 22 WWW.TIKALK.COM
  • 23. trait Future[A] { def get: A { def findUser(name: String): Future[User] = … … val future = findUser(userName) val user = future.get println(user) 23 WWW.TIKALK.COM
  • 24. trait Future[A] { def onReady(f: A => Unit) } val future = findUser(userName) future.onReady{user => println(user)} 24 WWW.TIKALK.COM
  • 25. Functor 25 WWW.TIKALK.COM
  • 26. trait Future[A] { def map[B](f: A => B): Future[B] { val user = findUser(userName) val age: Future[Int] = user.map{user => user.age} 26 WWW.TIKALK.COM
  • 27. val result = new ArrayList(list.size) for (i <- 0 to (list.size - 1)) { result += compute(list(i)) } result Vs. list.map(compute) 27 WWW.TIKALK.COM
  • 28. class ComputeTask extends RecursiveTask { def compute = // split and call invokeAll... { val pool = new ForkJoinPool() val task = new ComputeTask() pool.invoke(task) Vs. list.par.map(compute) 28 WWW.TIKALK.COM
  • 29. trait Future[A] { def get: A def map[B](f: A => B) = new Future[B] { def get = f(Future.this.get) { { 29 WWW.TIKALK.COM
  • 30. def marry(man: User, woman: User): Family = ⠄⠄⠄ val joe = findUser("joe") val jane = findUser("jane") Get Joe and Jane married 30 WWW.TIKALK.COM
  • 31. Future[Family] User => Family joe.map{joe => jane.map{jane => marry(joe, jane)}} User => Future[Family] Future[Future[Family]] 31 WWW.TIKALK.COM
  • 32. Attempt I trait Future[A] { def apply[B, C](other: Future[B], f: (A, B) => C): Future[C] } joe.apply(jane, marry) 32 WWW.TIKALK.COM
  • 33. It is easier to work with single argument functions 33 WWW.TIKALK.COM
  • 34. Curried Functions val func: Int => Int => Int = a => b => a + b (marry _).curried 34 WWW.TIKALK.COM
  • 35. Attempt II trait Future[A] { ... def apply[B](f: Future[A => B]): Future[B] { 35 WWW.TIKALK.COM
  • 36. Future[User => Family] User => User => Family joe.apply(jane.map((marry _).curried)) User => Future[Family] 36 WWW.TIKALK.COM
  • 37. We can do better 37 WWW.TIKALK.COM
  • 38. val marry: Future[User => User => Family] = Future(marry) val partial: Future[User => Family] = futureMarry.apply(joe) val family: Future[Family] = partial.apply(jane) Future(marry).apply(joe).apply(jane) Future(marry)(joe)(jane) Using apply compiles iff A is a function 38 WWW.TIKALK.COM
  • 39. Applicative Functors 39 WWW.TIKALK.COM
  • 40. • Put value in context • Apply function in a context to value in a context 40 WWW.TIKALK.COM
  • 41. trait Future[A] { def apply[B, C](b: Future[B]) (implicit ev: A <:< (B => C)) : Future[C] { object Future { def apply[A](a: A): Future[A] } Future((marry _).curried)(joe)(jane) 41 WWW.TIKALK.COM
  • 42. trait Future[A] { def apply[B, C](b: Future[B]) (implicit ev: A <:< (B => C)) = new Future[C] { def get = Future.this.get(b.get) { { object Future { def apply[A](a: A) = new Future[A] { def get = a } } } 42 WWW.TIKALK.COM
  • 43. Composing Functions that Return Value in a Context 43 WWW.TIKALK.COM
  • 44. Composing Special Functions def findUser(name: String): Future[User] def findProfile(user: User): Future[Profile] findProfile(findUser("joe")) 44 WWW.TIKALK.COM
  • 45. Monads 45 WWW.TIKALK.COM
  • 46. trait Future[A] { ... def flatMap[B](f: A => Future[B]): Future[B] } findUser("joe").flatMap(findProfile) 46 WWW.TIKALK.COM
  • 47. trait Future[A] { ... def flatMap[B](f: A => Future[B]): Future[B] = new Future[B] { def get = f(Future.this.get).get } } } 47 WWW.TIKALK.COM
  • 48. All Together trait Context[A] { def map[B](f: A => B): Context[B] def apply[B, C](b: Context[B]) (implicit ev: A <:< (B => C)): Context[C] def flatMap[B](f: A => Context[B]): Context[B] } 48 WWW.TIKALK.COM
  • 49. Laws 49 WWW.TIKALK.COM
  • 50. Idiomatic “interface” for context classes 50 WWW.TIKALK.COM
  • 51. Monoid • For type A to be (have) a Monoid: • Binary operation „•„: (A, A) => A • Identity element „∅‟: A 51 WWW.TIKALK.COM
  • 52. • String is a Monoid (2 ways): • Binary operation: append or prepend • Identity element: “” • Int is a Monoid (2 ways): • Binary operation: + or * • Identity element: 0 or 1 52 WWW.TIKALK.COM
  • 53. • Future[A] is a monoid, if A is a monoid • Binary operation: Future(A#•) • (Future as applicative) • Identity element: Future(A#identity) 53 WWW.TIKALK.COM
  • 54. Monoids generalize folds 54 WWW.TIKALK.COM
  • 55. Folds: reduce values to one • List(x,y,z) => ∅ • x • y • z • Option(x) => if Some ∅ • x else ∅ 55 WWW.TIKALK.COM
  • 56. Unfolds: Create values from initial value and function • 1, if (a < 3) Some(a, a+1) else None • Some(1, 2), Some(2, 3), None • ∅•1•2•3 • If our monoid is a List: • Nil ++ List(1) ++ List(2) ++ List(3) • List(1,2,3) 56 WWW.TIKALK.COM
  • 57. Since many things are monoids, we can have many generic algorithms 57 WWW.TIKALK.COM
  • 58. Problems with subtyping: • Namespace pollution • Need to control the class • Describes behavior, not „is-a‟ • Sometimes ability is conditional: • Future is a monoid if A is a monoid • Sometimes there are different definitions • E.g., 2 monoids for integers • Maybe a class has several facets • E.g. context of 2 values 58 WWW.TIKALK.COM
  • 59. Typeclass • Define an API • Create instance for each class that can conform to the API 59 WWW.TIKALK.COM
  • 60. Java example: Comparator 60 WWW.TIKALK.COM
  • 61. trait Functor[F[_]] { def map[A, B](fa: F[A], f: A => B): F[B] { implicit object FutureHasFunctor extends Functor[Future] { def map[A, B](t: Future[A], f: A => B) = new Future[B] { def get = f(t.get) } } { def age(name: String) = { val functor = implicitly[Functor[Future]] functor.map(findUser(name), {u: User => u.age}) } 61 WWW.TIKALK.COM
  • 62. def age(name: String) (implicit functor: Functor[Future]) { functor.map(findUser(name), {u: User => u.age}) } 62 WWW.TIKALK.COM
  • 63. def age[F[_] : Functor](name: String) { functor.map(findUser(name), {u: User => u.age}) } 63 WWW.TIKALK.COM
  • 64. Generic Programming 64 WWW.TIKALK.COM
  • 65. def sequence[A, AP[_] : Applicative] (apas: List[AP[A]]): AP[List[A]] val futureOfList = sequence(listOfFutures) 65 WWW.TIKALK.COM
  • 66. trait Applicative[AP[_]] { def pure[A](a: A) : AP[A] def apply[A, B](ap: AP[A => B], a: AP[A]): AP[B] { 66 WWW.TIKALK.COM
  • 67. It is NOT important if you don’t understand the next slide 67 WWW.TIKALK.COM
  • 68. def sequence[A, AP[_] : Applicative](apas: List[AP[A]]): AP[List[A]] = { val applicative = implicitly[Applicative[AP]] import applicative._ val cons = pure{x: A => xs: List[A] => x :: xs} apas match { case Nil => pure(Nil) case apa :: apas => val recursion = sequence(apas) // AP[List[A]] val partial = apply(cons, apa) // AP[List[A]=>List[A]] apply(partial, recursion) } } 68 WWW.TIKALK.COM
  • 69. Beyond 69 WWW.TIKALK.COM
  • 70. val m = Map[Int, Int]() m.map{case (k, v) => k + v} 70 WWW.TIKALK.COM
  • 71. def map[B, That](f: A => B) (implicit bf: CanBuildFrom[Repr, B, That]) : That 71 WWW.TIKALK.COM
  • 72. ? 72 WWW.TIKALK.COM