SlideShare una empresa de Scribd logo
1 de 72
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

How to extend map? Or why we need collections redesign? - Scalar 2017
How to extend map? Or why we need collections redesign? - Scalar 2017How to extend map? Or why we need collections redesign? - Scalar 2017
How to extend map? Or why we need collections redesign? - Scalar 2017Szymon Matejczyk
 
WorkingWithSlick2.1.0
WorkingWithSlick2.1.0WorkingWithSlick2.1.0
WorkingWithSlick2.1.0Knoldus Inc.
 
Herding types with Scala macros
Herding types with Scala macrosHerding types with Scala macros
Herding types with Scala macrosMarina Sigaeva
 
Json and SQL DB serialization Introduction with Play! and Slick
Json and SQL DB serialization Introduction with Play! and SlickJson and SQL DB serialization Introduction with Play! and Slick
Json and SQL DB serialization Introduction with Play! and SlickStephen Kemmerling
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in pythonLifna C.S
 
The Ring programming language version 1.2 book - Part 19 of 84
The Ring programming language version 1.2 book - Part 19 of 84The Ring programming language version 1.2 book - Part 19 of 84
The Ring programming language version 1.2 book - Part 19 of 84Mahmoud Samir Fayed
 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavVyacheslav Arbuzov
 
The Ring programming language version 1.5.2 book - Part 29 of 181
The Ring programming language version 1.5.2 book - Part 29 of 181The Ring programming language version 1.5.2 book - Part 29 of 181
The Ring programming language version 1.5.2 book - Part 29 of 181Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 43 of 202
The Ring programming language version 1.8 book - Part 43 of 202The Ring programming language version 1.8 book - Part 43 of 202
The Ring programming language version 1.8 book - Part 43 of 202Mahmoud Samir Fayed
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)stasimus
 
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 46 of 210
The Ring programming language version 1.9 book - Part 46 of 210The Ring programming language version 1.9 book - Part 46 of 210
The Ring programming language version 1.9 book - Part 46 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.4 book - Part 8 of 30
The Ring programming language version 1.4 book - Part 8 of 30The Ring programming language version 1.4 book - Part 8 of 30
The Ring programming language version 1.4 book - Part 8 of 30Mahmoud Samir Fayed
 

La actualidad más candente (20)

How to extend map? Or why we need collections redesign? - Scalar 2017
How to extend map? Or why we need collections redesign? - Scalar 2017How to extend map? Or why we need collections redesign? - Scalar 2017
How to extend map? Or why we need collections redesign? - Scalar 2017
 
WorkingWithSlick2.1.0
WorkingWithSlick2.1.0WorkingWithSlick2.1.0
WorkingWithSlick2.1.0
 
Typelevel summit
Typelevel summitTypelevel summit
Typelevel summit
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
Herding types with Scala macros
Herding types with Scala macrosHerding types with Scala macros
Herding types with Scala macros
 
Json and SQL DB serialization Introduction with Play! and Slick
Json and SQL DB serialization Introduction with Play! and SlickJson and SQL DB serialization Introduction with Play! and Slick
Json and SQL DB serialization Introduction with Play! and Slick
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Scala.io
Scala.ioScala.io
Scala.io
 
The Ring programming language version 1.2 book - Part 19 of 84
The Ring programming language version 1.2 book - Part 19 of 84The Ring programming language version 1.2 book - Part 19 of 84
The Ring programming language version 1.2 book - Part 19 of 84
 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
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.5.2 book - Part 29 of 181The Ring programming language version 1.5.2 book - Part 29 of 181
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
The Ring programming language version 1.8 book - Part 43 of 202The Ring programming language version 1.8 book - Part 43 of 202
The Ring programming language version 1.8 book - Part 43 of 202
 
Seminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mmeSeminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mme
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
 
Python speleology
Python speleologyPython speleology
Python speleology
 
CLUSTERGRAM
CLUSTERGRAMCLUSTERGRAM
CLUSTERGRAM
 
Perm winter school 2014.01.31
Perm winter school 2014.01.31Perm winter school 2014.01.31
Perm winter school 2014.01.31
 
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 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.9 book - Part 46 of 210The Ring programming language version 1.9 book - Part 46 of 210
The Ring programming language version 1.9 book - Part 46 of 210
 
The Ring programming language version 1.4 book - Part 8 of 30
The Ring programming language version 1.4 book - Part 8 of 30The Ring programming language version 1.4 book - Part 8 of 30
The Ring programming language version 1.4 book - Part 8 of 30
 

Similar a Functional Programming from OO perspective (Sayeret Lambda lecture)

ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaWiem Zine Elabidine
 
Functor, Apply, Applicative And Monad
Functor, Apply, Applicative And MonadFunctor, Apply, Applicative And Monad
Functor, Apply, Applicative And MonadOliver Daff
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoMuhammad Abdullah
 
Introducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilderIntroducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilderEduardo Lundgren
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersBartosz Kosarzycki
 
The Ring programming language version 1.5.1 book - Part 36 of 180
The Ring programming language version 1.5.1 book - Part 36 of 180The Ring programming language version 1.5.1 book - Part 36 of 180
The Ring programming language version 1.5.1 book - Part 36 of 180Mahmoud Samir Fayed
 
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...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...Piotr Paradziński
 
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...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...Scalac
 
Why learn new programming languages
Why learn new programming languagesWhy learn new programming languages
Why learn new programming languagesJonas Follesø
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
The What, Why And How of ClojureScript
The What, Why And How of ClojureScriptThe What, Why And How of ClojureScript
The What, Why And How of ClojureScriptIvan Bokii
 
Monads and friends demystified
Monads and friends demystifiedMonads and friends demystified
Monads and friends demystifiedAlessandro Lacava
 
G*ワークショップ in 仙台 Grails(とことん)入門
G*ワークショップ in 仙台 Grails(とことん)入門G*ワークショップ in 仙台 Grails(とことん)入門
G*ワークショップ in 仙台 Grails(とことん)入門Tsuyoshi Yamamoto
 
K is for Kotlin
K is for KotlinK is for Kotlin
K is for KotlinTechMagic
 
Programmation fonctionnelle Scala
Programmation fonctionnelle ScalaProgrammation fonctionnelle Scala
Programmation fonctionnelle ScalaSlim Ouertani
 
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...GeeksLab Odessa
 
Cocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magicCocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magicBadoo Development
 

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

ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in Scala
 
Functor, Apply, Applicative And Monad
Functor, Apply, Applicative And MonadFunctor, Apply, Applicative And Monad
Functor, Apply, Applicative And Monad
 
Introduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demo
 
Coding in Style
Coding in StyleCoding in Style
Coding in Style
 
Introducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilderIntroducing AlloyUI DiagramBuilder
Introducing AlloyUI DiagramBuilder
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developers
 
The Ring programming language version 1.5.1 book - Part 36 of 180
The Ring programming language version 1.5.1 book - Part 36 of 180The Ring programming language version 1.5.1 book - Part 36 of 180
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...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...
 
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...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
Why learn new programming languagesWhy learn new programming languages
Why learn new programming languages
 
Berlin meetup
Berlin meetupBerlin meetup
Berlin meetup
 
Monadologie
MonadologieMonadologie
Monadologie
 
The What, Why And How of ClojureScript
The What, Why And How of ClojureScriptThe What, Why And How of ClojureScript
The What, Why And How of ClojureScript
 
Monads and friends demystified
Monads and friends demystifiedMonads and friends demystified
Monads and friends demystified
 
G*ワークショップ in 仙台 Grails(とことん)入門
G*ワークショップ in 仙台 Grails(とことん)入門G*ワークショップ in 仙台 Grails(とことん)入門
G*ワークショップ in 仙台 Grails(とことん)入門
 
K is for Kotlin
K is for KotlinK is for Kotlin
K is for Kotlin
 
Programmation fonctionnelle Scala
Programmation fonctionnelle ScalaProgrammation fonctionnelle Scala
Programmation fonctionnelle Scala
 
Joy of scala
Joy of scalaJoy of scala
Joy of scala
 
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
 
Cocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magicCocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magic
 

Último

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 

Último (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

Functional Programming from OO perspective (Sayeret Lambda lecture)

  • 1. Ittay Dror ittayd@tikalk.com @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