SlideShare a Scribd company logo
1 of 151
127.0.0.1




Aslam Khan :: @aslamkhn :: aslam.khan@factor10.com :: http://aslamkhan.net   factor10
Not Quite Object Oriented
                       a frustrating and incomplete journey from OO to FP




Aslam Khan :: @aslamkhn :: +Aslam Khan :: factor10.com :: aslamkhan.net     factor10
My journey in trying to write code
     it felt like a language journey, but it’s actually far from that
My journey in trying to write code
                    it felt like a language journey, but it’s actually far from that




In the beginning,
     I used C
My journey in trying to write code
                       it felt like a language journey, but it’s actually far from that




In the beginning,   Procedural
     I used C          C++
My journey in trying to write code
                       it felt like a language journey, but it’s actually far from that




In the beginning,   Procedural       Procedural
     I used C          C++              Java
My journey in trying to write code
                       it felt like a language journey, but it’s actually far from that




                                                            Patterns
In the beginning,   Procedural       Procedural
                                                               and
     I used C          C++              Java
                                                         Polymorphism
My journey in trying to write code
                       it felt like a language journey, but it’s actually far from that



                                                                                 Java
                                                            Patterns
In the beginning,   Procedural       Procedural
                                                               and               Ruby
     I used C          C++              Java
                                                         Polymorphism             C#
My journey in trying to write code
                       it felt like a language journey, but it’s actually far from that



                                                                                 Java
                                                            Patterns                       Finally
In the beginning,   Procedural       Procedural
                                                               and               Ruby       Object
     I used C          C++              Java
                                                         Polymorphism                     Oriented!
                                                                                  C#
My journey in trying to write code
                       it felt like a language journey, but it’s actually far from that



                                                                                 Java
                                                            Patterns                       Finally
In the beginning,   Procedural       Procedural
                                                               and               Ruby       Object
     I used C          C++              Java
                                                         Polymorphism                     Oriented!
                                                                                  C#



                          then I had to write a parser in JavaScript
My journey in trying to write code
                         it felt like a language journey, but it’s actually far from that



                                                                                   Java
                                                              Patterns                       Finally
In the beginning,   Procedural         Procedural
                                                                 and               Ruby       Object
     I used C          C++                Java
                                                           Polymorphism                     Oriented!
                                                                                    C#



                           then I had to write a parser in JavaScript




           Ultra messy
            JavaScript
My journey in trying to write code
                         it felt like a language journey, but it’s actually far from that



                                                                                   Java
                                                              Patterns                       Finally
In the beginning,   Procedural         Procedural
                                                                 and               Ruby       Object
     I used C          C++                Java
                                                           Polymorphism                     Oriented!
                                                                                    C#



                           then I had to write a parser in JavaScript




           Ultra messy         “Object Oriented”
            JavaScript            JavaScript
My journey in trying to write code
                         it felt like a language journey, but it’s actually far from that



                                                                                   Java
                                                              Patterns                       Finally
In the beginning,   Procedural         Procedural
                                                                 and               Ruby       Object
     I used C          C++                Java
                                                           Polymorphism                     Oriented!
                                                                                    C#



                           then I had to write a parser in JavaScript




           Ultra messy         “Object Oriented”         Haskell Tutorial
            JavaScript            JavaScript               on Parsers
My journey in trying to write code
                         it felt like a language journey, but it’s actually far from that



                                                                                   Java
                                                              Patterns                           Finally
In the beginning,   Procedural         Procedural
                                                                 and               Ruby           Object
     I used C          C++                Java
                                                           Polymorphism                         Oriented!
                                                                                    C#



                           then I had to write a parser in JavaScript




           Ultra messy         “Object Oriented”         Haskell Tutorial          More functional
            JavaScript            JavaScript               on Parsers                JavaScript
My journey in trying to write code
                         it felt like a language journey, but it’s actually far from that



                                                                                   Java
                                                              Patterns                           Finally
In the beginning,   Procedural         Procedural
                                                                 and               Ruby           Object
     I used C          C++                Java
                                                           Polymorphism                         Oriented!
                                                                                    C#



                           then I had to write a parser in JavaScript




           Ultra messy         “Object Oriented”         Haskell Tutorial          More functional
            JavaScript            JavaScript               on Parsers                JavaScript




                                   inspired to be more functional
My journey in trying to write code
                         it felt like a language journey, but it’s actually far from that



                                                                                   Java
                                                              Patterns                           Finally
In the beginning,     Procedural       Procedural
                                                                 and               Ruby           Object
     I used C            C++              Java
                                                           Polymorphism                         Oriented!
                                                                                    C#



                            then I had to write a parser in JavaScript




           Ultra messy         “Object Oriented”         Haskell Tutorial          More functional
            JavaScript            JavaScript               on Parsers                JavaScript




                                   inspired to be more functional



    Object Oriented
         Scala
My journey in trying to write code
                         it felt like a language journey, but it’s actually far from that



                                                                                   Java
                                                              Patterns                           Finally
In the beginning,     Procedural       Procedural
                                                                 and               Ruby           Object
     I used C            C++              Java
                                                           Polymorphism                         Oriented!
                                                                                    C#



                            then I had to write a parser in JavaScript




           Ultra messy         “Object Oriented”         Haskell Tutorial          More functional
            JavaScript            JavaScript               on Parsers                JavaScript




                                   inspired to be more functional



    Object Oriented
                              Read Haskell
         Scala
My journey in trying to write code
                         it felt like a language journey, but it’s actually far from that



                                                                                   Java
                                                              Patterns                           Finally
In the beginning,     Procedural       Procedural
                                                                 and               Ruby           Object
     I used C            C++              Java
                                                           Polymorphism                         Oriented!
                                                                                    C#



                            then I had to write a parser in JavaScript




           Ultra messy         “Object Oriented”         Haskell Tutorial          More functional
            JavaScript            JavaScript               on Parsers                JavaScript




                                   inspired to be more functional



    Object Oriented                                        More
                              Read Haskell
         Scala                                        Functional Scala
My journey in trying to write code
                         it felt like a language journey, but it’s actually far from that



                                                                                   Java
                                                              Patterns                           Finally
In the beginning,     Procedural       Procedural
                                                                 and               Ruby           Object
     I used C            C++              Java
                                                           Polymorphism                         Oriented!
                                                                                    C#



                            then I had to write a parser in JavaScript




           Ultra messy         “Object Oriented”         Haskell Tutorial          More functional
            JavaScript            JavaScript               on Parsers                JavaScript




                                   inspired to be more functional



    Object Oriented                                        More
                              Read Haskell                                       Clojure
         Scala                                        Functional Scala
My journey in trying to write code
                         it felt like a language journey, but it’s actually far from that



                                                                                   Java
                                                              Patterns                           Finally
In the beginning,     Procedural       Procedural
                                                                 and               Ruby           Object
     I used C            C++              Java
                                                           Polymorphism                         Oriented!
                                                                                    C#



                            then I had to write a parser in JavaScript




           Ultra messy         “Object Oriented”         Haskell Tutorial          More functional
            JavaScript            JavaScript               on Parsers                JavaScript




                                   inspired to be more functional



    Object Oriented                                        More                                 Long mental
                              Read Haskell                                       Clojure
         Scala                                        Functional Scala                           journey ...
My functional learning loop
Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track




                            Read about
                        functional concepts
                             in Haskell

                          do this slowly … don’t rush!
My functional learning loop
Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track




                            Read about
                        functional concepts
                             in Haskell

                          do this slowly … don’t rush!




                                                         Try the equivalent in Scala


                                                              with some trivial example
My functional learning loop
Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track




                             Read about
                         functional concepts
                              in Haskell

                            do this slowly … don’t rush!




                                                              Try the equivalent in Scala


                                                                   with some trivial example




                      Try it on your project


                 the essential parts of the problem and solution
My functional learning loop
           Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track




                                          Read about
                                      functional concepts
                                           in Haskell

                                         do this slowly … don’t rush!




       Stuck ?                                                             Try the equivalent in Scala

(impurity) or (ignorance) ?
                                                                                with some trivial example




                                   Try it on your project


                              the essential parts of the problem and solution
My functional learning loop
           Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track




                                          Read about
                                      functional concepts
                                           in Haskell

                                         do this slowly … don’t rush!




       Stuck ?                                                             Try the equivalent in Scala

(impurity) or (ignorance) ?
                                                                                with some trivial example




                                   Try it on your project


                              the essential parts of the problem and solution
The language of functional
   And there are quite a few new concepts to understand
The language of functional
       And there are quite a few new concepts to understand



                                                      Currying
     Algebraic Types


Closures                                    Type Classes


    Monads                                            High Order Functions

       Pattern Matching                                   Monoids


                     Functors

  Lambdas                                  Polymorphic Functions

 List Comprehension
The language of functional
         And there are quite a few new concepts to understand



                                                        Currying



  Closures                                    Type Classes


      Monads

                                                            Monoids


                       Functors

     Lambdas                                 Polymorphic Functions



List Comprehension                               High Order Functions

 Algebraic Types                                    Pattern Matching
Let’s write some Java
      Find all even integers in a range




   List<Integer> xs = range(1,20);

   List<Integer> evenRange = evens(xs);
Let’s write some Java
                                       Find all even integers in a range



                                                     List<Integer> range(int start, int end) {
                                                       List<Integer> xs = new ArrayList<Integer>();
                                                       for (int i = start; i < end + 1; i++) {
                                                          xs.add(i);
                                                       }
                                                       return xs;
                                                     }



List<Integer> xs = range(1,20);

List<Integer> evenRange = evens(xs);
Let’s write some Java
                                       Find all even integers in a range



                                                     List<Integer> range(int start, int end) {
                                                       List<Integer> xs = new ArrayList<Integer>();
                                                       for (int i = start; i < end + 1; i++) {
                                                          xs.add(i);
                                                       }
                                                       return xs;
                                                     }



List<Integer> xs = range(1,20);

List<Integer> evenRange = evens(xs);

                                                    List<Integer> evens(List<Integer> xs) {
                                                      List<Integer> keep = new ArrayList<Integer>();
                                                      for (Integer x : xs) {
                                                        if (isEven(x)) keep.add(x);
                                                      }
                                                      return keep;
                                                    }
Let’s write some Java
                                       Find all even integers in a range



                                                     List<Integer> range(int start, int end) {
                                                       List<Integer> xs = new ArrayList<Integer>();
                                                       for (int i = start; i < end + 1; i++) {
                                                          xs.add(i);
                                                       }
                                                       return xs;
                                                     }



List<Integer> xs = range(1,20);

List<Integer> evenRange = evens(xs);

                                                    List<Integer> evens(List<Integer> xs) {
                                                      List<Integer> keep = new ArrayList<Integer>();
                                                      for (Integer x : xs) {
                                                        if (isEven(x)) keep.add(x);
                                                      }
                                                      return keep;
                                                    }




                                                    boolean isEven(Integer x) { return x % 2 == 0; }
The Haskell way?
Remember: We are using Haskell to understand functional concepts




evens xs = [ x | x <- xs, even x ]
The Haskell way?
      Remember: We are using Haskell to understand functional concepts




      evens xs = [ x | x <- xs, even x ]


function evens
The Haskell way?
      Remember: We are using Haskell to understand functional concepts




      evens xs = [ x | x <- xs, even x ]


function evens   applied to xs
The Haskell way?
      Remember: We are using Haskell to understand functional concepts




      evens xs = [ x | x <- xs, even x ]


function evens   applied to xs   is a list
The Haskell way?
      Remember: We are using Haskell to understand functional concepts




      evens xs = [ x | x <- xs, even x ]


function evens   applied to xs   is a list   for each x in xs
The Haskell way?
      Remember: We are using Haskell to understand functional concepts




      evens xs = [ x | x <- xs, even x ]


function evens   applied to xs   is a list   for each x in xs   where x is even
The Haskell way?
      Remember: We are using Haskell to understand functional concepts




      evens xs = [ x | x <- xs, even x ]


function evens   applied to xs   is a list   for each x in xs   where x is even




                  for example:
                            evens [1..20]
And in Scala
 list comprehensions are sometimes called for-comprehensions in Scala-land




val evens = (xs: List[Int]) =>
   for (x <- xs; even(x) ) yield x
And in Scala
 list comprehensions are sometimes called for-comprehensions in Scala-land




val evens = (xs: List[Int]) =>
   for (x <- xs; even(x) ) yield x

 evens
And in Scala
 list comprehensions are sometimes called for-comprehensions in Scala-land




val evens = (xs: List[Int]) =>
   for (x <- xs; even(x) ) yield x

 evens   takes from a list xs
And in Scala
 list comprehensions are sometimes called for-comprehensions in Scala-land




val evens = (xs: List[Int]) =>
   for (x <- xs; even(x) ) yield x

 evens   takes from a list xs   each x
And in Scala
 list comprehensions are sometimes called for-comprehensions in Scala-land




val evens = (xs: List[Int]) =>
   for (x <- xs; even(x) ) yield x

 evens   takes from a list xs   each x      in xs
And in Scala
 list comprehensions are sometimes called for-comprehensions in Scala-land




val evens = (xs: List[Int]) =>
   for (x <- xs; even(x) ) yield x

 evens   takes from a list xs   each x      in xs   where x is even
And in Scala
 list comprehensions are sometimes called for-comprehensions in Scala-land




val evens = (xs: List[Int]) =>
   for (x <- xs; even(x) ) yield x

 evens   takes from a list xs   each x      in xs   where x is even




val even = (x:Int) => x % 2 == 0
And in Clojure
         Let’s use the built in “filter” function




(defn evens [xs] (filter even? xs))
And in Clojure
         Let’s use the built in “filter” function




(defn evens [xs] (filter even? xs))


 evens
And in Clojure
                   Let’s use the built in “filter” function




(defn evens [xs] (filter even? xs))


 evens   applied to a list xs
And in Clojure
                   Let’s use the built in “filter” function




(defn evens [xs] (filter even? xs))


 evens   applied to a list xs    applies the filter
And in Clojure
                   Let’s use the built in “filter” function




(defn evens [xs] (filter even? xs))


 evens   applied to a list xs    applies the filter            even? on xs
What if we have lots of “filters”
   Remember: We are using Haskell to understand functional concepts




evens xs = [ x | x <- xs, even x ]
 odds xs = [ x | x <- xs, odd x ]
What if we have lots of “filters”
   Remember: We are using Haskell to understand functional concepts




evens xs = [ x | x <- xs, even x ]
 odds xs = [ x | x <- xs, odd x ]
What if we have lots of “filters”
   Remember: We are using Haskell to understand functional concepts




evens xs = [ x | x <- xs, even x ]
 odds xs = [ x | x <- xs, odd x ]

                                 abstract the parts that change




 find f xs = [ x | x <- xs, f x ]
What if we have lots of “filters”
         Remember: We are using Haskell to understand functional concepts




 evens xs = [ x | x <- xs, even x ]
  odds xs = [ x | x <- xs, odd x ]

                                       abstract the parts that change




    find f xs = [ x | x <- xs, f x ]

using filter f
What if we have lots of “filters”
         Remember: We are using Haskell to understand functional concepts




 evens xs = [ x | x <- xs, even x ]
  odds xs = [ x | x <- xs, odd x ]

                                       abstract the parts that change




    find f xs = [ x | x <- xs, f x ]

using filter f    on a list xs
What if we have lots of “filters”
         Remember: We are using Haskell to understand functional concepts




 evens xs = [ x | x <- xs, even x ]
  odds xs = [ x | x <- xs, odd x ]

                                        abstract the parts that change




    find f xs = [ x | x <- xs, f x ]

using filter f    on a list xs     for each x in xs
What if we have lots of “filters”
         Remember: We are using Haskell to understand functional concepts




 evens xs = [ x | x <- xs, even x ]
  odds xs = [ x | x <- xs, odd x ]

                                        abstract the parts that change




    find f xs = [ x | x <- xs, f x ]

using filter f    on a list xs     for each x in xs                  where x satisfies the filter f
High Order Functions
      helps you get DRY and leads to clean compositions




val find = (predicate :Int => Boolean, xs :List[Int]) =>
   for (x <- xs; if predicate(x)) yield x




          val even = (x :Int) => x % 2 == 0
          val odd = (x :Int) => x % 2 == 1
High Order Functions
       helps you get DRY and leads to clean compositions




val find = (predicate :Int => Boolean, xs :List[Int]) =>
   for (x <- xs; if predicate(x)) yield x


 use any function which we
 locally call predicate




           val even = (x :Int) => x % 2 == 0
           val odd = (x :Int) => x % 2 == 1
High Order Functions
       helps you get DRY and leads to clean compositions




val find = (predicate :Int => Boolean, xs :List[Int]) =>
   for (x <- xs; if predicate(x)) yield x


 use any function which we    as long as the provided function takes an
 locally call predicate       integer and returns a boolean




           val even = (x :Int) => x % 2 == 0
           val odd = (x :Int) => x % 2 == 1
High Order Functions
       helps you get DRY and leads to clean compositions




val find = (predicate :Int => Boolean, xs :List[Int]) =>
   for (x <- xs; if predicate(x)) yield x


 use any function which we    as long as the provided function takes an
 locally call predicate       integer and returns a boolean




           val even = (x :Int) => x % 2 == 0
           val odd = (x :Int) => x % 2 == 1
High Order Functions
       helps you get DRY and leads to clean compositions




val find = (predicate :Int => Boolean, xs :List[Int]) =>
   for (x <- xs; if predicate(x)) yield x


 use any function which we    as long as the provided function takes an
 locally call predicate       integer and returns a boolean




           val even = (x :Int) => x % 2 == 0
           val odd = (x :Int) => x % 2 == 1


                                 for example




               find(even, List.range(0,21))
               find(odd, List.range(0,21))
And in Clojure
       notice how we compose more powerful functions from smaller functions




(defn find [predicate, xs] (filter predicate xs))
And in Clojure
       notice how we compose more powerful functions from smaller functions




(defn find [predicate, xs] (filter predicate xs))




                               function
And in Clojure
       notice how we compose more powerful functions from smaller functions




(defn find [predicate, xs] (filter predicate xs))




                               function


                                          for example




                        (find odd? [1,2,3,4])
                        (find even? [1,2,3,4])
Let’s apply this learning
      how can we model a few rules?
Let’s apply this learning
      how can we model a few rules?




          Non Substitutable
       have to downcast to get
          the specific type
Let’s apply this learning
                          how can we model a few rules?




   Non Substitutable
have to downcast to get
   the specific type
Let’s apply this learning
                          how can we model a few rules?




   Non Substitutable                                 Semantically Wrong
have to downcast to get                             how can a FixedTime
   the specific type                               project be over budget?
It’s the rules that vary
    In Java, using the strategy patterns
It’s the rules that vary
    In Java, using the strategy patterns




                  class Project {
                    public boolean check(CheckProjectRule rule) {
                      return rule.check(this);
                    }
                  }
It’s the rules that vary
    In Java, using the strategy patterns




                  class Project {
                    public boolean check(CheckProjectRule rule) {
                      return rule.check(this);
                    }
                  }



                  interface ProjectRule {
                    public boolean check(Project p);
                  }
It’s the rules that vary
    In Java, using the strategy patterns




                  class Project {
                    public boolean check(CheckProjectRule rule) {
                      return rule.check(this);
                    }
                  }



                  interface ProjectRule {
                    public boolean check(Project p);
                  }




                 class OverdueProjectRule implements ProjectRule {
                   ...
                   public boolean check(Project p) {
                     return p.getEndDate().isBefore(today());
                   }
                 }
In Scala
Using high order functions
In Scala
Using high order functions




     class Project {
       val check = (rule :Prj => Boolean) => rule(this)
     }
In Scala
Using high order functions




     class Project {
       val check = (rule :Prj => Boolean) => rule(this)
     }




       val overBudgetRule = (p :Project) =>
        p.costToDate() > p.budget()
In Scala
Using high order functions




     class Project {
       val check = (rule :Prj => Boolean) => rule(this)
     }




       val overBudgetRule = (p :Project) =>
        p.costToDate() > p.budget()



       val overdueProjectRule = (p :Project) =>
        p.getEndDate().isBefore(today())




                The Strategy Pattern
    is not needed because of high order functions
A Model for Shapes

                             Shape

                      abstract area()




                    Circle                             Rectangle

              Circle(x, y, r)                     Rectangle(x, y, w, h)




                                     Square

                                Square(x, y, l)




It’s easy to imagine what the Java code will look like
Haskell
                     not inheritance, but “alternatives” of cases of the type




data Shape = Circle Float Float Float |                                  Shape
            
             Rectangle Float Float Float Float |                  abstract area()


             Square Float Float Float


                                                                Circle                             Rectangle

                                                          Circle(x, y, r)                     Rectangle(x, y, w, h)




                                                                                 Square

                                                                            Square(x, y, l)
Haskell
                     not inheritance, but “alternatives” of cases of the type




data Shape = Circle Float Float Float |                                  Shape
            
             Rectangle Float Float Float Float |                  abstract area()


             Square Float Float Float


                                                                Circle                             Rectangle

                                                          Circle(x, y, r)                     Rectangle(x, y, w, h)




                                                                                 Square

                                                                            Square(x, y, l)
Haskell
                     not inheritance, but “alternatives” of cases of the type




data Shape = Circle Float Float Float |                                  Shape
            
             Rectangle Float Float Float Float |                  abstract area()


             Square Float Float Float


                                                                Circle                             Rectangle

                                                          Circle(x, y, r)                     Rectangle(x, y, w, h)




                                                                                 Square

                                                                            Square(x, y, l)
Haskell
                     not inheritance, but “alternatives” of cases of the type




data Shape = Circle Float Float Float |                                  Shape
            
             Rectangle Float Float Float Float |                  abstract area()


             Square Float Float Float


                                                                Circle                             Rectangle

                                                          Circle(x, y, r)                     Rectangle(x, y, w, h)




                                                                                 Square

                                                                            Square(x, y, l)
Haskell
                     not inheritance, but “alternatives” of cases of the type




data Shape = Circle Float Float Float |                                  Shape
            
             Rectangle Float Float Float Float |                  abstract area()


             Square Float Float Float


                                                                Circle                             Rectangle

                                                          Circle(x, y, r)                     Rectangle(x, y, w, h)
area :: Shape -> Float


                                                                                 Square

                                                                            Square(x, y, l)
Haskell
                        not inheritance, but “alternatives” of cases of the type




data Shape = Circle Float Float Float |                                     Shape
            
             Rectangle Float Float Float Float |                     abstract area()


               Square Float Float Float


                                                                   Circle                             Rectangle

                                                             Circle(x, y, r)                     Rectangle(x, y, w, h)
area :: Shape -> Float

area (Circle _ _ r) = pi * r^2
                                                                                    Square
area (Rectangle _ _ w h) = w * h
                                                                               Square(x, y, l)
area (Square _ _ l) = l * l
Haskell
                        not inheritance, but “alternatives” of cases of the type




data Shape = Circle Float Float Float |                                     Shape
            
             Rectangle Float Float Float Float |                     abstract area()


               Square Float Float Float


                                                                   Circle                             Rectangle

                                                             Circle(x, y, r)                     Rectangle(x, y, w, h)
area :: Shape -> Float

area (Circle _ _ r) = pi * r^2
                                                                                    Square
area (Rectangle _ _ w h) = w * h
                                                                               Square(x, y, l)
area (Square _ _ l) = l * l
Haskell
                        not inheritance, but “alternatives” of cases of the type




data Shape = Circle Float Float Float |                                     Shape
            
             Rectangle Float Float Float Float |                     abstract area()


               Square Float Float Float


                                                                   Circle                             Rectangle

                                                             Circle(x, y, r)                     Rectangle(x, y, w, h)
area :: Shape -> Float

area (Circle _ _ r) = pi * r^2
                                                                                    Square
area (Rectangle _ _ w h) = w * h
                                                                               Square(x, y, l)
area (Square _ _ l) = l * l
Haskell
                        not inheritance, but “alternatives” of cases of the type




data Shape = Circle Float Float Float |                                     Shape
            
             Rectangle Float Float Float Float |                     abstract area()


               Square Float Float Float


                                                                   Circle                             Rectangle

                                                             Circle(x, y, r)                     Rectangle(x, y, w, h)
area :: Shape -> Float

area (Circle _ _ r) = pi * r^2
                                                                                    Square
area (Rectangle _ _ w h) = w * h
                                                                               Square(x, y, l)
area (Square _ _ l) = l * l
                                      Algebraic Types
                         used to construct values for specific cases
Shapes in Scala
Shapes in Scala


sealed abstract class Shape
Shapes in Scala


sealed abstract class Shape
case class Circle( x: Int, y :Int, r :Int) extends Shape
Shapes in Scala


sealed abstract class Shape
case class Circle( x: Int, y :Int, r :Int) extends Shape
case class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape
Shapes in Scala


sealed abstract class Shape
case class Circle( x: Int, y :Int, r :Int) extends Shape
case class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape
case class Square(x :Int, y:Int, l :Int) extends Shape
Shapes in Scala

                                Algebraic Types
                   to describe how to construct alternatives
sealed abstract class Shape
case class Circle( x: Int, y :Int, r :Int) extends Shape
case class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape
case class Square(x :Int, y:Int, l :Int) extends Shape
Shapes in Scala

                                Algebraic Types
                   to describe how to construct alternatives
sealed abstract class Shape
case class Circle( x: Int, y :Int, r :Int) extends Shape
case class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape
case class Square(x :Int, y:Int, l :Int) extends Shape

                            Pattern Matching
                  to specify use cases based on values
Shapes in Scala

                                Algebraic Types
                   to describe how to construct alternatives
sealed abstract class Shape
case class Circle( x: Int, y :Int, r :Int) extends Shape
case class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape
case class Square(x :Int, y:Int, l :Int) extends Shape

                            Pattern Matching
                  to specify use cases based on values
val area : Shape => Int = _ match {




}
Shapes in Scala

                                Algebraic Types
                   to describe how to construct alternatives
sealed abstract class Shape
case class Circle( x: Int, y :Int, r :Int) extends Shape
case class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape
case class Square(x :Int, y:Int, l :Int) extends Shape

                             Pattern Matching
                   to specify use cases based on values
val area : Shape => Int = _ match {
    case Circle( _, _, radius ) => Pi * ( pow( radius, 2.0 ) )
    
     case Rectangle( _, _, width, height ) => width * height
    
     case Square( _, _, length ) => length * length


}
Shapes in Scala

                                    Algebraic Types
                       to describe how to construct alternatives
sealed abstract class Shape
case class Circle( x: Int, y :Int, r :Int) extends Shape
case class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape
case class Square(x :Int, y:Int, l :Int) extends Shape

                                Pattern Matching
                      to specify use cases based on values
val area : Shape => Int = _ match {
    case    Circle( _, _, radius ) => Pi * ( pow( radius, 2.0 ) )
    
     case   Rectangle( _, _, width, height ) => width * height
    
     case   Square( _, _, length ) => length * length
     case   Rectangle( _, _, 1, height ) => height
     case   Rectangle( _, _, width, 1 ) => width
 }
What has been our journey so far?
     Maybe we should apply it to something a bit more substantial




                      List Comprehension
What has been our journey so far?
     Maybe we should apply it to something a bit more substantial




                      List Comprehension




                     High Order Functions
What has been our journey so far?
     Maybe we should apply it to something a bit more substantial




                      List Comprehension




                     High Order Functions




                        Algebraic Types
What has been our journey so far?
     Maybe we should apply it to something a bit more substantial




                      List Comprehension




                     High Order Functions




                        Algebraic Types




                        Pattern Matching
A better model for a Project
        Using the composite pattern




                                              Activity




                     Phase        Milestone         Task   Project
A better model for a Project
           Using the composite pattern




                    Activity




Phase   Milestone           Task         Project
A better model for a Project
        Using the composite pattern




                                              Activity




                     Phase        Milestone         Task   Project
A better model for a Project
                                 Using the composite pattern



sealed abstract class Activity
                                                                       Activity




                                              Phase        Milestone         Task   Project
A better model for a Project
                                          Using the composite pattern



sealed abstract class Activity
                                                                                Activity


case class Task(description: String, person :String)
   extends Activity




                                                       Phase        Milestone         Task   Project
A better model for a Project
                                          Using the composite pattern



sealed abstract class Activity
                                                                                Activity


case class Task(description: String, person :String)
   extends Activity
case class Milestone(name :String)
   extends Activity



                                                       Phase        Milestone         Task   Project
A better model for a Project
                                          Using the composite pattern



sealed abstract class Activity
                                                                                Activity


case class Task(description: String, person :String)
   extends Activity
case class Milestone(name :String)
   extends Activity

case class Project(name :String) extends Activity
  lazy val activities = List (
    Phase(1),                                          Phase        Milestone         Task   Project
    Task("Develop", "Peter"),
    Milestone("Release to UAT"),
  )
}
A better model for a Project
                                          Using the composite pattern



sealed abstract class Activity
                                                                                Activity


case class Task(description: String, person :String)
   extends Activity
case class Milestone(name :String)
   extends Activity

case class Project(name :String) extends Activity
  lazy val activities = List (
    Phase(1),                                          Phase        Milestone         Task   Project
    Task("Develop", "Peter"),
    Milestone("Release to UAT"),
  )
}



case class Phase(phaseNumber :Int) extends Activity
A better model for a Project
                                          Using the composite pattern



sealed abstract class Activity
                                                                                Activity


case class Task(description: String, person :String)
   extends Activity
case class Milestone(name :String)
   extends Activity

case class Project(name :String) extends Activity
  lazy val activities = List (
    Phase(1),                                          Phase        Milestone         Task   Project
    Task("Develop", "Peter"),
    Milestone("Release to UAT"),
  )
}



case class Phase(phaseNumber :Int) extends Activity
A better model for a Project
                                              Using the composite pattern



sealed abstract class Activity
                                                                                    Activity
trait ManyActivities { def activities :Seq[Activity] }


case class Task(description: String, person :String)
   extends Activity
case class Milestone(name :String)
   extends Activity

case class Project(name :String) extends Activity
  lazy val activities = List (
    Phase(1),                                              Phase        Milestone         Task   Project
    Task("Develop", "Peter"),
    Milestone("Release to UAT"),
  )
}



case class Phase(phaseNumber :Int) extends Activity
A better model for a Project
                                              Using the composite pattern



sealed abstract class Activity
                                                                                    Activity
trait ManyActivities { def activities :Seq[Activity] }


case class Task(description: String, person :String)
   extends Activity
case class Milestone(name :String)
   extends Activity

case class Project(name :String) extends Activity
    with ManyActivities {
  lazy val activities = List (                             Phase        Milestone         Task   Project
    Phase(1),
    Task("Develop", "Peter"),
    Milestone("Release to UAT"),
  )
}


case class Phase(phaseNumber :Int) extends Activity
A better model for a Project
                                              Using the composite pattern



sealed abstract class Activity
                                                                                    Activity
trait ManyActivities { def activities :Seq[Activity] }


case class Task(description: String, person :String)
   extends Activity
case class Milestone(name :String)
   extends Activity

case class Project(name :String) extends Activity
    with ManyActivities {
  lazy val activities = List (                             Phase        Milestone         Task   Project
    Phase(1),
    Task("Develop", "Peter"),
    Milestone("Release to UAT"),
  )
}


case class Phase(phaseNumber :Int) extends Activity
   with ManyActivities {
    val activities = List(
      Task("Analysis", "John"),
      Milestone("Ready for Development")
    )
}
A better model for a Project
                                              Using the composite pattern



sealed abstract class Activity
                                                                                     Activity
trait ManyActivities { def activities :Seq[Activity] }


case class Task(description: String, person :String)
   extends Activity
case class Milestone(name :String)
   extends Activity

case class Project(name :String) extends Activity
    with ManyActivities {
  lazy val activities = List (                             Phase         Milestone         Task              Project
    Phase(1),
    Task("Develop", "Peter"),
    Milestone("Release to UAT"),
  )                                                                    unapply() is an Extractor for
}
                                                                             algebraic types
                                                              object ManyActivities {
case class Phase(phaseNumber :Int) extends Activity
                                                                def unapply(a :Activity) = {
   with ManyActivities {                                          a match {
    val activities = List(                                          case (a :ManyActivities) => Some(a.activities)
      Task("Analysis", "John"),                                     case _ => None
      Milestone("Ready for Development")                          }
    )                                                           }
}                                                             }
Actions for this project
            Using the visitor pattern



                        Activity

                    process (Action)




  Phase     Milestone            Task       Project



                        Action
                  for_Phase()
                  for_Milestone()
                  for_Task()
                  for_Project()




     Plan      Start            Status   Celebrate
Actions for this project
            Using the visitor pattern



                        Activity

                    process (Action)




  Phase     Milestone            Task       Project



                        Action
                  for_Phase()
                  for_Milestone()
                  for_Task()
                  for_Project()




     Plan      Start            Status   Celebrate
With functions
                                   we don’t need the object oriented visitor patterns



def process(action :Activity => Unit)(a :Activity) {                                     Activity

        a match {                                                                   process (Action)
         case ManyActivities(activities)
              => activities.foreach(process(action))

           case _ => ()

        }
        action(a)
    }                                                           Phase        Milestone            Task     Project
}


                                                                                         Action
                                                                                  for_Phase()
                                                                                  for_Milestone()
                                                                                  for_Task()
                                                                                  for_Project()




                                                                   Plan        Start            Status   Celebrate
With functions
                                   we don’t need the object oriented visitor patterns



def process(action :Activity => Unit)(a :Activity) {                                     Activity

        a match {                                                                   process (Action)
         case ManyActivities(activities)
              => activities.foreach(process(action))

           case _ => ()

        }
        action(a)
    }                                                           Phase        Milestone            Task     Project
}


                                                                                         Action
                                                                                  for_Phase()
                                                                                  for_Milestone()
                                                                                  for_Task()
                                                                                  for_Project()




                                                                   Plan        Start            Status   Celebrate
With functions
                                   we don’t need the object oriented visitor patterns



def process(action :Activity => Unit)(a :Activity) {                                     Activity

        a match {                                                                   process (Action)
         case ManyActivities(activities)
              => activities.foreach(process(action))

           case _ => ()

        }
        action(a)
    }                                                           Phase        Milestone            Task     Project
}


                                                                                         Action
                                                                                  for_Phase()
                                                                                  for_Milestone()
                                                                                  for_Task()
                                                                                  for_Project()




                                                                   Plan        Start            Status   Celebrate
With functions
                                   we don’t need the object oriented visitor patterns



def process(action :Activity => Unit)(a :Activity) {                                     Activity

        a match {                                                                   process (Action)
         case ManyActivities(activities)
              => activities.foreach(process(action))

           case _ => ()

        }
        action(a)
    }                                                           Phase        Milestone            Task     Project
}


                                                                                         Action
                                                                                  for_Phase()
                                                                                  for_Milestone()
                                                                                  for_Task()
                                                                                  for_Project()




                                                                   Plan        Start            Status   Celebrate
With functions
                                     we don’t need the object oriented visitor patterns



 def process(action :Activity => Unit)(a :Activity) {                                      Activity

         a match {                                                                    process (Action)
          case ManyActivities(activities)
               => activities.foreach(process(action))

            case _ => ()

         }
         action(a)
     }                                                            Phase        Milestone            Task     Project
 }


def plan(a :Activity) {                                                                    Action
     a match {                                                                      for_Phase()
                                                                                    for_Milestone()
         case Task(description, person) => { ... }
                                                                                    for_Task()
         case Phase(number) => { ... }                                              for_Project()
         case Milestone(name) => { ... }

         case Project(name) => { ... }

     }                                                               Plan        Start            Status   Celebrate
 }
With functions
                                     we don’t need the object oriented visitor patterns



 def process(action :Activity => Unit)(a :Activity) {                                      Activity

         a match {                                                                    process (Action)
          case ManyActivities(activities)
               => activities.foreach(process(action))

            case _ => ()

         }
         action(a)
     }                                                            Phase        Milestone            Task     Project
 }


def plan(a :Activity) {                                                                    Action
     a match {                                                                      for_Phase()
                                                                                    for_Milestone()
         case Task(description, person) => { ... }
                                                                                    for_Task()
         case Phase(number) => { ... }                                              for_Project()
         case Milestone(name) => { ... }

         case Project(name) => { ... }

     }                                                               Plan        Start            Status   Celebrate
 }
With functions
                                     we don’t need the object oriented visitor patterns



 def process(action :Activity => Unit)(a :Activity) {                                      Activity

         a match {                                                                    process (Action)
          case ManyActivities(activities)
               => activities.foreach(process(action))

            case _ => ()

         }
         action(a)
     }                                                            Phase        Milestone            Task     Project
 }


def plan(a :Activity) {                                                                    Action
     a match {                                                                      for_Phase()
                                                                                    for_Milestone()
         case Task(description, person) => { ... }
                                                                                    for_Task()
         case Phase(number) => { ... }                                              for_Project()
         case Milestone(name) => { ... }

         case Project(name) => { ... }

     }                                                               Plan        Start            Status   Celebrate
 }
With functions
                                     we don’t need the object oriented visitor patterns



 def process(action :Activity => Unit)(a :Activity) {                                      Activity

         a match {                                                                    process (Action)
          case ManyActivities(activities)
               => activities.foreach(process(action))

            case _ => ()

         }
         action(a)
     }                                                            Phase        Milestone            Task     Project
 }


def plan(a :Activity) {                                                                    Action
     a match {                                                                      for_Phase()
                                                                                    for_Milestone()
         case Task(description, person) => { ... }
                                                                                    for_Task()
         case Phase(number) => { ... }                                              for_Project()
         case Milestone(name) => { ... }

         case Project(name) => { ... }

     }                                                               Plan        Start            Status   Celebrate
 }
With functions
                                     we don’t need the object oriented visitor patterns



 def process(action :Activity => Unit)(a :Activity) {                                      Activity

         a match {                                                                    process (Action)
          case ManyActivities(activities)
               => activities.foreach(process(action))

            case _ => ()

         }
         action(a)
     }                                                            Phase        Milestone            Task     Project
 }


def plan(a :Activity) {                                                                    Action
     a match {                                                                      for_Phase()
                                                                                    for_Milestone()
         case Task(description, person) => { ... }
                                                                                    for_Task()
         case Phase(number) => { ... }                                              for_Project()
         case Milestone(name) => { ... }

         case Project(name) => { ... }

     }                                                               Plan        Start            Status   Celebrate
 }
With functions
                                     we don’t need the object oriented visitor patterns



 def process(action :Activity => Unit)(a :Activity) {                                      Activity

         a match {         val prj = Project("HelloWorld") (Action)
                                                       process
          case ManyActivities(activities)
               => activities.foreach(process(action))

            case _ => ()   process(plan)(prj)
         }
         action(a)
     }                     process(start)(prj) Phase                           Milestone            Task     Project
 }


def plan(a :Activity) {                                                                    Action
     a match {                                                                      for_Phase()
                                                                                    for_Milestone()
         case Task(description, person) => { ... }
                                                                                    for_Task()
         case Phase(number) => { ... }                                              for_Project()
         case Milestone(name) => { ... }

         case Project(name) => { ... }

     }                                                               Plan        Start            Status   Celebrate
 }
With functions
                                     we don’t need the object oriented visitor patterns



 def process(action :Activity => Unit)(a :Activity) {                                      Activity

         a match {         val prj = Project("HelloWorld") (Action)
                                                       process
          case ManyActivities(activities)
               => activities.foreach(process(action))

            case _ => ()   process(plan)(prj)
         }
         action(a)
     }                     process(start)(prj) Phase                           Milestone            Task     Project
 }


def plan(a :Activity) {                                                                    Action
     a match {                                                                      for_Phase()
                                                                                    for_Milestone()
         case Task(description, person) => { ... }
                                                                                    for_Task()
         case Phase(number) => { ... }                                              for_Project()
                                                   Next Steps
         case Milestone(name) => { ... }
                             Look at Haskell type classes and Scala implicit
         case Project(name) => { ... }   def for polymorphic functions
     }                                                               Plan        Start            Status   Celebrate
 }
Can you take the same journey with Clojure?
                             Use each step to build confidence, and go pure if you are stuck




                                                        Read about
                                                    functional concepts
                                                         in Haskell

                                                       do this slowly … don’t rush!




              Stuck ?                                                                         Try equivalent in Clojure

(impurity of Clojure) or (my ignorance) ?
                                                                                                  with some trivial example




                                                 Try it on your project


                                            the essential parts of the problem and solution
Some changes in thinking
                as a result of going on this functional journey




  Simple Structures                                 Simple Transformations
                                     and
lists and dictionaries                        list comprehensions, map, reduce
Some changes in thinking
                as a result of going on this functional journey




  Simple Structures                                 Simple Transformations
                                     and
lists and dictionaries                        list comprehensions, map, reduce



                  Find a starting set of elements
                         [ a, b, c, d, e, f ]
Some changes in thinking
                as a result of going on this functional journey




  Simple Structures                                 Simple Transformations
                                     and
lists and dictionaries                        list comprehensions, map, reduce



                  Find a starting set of elements
                         [ a, b, c, d, e, f ]



                    Transform to what you need
                       [ (a, b), (c,d) (e, f) ]
Some changes in thinking
                as a result of going on this functional journey




  Simple Structures                                 Simple Transformations
                                     and
lists and dictionaries                        list comprehensions, map, reduce



                  Find a starting set of elements
                         [ a, b, c, d, e, f ]



                    Transform to what you need
                       [ (a, b), (c,d) (e, f) ]



                    Filter to get what you want
                               [(c,d)]
Other things I work hard at

     Stop thinking about                      Remind myself of
                               and
interactions between objects         values of data and transformations
Other things I work hard at

     Stop thinking about                           Remind myself of
                                  and
interactions between objects              values of data and transformations




                        Keep it immutable with val
                          var feels dirty in Scala
Other things I work hard at

     Stop thinking about                           Remind myself of
                                  and
interactions between objects              values of data and transformations




                        Keep it immutable with val
                          var feels dirty in Scala



                        No unexpected side effects
Other things I work hard at

     Stop thinking about                           Remind myself of
                                  and
interactions between objects              values of data and transformations




                        Keep it immutable with val
                          var feels dirty in Scala



                        No unexpected side effects




                        Always return something
                     makes me consider every branch
Benefits that I’ve observed
   and some that I have an anticipation that will happen
Benefits that I’ve observed
   and some that I have an anticipation that will happen



                DRY, really DRY
              more than I expected
Benefits that I’ve observed
   and some that I have an anticipation that will happen



                DRY, really DRY
              more than I expected



               Very declarative
           makes it very readable too
Benefits that I’ve observed
   and some that I have an anticipation that will happen



                DRY, really DRY
              more than I expected



               Very declarative
           makes it very readable too



          Some OO patterns disappear
           into trivial implementations
Benefits that I’ve observed
   and some that I have an anticipation that will happen



                DRY, really DRY
              more than I expected



               Very declarative
           makes it very readable too



          Some OO patterns disappear
           into trivial implementations



                 Very testable
       small, immutable, side-effect free
Benefits that I’ve observed
   and some that I have an anticipation that will happen



                DRY, really DRY
              more than I expected



               Very declarative
           makes it very readable too



          Some OO patterns disappear
           into trivial implementations



                 Very testable
       small, immutable, side-effect free



                    Modular
           with excellent composition
How I now think about code



                  Primitive expressions
               food, I, put, my, mouth, in



                A means of combination
                I put food in my mouth



                 A means of abstraction
                         I eat


 This is the basics of understanding and organisation of thought
Programs are the same
they express our understanding and show the organisation of our thoughts
Programs are the same
they express our understanding and show the organisation of our thoughts




         Think
         large
         static
      structures
Programs are the same
they express our understanding and show the organisation of our thoughts




         Think
         large Think
         static small
      structures
              composable
               structures
Programs are the same
     they express our understanding and show the organisation of our thoughts




 Th ink
  la rge
  st atic
      ctu res                      or                   Think
stru
                                                        small
                                                    composable
                                                     structures
The language of functional
       And there are quite a few new concepts to understand



                                                      Currying
     Algebraic Types


Closures                                    Type Classes


    Monads                                            High Order Functions

       Pattern Matching                                   Monoids


                     Functors

  Lambdas                                  Polymorphic Functions

 List Comprehension
My functional learning loop
           Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track




                                          Read about
                                      functional concepts
                                           in Haskell

                                         do this slowly … don’t rush!




       Stuck ?                                                             Try the equivalent in Scala

(impurity) or (ignorance) ?
                                                                                with some trivial example




                                   Try it on your project


                              the essential parts of the problem and solution
What How do you want to build?
      factor10.com :: @aslamkhn :: aslamkhan.net

More Related Content

More from Aslam Khan

Beyond Apartheid and Democracy
Beyond Apartheid and DemocracyBeyond Apartheid and Democracy
Beyond Apartheid and DemocracyAslam Khan
 
Lean in Software Development
Lean in Software DevelopmentLean in Software Development
Lean in Software DevelopmentAslam Khan
 
Product Ownership - Leaders of Agile Webinar
Product Ownership - Leaders of Agile WebinarProduct Ownership - Leaders of Agile Webinar
Product Ownership - Leaders of Agile WebinarAslam Khan
 
Yet another building metaphor
Yet another building metaphorYet another building metaphor
Yet another building metaphorAslam Khan
 
Being in a State of REST
Being in a State of RESTBeing in a State of REST
Being in a State of RESTAslam Khan
 
Practical Scrum with Kent Beck (SD Times Webinar)
Practical Scrum with Kent Beck (SD Times Webinar)Practical Scrum with Kent Beck (SD Times Webinar)
Practical Scrum with Kent Beck (SD Times Webinar)Aslam Khan
 
Collaboration: TRC
Collaboration: TRCCollaboration: TRC
Collaboration: TRCAslam Khan
 

More from Aslam Khan (7)

Beyond Apartheid and Democracy
Beyond Apartheid and DemocracyBeyond Apartheid and Democracy
Beyond Apartheid and Democracy
 
Lean in Software Development
Lean in Software DevelopmentLean in Software Development
Lean in Software Development
 
Product Ownership - Leaders of Agile Webinar
Product Ownership - Leaders of Agile WebinarProduct Ownership - Leaders of Agile Webinar
Product Ownership - Leaders of Agile Webinar
 
Yet another building metaphor
Yet another building metaphorYet another building metaphor
Yet another building metaphor
 
Being in a State of REST
Being in a State of RESTBeing in a State of REST
Being in a State of REST
 
Practical Scrum with Kent Beck (SD Times Webinar)
Practical Scrum with Kent Beck (SD Times Webinar)Practical Scrum with Kent Beck (SD Times Webinar)
Practical Scrum with Kent Beck (SD Times Webinar)
 
Collaboration: TRC
Collaboration: TRCCollaboration: TRC
Collaboration: TRC
 

Recently uploaded

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Recently uploaded (20)

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

Not Quite Object Oriented

  • 1. 127.0.0.1 Aslam Khan :: @aslamkhn :: aslam.khan@factor10.com :: http://aslamkhan.net factor10
  • 2. Not Quite Object Oriented a frustrating and incomplete journey from OO to FP Aslam Khan :: @aslamkhn :: +Aslam Khan :: factor10.com :: aslamkhan.net factor10
  • 3. My journey in trying to write code it felt like a language journey, but it’s actually far from that
  • 4. My journey in trying to write code it felt like a language journey, but it’s actually far from that In the beginning, I used C
  • 5. My journey in trying to write code it felt like a language journey, but it’s actually far from that In the beginning, Procedural I used C C++
  • 6. My journey in trying to write code it felt like a language journey, but it’s actually far from that In the beginning, Procedural Procedural I used C C++ Java
  • 7. My journey in trying to write code it felt like a language journey, but it’s actually far from that Patterns In the beginning, Procedural Procedural and I used C C++ Java Polymorphism
  • 8. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns In the beginning, Procedural Procedural and Ruby I used C C++ Java Polymorphism C#
  • 9. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns Finally In the beginning, Procedural Procedural and Ruby Object I used C C++ Java Polymorphism Oriented! C#
  • 10. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns Finally In the beginning, Procedural Procedural and Ruby Object I used C C++ Java Polymorphism Oriented! C# then I had to write a parser in JavaScript
  • 11. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns Finally In the beginning, Procedural Procedural and Ruby Object I used C C++ Java Polymorphism Oriented! C# then I had to write a parser in JavaScript Ultra messy JavaScript
  • 12. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns Finally In the beginning, Procedural Procedural and Ruby Object I used C C++ Java Polymorphism Oriented! C# then I had to write a parser in JavaScript Ultra messy “Object Oriented” JavaScript JavaScript
  • 13. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns Finally In the beginning, Procedural Procedural and Ruby Object I used C C++ Java Polymorphism Oriented! C# then I had to write a parser in JavaScript Ultra messy “Object Oriented” Haskell Tutorial JavaScript JavaScript on Parsers
  • 14. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns Finally In the beginning, Procedural Procedural and Ruby Object I used C C++ Java Polymorphism Oriented! C# then I had to write a parser in JavaScript Ultra messy “Object Oriented” Haskell Tutorial More functional JavaScript JavaScript on Parsers JavaScript
  • 15. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns Finally In the beginning, Procedural Procedural and Ruby Object I used C C++ Java Polymorphism Oriented! C# then I had to write a parser in JavaScript Ultra messy “Object Oriented” Haskell Tutorial More functional JavaScript JavaScript on Parsers JavaScript inspired to be more functional
  • 16. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns Finally In the beginning, Procedural Procedural and Ruby Object I used C C++ Java Polymorphism Oriented! C# then I had to write a parser in JavaScript Ultra messy “Object Oriented” Haskell Tutorial More functional JavaScript JavaScript on Parsers JavaScript inspired to be more functional Object Oriented Scala
  • 17. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns Finally In the beginning, Procedural Procedural and Ruby Object I used C C++ Java Polymorphism Oriented! C# then I had to write a parser in JavaScript Ultra messy “Object Oriented” Haskell Tutorial More functional JavaScript JavaScript on Parsers JavaScript inspired to be more functional Object Oriented Read Haskell Scala
  • 18. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns Finally In the beginning, Procedural Procedural and Ruby Object I used C C++ Java Polymorphism Oriented! C# then I had to write a parser in JavaScript Ultra messy “Object Oriented” Haskell Tutorial More functional JavaScript JavaScript on Parsers JavaScript inspired to be more functional Object Oriented More Read Haskell Scala Functional Scala
  • 19. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns Finally In the beginning, Procedural Procedural and Ruby Object I used C C++ Java Polymorphism Oriented! C# then I had to write a parser in JavaScript Ultra messy “Object Oriented” Haskell Tutorial More functional JavaScript JavaScript on Parsers JavaScript inspired to be more functional Object Oriented More Read Haskell Clojure Scala Functional Scala
  • 20. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns Finally In the beginning, Procedural Procedural and Ruby Object I used C C++ Java Polymorphism Oriented! C# then I had to write a parser in JavaScript Ultra messy “Object Oriented” Haskell Tutorial More functional JavaScript JavaScript on Parsers JavaScript inspired to be more functional Object Oriented More Long mental Read Haskell Clojure Scala Functional Scala journey ...
  • 21. My functional learning loop Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track Read about functional concepts in Haskell do this slowly … don’t rush!
  • 22. My functional learning loop Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track Read about functional concepts in Haskell do this slowly … don’t rush! Try the equivalent in Scala with some trivial example
  • 23. My functional learning loop Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track Read about functional concepts in Haskell do this slowly … don’t rush! Try the equivalent in Scala with some trivial example Try it on your project the essential parts of the problem and solution
  • 24. My functional learning loop Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track Read about functional concepts in Haskell do this slowly … don’t rush! Stuck ? Try the equivalent in Scala (impurity) or (ignorance) ? with some trivial example Try it on your project the essential parts of the problem and solution
  • 25. My functional learning loop Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track Read about functional concepts in Haskell do this slowly … don’t rush! Stuck ? Try the equivalent in Scala (impurity) or (ignorance) ? with some trivial example Try it on your project the essential parts of the problem and solution
  • 26. The language of functional And there are quite a few new concepts to understand
  • 27. The language of functional And there are quite a few new concepts to understand Currying Algebraic Types Closures Type Classes Monads High Order Functions Pattern Matching Monoids Functors Lambdas Polymorphic Functions List Comprehension
  • 28. The language of functional And there are quite a few new concepts to understand Currying Closures Type Classes Monads Monoids Functors Lambdas Polymorphic Functions List Comprehension High Order Functions Algebraic Types Pattern Matching
  • 29. Let’s write some Java Find all even integers in a range List<Integer> xs = range(1,20); List<Integer> evenRange = evens(xs);
  • 30. Let’s write some Java Find all even integers in a range List<Integer> range(int start, int end) { List<Integer> xs = new ArrayList<Integer>(); for (int i = start; i < end + 1; i++) { xs.add(i); } return xs; } List<Integer> xs = range(1,20); List<Integer> evenRange = evens(xs);
  • 31. Let’s write some Java Find all even integers in a range List<Integer> range(int start, int end) { List<Integer> xs = new ArrayList<Integer>(); for (int i = start; i < end + 1; i++) { xs.add(i); } return xs; } List<Integer> xs = range(1,20); List<Integer> evenRange = evens(xs); List<Integer> evens(List<Integer> xs) { List<Integer> keep = new ArrayList<Integer>(); for (Integer x : xs) { if (isEven(x)) keep.add(x); } return keep; }
  • 32. Let’s write some Java Find all even integers in a range List<Integer> range(int start, int end) { List<Integer> xs = new ArrayList<Integer>(); for (int i = start; i < end + 1; i++) { xs.add(i); } return xs; } List<Integer> xs = range(1,20); List<Integer> evenRange = evens(xs); List<Integer> evens(List<Integer> xs) { List<Integer> keep = new ArrayList<Integer>(); for (Integer x : xs) { if (isEven(x)) keep.add(x); } return keep; } boolean isEven(Integer x) { return x % 2 == 0; }
  • 33. The Haskell way? Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ]
  • 34. The Haskell way? Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ] function evens
  • 35. The Haskell way? Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ] function evens applied to xs
  • 36. The Haskell way? Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ] function evens applied to xs is a list
  • 37. The Haskell way? Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ] function evens applied to xs is a list for each x in xs
  • 38. The Haskell way? Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ] function evens applied to xs is a list for each x in xs where x is even
  • 39. The Haskell way? Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ] function evens applied to xs is a list for each x in xs where x is even for example: evens [1..20]
  • 40. And in Scala list comprehensions are sometimes called for-comprehensions in Scala-land val evens = (xs: List[Int]) => for (x <- xs; even(x) ) yield x
  • 41. And in Scala list comprehensions are sometimes called for-comprehensions in Scala-land val evens = (xs: List[Int]) => for (x <- xs; even(x) ) yield x evens
  • 42. And in Scala list comprehensions are sometimes called for-comprehensions in Scala-land val evens = (xs: List[Int]) => for (x <- xs; even(x) ) yield x evens takes from a list xs
  • 43. And in Scala list comprehensions are sometimes called for-comprehensions in Scala-land val evens = (xs: List[Int]) => for (x <- xs; even(x) ) yield x evens takes from a list xs each x
  • 44. And in Scala list comprehensions are sometimes called for-comprehensions in Scala-land val evens = (xs: List[Int]) => for (x <- xs; even(x) ) yield x evens takes from a list xs each x in xs
  • 45. And in Scala list comprehensions are sometimes called for-comprehensions in Scala-land val evens = (xs: List[Int]) => for (x <- xs; even(x) ) yield x evens takes from a list xs each x in xs where x is even
  • 46. And in Scala list comprehensions are sometimes called for-comprehensions in Scala-land val evens = (xs: List[Int]) => for (x <- xs; even(x) ) yield x evens takes from a list xs each x in xs where x is even val even = (x:Int) => x % 2 == 0
  • 47. And in Clojure Let’s use the built in “filter” function (defn evens [xs] (filter even? xs))
  • 48. And in Clojure Let’s use the built in “filter” function (defn evens [xs] (filter even? xs)) evens
  • 49. And in Clojure Let’s use the built in “filter” function (defn evens [xs] (filter even? xs)) evens applied to a list xs
  • 50. And in Clojure Let’s use the built in “filter” function (defn evens [xs] (filter even? xs)) evens applied to a list xs applies the filter
  • 51. And in Clojure Let’s use the built in “filter” function (defn evens [xs] (filter even? xs)) evens applied to a list xs applies the filter even? on xs
  • 52. What if we have lots of “filters” Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ] odds xs = [ x | x <- xs, odd x ]
  • 53. What if we have lots of “filters” Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ] odds xs = [ x | x <- xs, odd x ]
  • 54. What if we have lots of “filters” Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ] odds xs = [ x | x <- xs, odd x ] abstract the parts that change find f xs = [ x | x <- xs, f x ]
  • 55. What if we have lots of “filters” Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ] odds xs = [ x | x <- xs, odd x ] abstract the parts that change find f xs = [ x | x <- xs, f x ] using filter f
  • 56. What if we have lots of “filters” Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ] odds xs = [ x | x <- xs, odd x ] abstract the parts that change find f xs = [ x | x <- xs, f x ] using filter f on a list xs
  • 57. What if we have lots of “filters” Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ] odds xs = [ x | x <- xs, odd x ] abstract the parts that change find f xs = [ x | x <- xs, f x ] using filter f on a list xs for each x in xs
  • 58. What if we have lots of “filters” Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ] odds xs = [ x | x <- xs, odd x ] abstract the parts that change find f xs = [ x | x <- xs, f x ] using filter f on a list xs for each x in xs where x satisfies the filter f
  • 59. High Order Functions helps you get DRY and leads to clean compositions val find = (predicate :Int => Boolean, xs :List[Int]) => for (x <- xs; if predicate(x)) yield x val even = (x :Int) => x % 2 == 0 val odd = (x :Int) => x % 2 == 1
  • 60. High Order Functions helps you get DRY and leads to clean compositions val find = (predicate :Int => Boolean, xs :List[Int]) => for (x <- xs; if predicate(x)) yield x use any function which we locally call predicate val even = (x :Int) => x % 2 == 0 val odd = (x :Int) => x % 2 == 1
  • 61. High Order Functions helps you get DRY and leads to clean compositions val find = (predicate :Int => Boolean, xs :List[Int]) => for (x <- xs; if predicate(x)) yield x use any function which we as long as the provided function takes an locally call predicate integer and returns a boolean val even = (x :Int) => x % 2 == 0 val odd = (x :Int) => x % 2 == 1
  • 62. High Order Functions helps you get DRY and leads to clean compositions val find = (predicate :Int => Boolean, xs :List[Int]) => for (x <- xs; if predicate(x)) yield x use any function which we as long as the provided function takes an locally call predicate integer and returns a boolean val even = (x :Int) => x % 2 == 0 val odd = (x :Int) => x % 2 == 1
  • 63. High Order Functions helps you get DRY and leads to clean compositions val find = (predicate :Int => Boolean, xs :List[Int]) => for (x <- xs; if predicate(x)) yield x use any function which we as long as the provided function takes an locally call predicate integer and returns a boolean val even = (x :Int) => x % 2 == 0 val odd = (x :Int) => x % 2 == 1 for example find(even, List.range(0,21)) find(odd, List.range(0,21))
  • 64. And in Clojure notice how we compose more powerful functions from smaller functions (defn find [predicate, xs] (filter predicate xs))
  • 65. And in Clojure notice how we compose more powerful functions from smaller functions (defn find [predicate, xs] (filter predicate xs)) function
  • 66. And in Clojure notice how we compose more powerful functions from smaller functions (defn find [predicate, xs] (filter predicate xs)) function for example (find odd? [1,2,3,4]) (find even? [1,2,3,4])
  • 67. Let’s apply this learning how can we model a few rules?
  • 68. Let’s apply this learning how can we model a few rules? Non Substitutable have to downcast to get the specific type
  • 69. Let’s apply this learning how can we model a few rules? Non Substitutable have to downcast to get the specific type
  • 70. Let’s apply this learning how can we model a few rules? Non Substitutable Semantically Wrong have to downcast to get how can a FixedTime the specific type project be over budget?
  • 71. It’s the rules that vary In Java, using the strategy patterns
  • 72. It’s the rules that vary In Java, using the strategy patterns class Project { public boolean check(CheckProjectRule rule) { return rule.check(this); } }
  • 73. It’s the rules that vary In Java, using the strategy patterns class Project { public boolean check(CheckProjectRule rule) { return rule.check(this); } } interface ProjectRule { public boolean check(Project p); }
  • 74. It’s the rules that vary In Java, using the strategy patterns class Project { public boolean check(CheckProjectRule rule) { return rule.check(this); } } interface ProjectRule { public boolean check(Project p); } class OverdueProjectRule implements ProjectRule { ... public boolean check(Project p) { return p.getEndDate().isBefore(today()); } }
  • 75. In Scala Using high order functions
  • 76. In Scala Using high order functions class Project { val check = (rule :Prj => Boolean) => rule(this) }
  • 77. In Scala Using high order functions class Project { val check = (rule :Prj => Boolean) => rule(this) } val overBudgetRule = (p :Project) => p.costToDate() > p.budget()
  • 78. In Scala Using high order functions class Project { val check = (rule :Prj => Boolean) => rule(this) } val overBudgetRule = (p :Project) => p.costToDate() > p.budget() val overdueProjectRule = (p :Project) => p.getEndDate().isBefore(today()) The Strategy Pattern is not needed because of high order functions
  • 79. A Model for Shapes Shape abstract area() Circle Rectangle Circle(x, y, r) Rectangle(x, y, w, h) Square Square(x, y, l) It’s easy to imagine what the Java code will look like
  • 80. Haskell not inheritance, but “alternatives” of cases of the type data Shape = Circle Float Float Float | Shape   Rectangle Float Float Float Float | abstract area() Square Float Float Float Circle Rectangle Circle(x, y, r) Rectangle(x, y, w, h) Square Square(x, y, l)
  • 81. Haskell not inheritance, but “alternatives” of cases of the type data Shape = Circle Float Float Float | Shape   Rectangle Float Float Float Float | abstract area() Square Float Float Float Circle Rectangle Circle(x, y, r) Rectangle(x, y, w, h) Square Square(x, y, l)
  • 82. Haskell not inheritance, but “alternatives” of cases of the type data Shape = Circle Float Float Float | Shape   Rectangle Float Float Float Float | abstract area() Square Float Float Float Circle Rectangle Circle(x, y, r) Rectangle(x, y, w, h) Square Square(x, y, l)
  • 83. Haskell not inheritance, but “alternatives” of cases of the type data Shape = Circle Float Float Float | Shape   Rectangle Float Float Float Float | abstract area() Square Float Float Float Circle Rectangle Circle(x, y, r) Rectangle(x, y, w, h) Square Square(x, y, l)
  • 84. Haskell not inheritance, but “alternatives” of cases of the type data Shape = Circle Float Float Float | Shape   Rectangle Float Float Float Float | abstract area() Square Float Float Float Circle Rectangle Circle(x, y, r) Rectangle(x, y, w, h) area :: Shape -> Float Square Square(x, y, l)
  • 85. Haskell not inheritance, but “alternatives” of cases of the type data Shape = Circle Float Float Float | Shape   Rectangle Float Float Float Float | abstract area() Square Float Float Float Circle Rectangle Circle(x, y, r) Rectangle(x, y, w, h) area :: Shape -> Float area (Circle _ _ r) = pi * r^2 Square area (Rectangle _ _ w h) = w * h Square(x, y, l) area (Square _ _ l) = l * l
  • 86. Haskell not inheritance, but “alternatives” of cases of the type data Shape = Circle Float Float Float | Shape   Rectangle Float Float Float Float | abstract area() Square Float Float Float Circle Rectangle Circle(x, y, r) Rectangle(x, y, w, h) area :: Shape -> Float area (Circle _ _ r) = pi * r^2 Square area (Rectangle _ _ w h) = w * h Square(x, y, l) area (Square _ _ l) = l * l
  • 87. Haskell not inheritance, but “alternatives” of cases of the type data Shape = Circle Float Float Float | Shape   Rectangle Float Float Float Float | abstract area() Square Float Float Float Circle Rectangle Circle(x, y, r) Rectangle(x, y, w, h) area :: Shape -> Float area (Circle _ _ r) = pi * r^2 Square area (Rectangle _ _ w h) = w * h Square(x, y, l) area (Square _ _ l) = l * l
  • 88. Haskell not inheritance, but “alternatives” of cases of the type data Shape = Circle Float Float Float | Shape   Rectangle Float Float Float Float | abstract area() Square Float Float Float Circle Rectangle Circle(x, y, r) Rectangle(x, y, w, h) area :: Shape -> Float area (Circle _ _ r) = pi * r^2 Square area (Rectangle _ _ w h) = w * h Square(x, y, l) area (Square _ _ l) = l * l Algebraic Types used to construct values for specific cases
  • 90. Shapes in Scala sealed abstract class Shape
  • 91. Shapes in Scala sealed abstract class Shape case class Circle( x: Int, y :Int, r :Int) extends Shape
  • 92. Shapes in Scala sealed abstract class Shape case class Circle( x: Int, y :Int, r :Int) extends Shape case class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape
  • 93. Shapes in Scala sealed abstract class Shape case class Circle( x: Int, y :Int, r :Int) extends Shape case class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape case class Square(x :Int, y:Int, l :Int) extends Shape
  • 94. Shapes in Scala Algebraic Types to describe how to construct alternatives sealed abstract class Shape case class Circle( x: Int, y :Int, r :Int) extends Shape case class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape case class Square(x :Int, y:Int, l :Int) extends Shape
  • 95. Shapes in Scala Algebraic Types to describe how to construct alternatives sealed abstract class Shape case class Circle( x: Int, y :Int, r :Int) extends Shape case class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape case class Square(x :Int, y:Int, l :Int) extends Shape Pattern Matching to specify use cases based on values
  • 96. Shapes in Scala Algebraic Types to describe how to construct alternatives sealed abstract class Shape case class Circle( x: Int, y :Int, r :Int) extends Shape case class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape case class Square(x :Int, y:Int, l :Int) extends Shape Pattern Matching to specify use cases based on values val area : Shape => Int = _ match { }
  • 97. Shapes in Scala Algebraic Types to describe how to construct alternatives sealed abstract class Shape case class Circle( x: Int, y :Int, r :Int) extends Shape case class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape case class Square(x :Int, y:Int, l :Int) extends Shape Pattern Matching to specify use cases based on values val area : Shape => Int = _ match {     case Circle( _, _, radius ) => Pi * ( pow( radius, 2.0 ) )      case Rectangle( _, _, width, height ) => width * height      case Square( _, _, length ) => length * length }
  • 98. Shapes in Scala Algebraic Types to describe how to construct alternatives sealed abstract class Shape case class Circle( x: Int, y :Int, r :Int) extends Shape case class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape case class Square(x :Int, y:Int, l :Int) extends Shape Pattern Matching to specify use cases based on values val area : Shape => Int = _ match {     case Circle( _, _, radius ) => Pi * ( pow( radius, 2.0 ) )      case Rectangle( _, _, width, height ) => width * height      case Square( _, _, length ) => length * length case Rectangle( _, _, 1, height ) => height case Rectangle( _, _, width, 1 ) => width }
  • 99. What has been our journey so far? Maybe we should apply it to something a bit more substantial List Comprehension
  • 100. What has been our journey so far? Maybe we should apply it to something a bit more substantial List Comprehension High Order Functions
  • 101. What has been our journey so far? Maybe we should apply it to something a bit more substantial List Comprehension High Order Functions Algebraic Types
  • 102. What has been our journey so far? Maybe we should apply it to something a bit more substantial List Comprehension High Order Functions Algebraic Types Pattern Matching
  • 103. A better model for a Project Using the composite pattern Activity Phase Milestone Task Project
  • 104. A better model for a Project Using the composite pattern Activity Phase Milestone Task Project
  • 105. A better model for a Project Using the composite pattern Activity Phase Milestone Task Project
  • 106. A better model for a Project Using the composite pattern sealed abstract class Activity Activity Phase Milestone Task Project
  • 107. A better model for a Project Using the composite pattern sealed abstract class Activity Activity case class Task(description: String, person :String) extends Activity Phase Milestone Task Project
  • 108. A better model for a Project Using the composite pattern sealed abstract class Activity Activity case class Task(description: String, person :String) extends Activity case class Milestone(name :String) extends Activity Phase Milestone Task Project
  • 109. A better model for a Project Using the composite pattern sealed abstract class Activity Activity case class Task(description: String, person :String) extends Activity case class Milestone(name :String) extends Activity case class Project(name :String) extends Activity lazy val activities = List ( Phase(1), Phase Milestone Task Project Task("Develop", "Peter"), Milestone("Release to UAT"), ) }
  • 110. A better model for a Project Using the composite pattern sealed abstract class Activity Activity case class Task(description: String, person :String) extends Activity case class Milestone(name :String) extends Activity case class Project(name :String) extends Activity lazy val activities = List ( Phase(1), Phase Milestone Task Project Task("Develop", "Peter"), Milestone("Release to UAT"), ) } case class Phase(phaseNumber :Int) extends Activity
  • 111. A better model for a Project Using the composite pattern sealed abstract class Activity Activity case class Task(description: String, person :String) extends Activity case class Milestone(name :String) extends Activity case class Project(name :String) extends Activity lazy val activities = List ( Phase(1), Phase Milestone Task Project Task("Develop", "Peter"), Milestone("Release to UAT"), ) } case class Phase(phaseNumber :Int) extends Activity
  • 112. A better model for a Project Using the composite pattern sealed abstract class Activity Activity trait ManyActivities { def activities :Seq[Activity] } case class Task(description: String, person :String) extends Activity case class Milestone(name :String) extends Activity case class Project(name :String) extends Activity lazy val activities = List ( Phase(1), Phase Milestone Task Project Task("Develop", "Peter"), Milestone("Release to UAT"), ) } case class Phase(phaseNumber :Int) extends Activity
  • 113. A better model for a Project Using the composite pattern sealed abstract class Activity Activity trait ManyActivities { def activities :Seq[Activity] } case class Task(description: String, person :String) extends Activity case class Milestone(name :String) extends Activity case class Project(name :String) extends Activity with ManyActivities { lazy val activities = List ( Phase Milestone Task Project Phase(1), Task("Develop", "Peter"), Milestone("Release to UAT"), ) } case class Phase(phaseNumber :Int) extends Activity
  • 114. A better model for a Project Using the composite pattern sealed abstract class Activity Activity trait ManyActivities { def activities :Seq[Activity] } case class Task(description: String, person :String) extends Activity case class Milestone(name :String) extends Activity case class Project(name :String) extends Activity with ManyActivities { lazy val activities = List ( Phase Milestone Task Project Phase(1), Task("Develop", "Peter"), Milestone("Release to UAT"), ) } case class Phase(phaseNumber :Int) extends Activity with ManyActivities { val activities = List( Task("Analysis", "John"), Milestone("Ready for Development") ) }
  • 115. A better model for a Project Using the composite pattern sealed abstract class Activity Activity trait ManyActivities { def activities :Seq[Activity] } case class Task(description: String, person :String) extends Activity case class Milestone(name :String) extends Activity case class Project(name :String) extends Activity with ManyActivities { lazy val activities = List ( Phase Milestone Task Project Phase(1), Task("Develop", "Peter"), Milestone("Release to UAT"), ) unapply() is an Extractor for } algebraic types object ManyActivities { case class Phase(phaseNumber :Int) extends Activity def unapply(a :Activity) = { with ManyActivities { a match { val activities = List( case (a :ManyActivities) => Some(a.activities) Task("Analysis", "John"), case _ => None Milestone("Ready for Development") } ) } } }
  • 116. Actions for this project Using the visitor pattern Activity process (Action) Phase Milestone Task Project Action for_Phase() for_Milestone() for_Task() for_Project() Plan Start Status Celebrate
  • 117. Actions for this project Using the visitor pattern Activity process (Action) Phase Milestone Task Project Action for_Phase() for_Milestone() for_Task() for_Project() Plan Start Status Celebrate
  • 118. With functions we don’t need the object oriented visitor patterns def process(action :Activity => Unit)(a :Activity) { Activity a match { process (Action) case ManyActivities(activities) => activities.foreach(process(action)) case _ => () } action(a) } Phase Milestone Task Project } Action for_Phase() for_Milestone() for_Task() for_Project() Plan Start Status Celebrate
  • 119. With functions we don’t need the object oriented visitor patterns def process(action :Activity => Unit)(a :Activity) { Activity a match { process (Action) case ManyActivities(activities) => activities.foreach(process(action)) case _ => () } action(a) } Phase Milestone Task Project } Action for_Phase() for_Milestone() for_Task() for_Project() Plan Start Status Celebrate
  • 120. With functions we don’t need the object oriented visitor patterns def process(action :Activity => Unit)(a :Activity) { Activity a match { process (Action) case ManyActivities(activities) => activities.foreach(process(action)) case _ => () } action(a) } Phase Milestone Task Project } Action for_Phase() for_Milestone() for_Task() for_Project() Plan Start Status Celebrate
  • 121. With functions we don’t need the object oriented visitor patterns def process(action :Activity => Unit)(a :Activity) { Activity a match { process (Action) case ManyActivities(activities) => activities.foreach(process(action)) case _ => () } action(a) } Phase Milestone Task Project } Action for_Phase() for_Milestone() for_Task() for_Project() Plan Start Status Celebrate
  • 122. With functions we don’t need the object oriented visitor patterns def process(action :Activity => Unit)(a :Activity) { Activity a match { process (Action) case ManyActivities(activities) => activities.foreach(process(action)) case _ => () } action(a) } Phase Milestone Task Project } def plan(a :Activity) { Action a match { for_Phase() for_Milestone() case Task(description, person) => { ... } for_Task() case Phase(number) => { ... } for_Project() case Milestone(name) => { ... } case Project(name) => { ... } } Plan Start Status Celebrate }
  • 123. With functions we don’t need the object oriented visitor patterns def process(action :Activity => Unit)(a :Activity) { Activity a match { process (Action) case ManyActivities(activities) => activities.foreach(process(action)) case _ => () } action(a) } Phase Milestone Task Project } def plan(a :Activity) { Action a match { for_Phase() for_Milestone() case Task(description, person) => { ... } for_Task() case Phase(number) => { ... } for_Project() case Milestone(name) => { ... } case Project(name) => { ... } } Plan Start Status Celebrate }
  • 124. With functions we don’t need the object oriented visitor patterns def process(action :Activity => Unit)(a :Activity) { Activity a match { process (Action) case ManyActivities(activities) => activities.foreach(process(action)) case _ => () } action(a) } Phase Milestone Task Project } def plan(a :Activity) { Action a match { for_Phase() for_Milestone() case Task(description, person) => { ... } for_Task() case Phase(number) => { ... } for_Project() case Milestone(name) => { ... } case Project(name) => { ... } } Plan Start Status Celebrate }
  • 125. With functions we don’t need the object oriented visitor patterns def process(action :Activity => Unit)(a :Activity) { Activity a match { process (Action) case ManyActivities(activities) => activities.foreach(process(action)) case _ => () } action(a) } Phase Milestone Task Project } def plan(a :Activity) { Action a match { for_Phase() for_Milestone() case Task(description, person) => { ... } for_Task() case Phase(number) => { ... } for_Project() case Milestone(name) => { ... } case Project(name) => { ... } } Plan Start Status Celebrate }
  • 126. With functions we don’t need the object oriented visitor patterns def process(action :Activity => Unit)(a :Activity) { Activity a match { process (Action) case ManyActivities(activities) => activities.foreach(process(action)) case _ => () } action(a) } Phase Milestone Task Project } def plan(a :Activity) { Action a match { for_Phase() for_Milestone() case Task(description, person) => { ... } for_Task() case Phase(number) => { ... } for_Project() case Milestone(name) => { ... } case Project(name) => { ... } } Plan Start Status Celebrate }
  • 127. With functions we don’t need the object oriented visitor patterns def process(action :Activity => Unit)(a :Activity) { Activity a match { val prj = Project("HelloWorld") (Action) process case ManyActivities(activities) => activities.foreach(process(action)) case _ => () process(plan)(prj) } action(a) } process(start)(prj) Phase Milestone Task Project } def plan(a :Activity) { Action a match { for_Phase() for_Milestone() case Task(description, person) => { ... } for_Task() case Phase(number) => { ... } for_Project() case Milestone(name) => { ... } case Project(name) => { ... } } Plan Start Status Celebrate }
  • 128. With functions we don’t need the object oriented visitor patterns def process(action :Activity => Unit)(a :Activity) { Activity a match { val prj = Project("HelloWorld") (Action) process case ManyActivities(activities) => activities.foreach(process(action)) case _ => () process(plan)(prj) } action(a) } process(start)(prj) Phase Milestone Task Project } def plan(a :Activity) { Action a match { for_Phase() for_Milestone() case Task(description, person) => { ... } for_Task() case Phase(number) => { ... } for_Project() Next Steps case Milestone(name) => { ... } Look at Haskell type classes and Scala implicit case Project(name) => { ... } def for polymorphic functions } Plan Start Status Celebrate }
  • 129. Can you take the same journey with Clojure? Use each step to build confidence, and go pure if you are stuck Read about functional concepts in Haskell do this slowly … don’t rush! Stuck ? Try equivalent in Clojure (impurity of Clojure) or (my ignorance) ? with some trivial example Try it on your project the essential parts of the problem and solution
  • 130. Some changes in thinking as a result of going on this functional journey Simple Structures Simple Transformations and lists and dictionaries list comprehensions, map, reduce
  • 131. Some changes in thinking as a result of going on this functional journey Simple Structures Simple Transformations and lists and dictionaries list comprehensions, map, reduce Find a starting set of elements [ a, b, c, d, e, f ]
  • 132. Some changes in thinking as a result of going on this functional journey Simple Structures Simple Transformations and lists and dictionaries list comprehensions, map, reduce Find a starting set of elements [ a, b, c, d, e, f ] Transform to what you need [ (a, b), (c,d) (e, f) ]
  • 133. Some changes in thinking as a result of going on this functional journey Simple Structures Simple Transformations and lists and dictionaries list comprehensions, map, reduce Find a starting set of elements [ a, b, c, d, e, f ] Transform to what you need [ (a, b), (c,d) (e, f) ] Filter to get what you want [(c,d)]
  • 134. Other things I work hard at Stop thinking about Remind myself of and interactions between objects values of data and transformations
  • 135. Other things I work hard at Stop thinking about Remind myself of and interactions between objects values of data and transformations Keep it immutable with val var feels dirty in Scala
  • 136. Other things I work hard at Stop thinking about Remind myself of and interactions between objects values of data and transformations Keep it immutable with val var feels dirty in Scala No unexpected side effects
  • 137. Other things I work hard at Stop thinking about Remind myself of and interactions between objects values of data and transformations Keep it immutable with val var feels dirty in Scala No unexpected side effects Always return something makes me consider every branch
  • 138. Benefits that I’ve observed and some that I have an anticipation that will happen
  • 139. Benefits that I’ve observed and some that I have an anticipation that will happen DRY, really DRY more than I expected
  • 140. Benefits that I’ve observed and some that I have an anticipation that will happen DRY, really DRY more than I expected Very declarative makes it very readable too
  • 141. Benefits that I’ve observed and some that I have an anticipation that will happen DRY, really DRY more than I expected Very declarative makes it very readable too Some OO patterns disappear into trivial implementations
  • 142. Benefits that I’ve observed and some that I have an anticipation that will happen DRY, really DRY more than I expected Very declarative makes it very readable too Some OO patterns disappear into trivial implementations Very testable small, immutable, side-effect free
  • 143. Benefits that I’ve observed and some that I have an anticipation that will happen DRY, really DRY more than I expected Very declarative makes it very readable too Some OO patterns disappear into trivial implementations Very testable small, immutable, side-effect free Modular with excellent composition
  • 144. How I now think about code Primitive expressions food, I, put, my, mouth, in A means of combination I put food in my mouth A means of abstraction I eat This is the basics of understanding and organisation of thought
  • 145. Programs are the same they express our understanding and show the organisation of our thoughts
  • 146. Programs are the same they express our understanding and show the organisation of our thoughts Think large static structures
  • 147. Programs are the same they express our understanding and show the organisation of our thoughts Think large Think static small structures composable structures
  • 148. Programs are the same they express our understanding and show the organisation of our thoughts Th ink la rge st atic ctu res or Think stru small composable structures
  • 149. The language of functional And there are quite a few new concepts to understand Currying Algebraic Types Closures Type Classes Monads High Order Functions Pattern Matching Monoids Functors Lambdas Polymorphic Functions List Comprehension
  • 150. My functional learning loop Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track Read about functional concepts in Haskell do this slowly … don’t rush! Stuck ? Try the equivalent in Scala (impurity) or (ignorance) ? with some trivial example Try it on your project the essential parts of the problem and solution
  • 151. What How do you want to build? factor10.com :: @aslamkhn :: aslamkhan.net

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. \n
  85. \n
  86. \n
  87. \n
  88. \n
  89. \n
  90. \n
  91. \n
  92. \n
  93. \n
  94. \n
  95. \n
  96. \n
  97. \n
  98. \n
  99. \n
  100. \n
  101. \n
  102. \n
  103. \n
  104. \n
  105. \n
  106. \n
  107. \n
  108. \n
  109. \n
  110. \n
  111. \n
  112. \n
  113. \n
  114. \n
  115. \n
  116. \n
  117. \n
  118. \n
  119. \n
  120. \n
  121. \n
  122. \n
  123. \n
  124. \n
  125. \n
  126. \n
  127. \n
  128. \n
  129. \n
  130. \n
  131. \n
  132. \n
  133. \n
  134. \n
  135. \n
  136. \n
  137. \n
  138. \n
  139. \n
  140. \n
  141. \n
  142. \n
  143. \n
  144. \n
  145. \n
  146. \n
  147. \n
  148. \n
  149. \n
  150. \n
  151. \n
  152. \n
  153. \n
  154. \n
  155. \n
  156. \n
  157. \n
  158. \n
  159. \n
  160. \n
  161. \n
  162. \n
  163. \n
  164. \n
  165. \n
  166. \n
  167. \n
  168. \n
  169. \n
  170. \n
  171. \n
  172. \n
  173. \n
  174. \n
  175. \n
  176. \n
  177. \n
  178. \n
  179. \n
  180. \n
  181. \n
  182. \n
  183. \n
  184. \n
  185. \n
  186. \n
  187. \n
  188. \n
  189. \n
  190. \n
  191. \n
  192. \n
  193. \n
  194. \n
  195. \n
  196. \n
  197. \n
  198. \n
  199. \n
  200. \n
  201. \n
  202. \n
  203. \n
  204. \n
  205. \n
  206. \n
  207. \n
  208. \n
  209. \n
  210. \n
  211. \n
  212. \n
  213. \n
  214. \n
  215. \n
  216. \n
  217. \n
  218. \n
  219. \n
  220. \n
  221. \n
  222. \n
  223. \n
  224. \n
  225. \n
  226. \n
  227. \n
  228. \n
  229. \n
  230. \n
  231. \n
  232. \n
  233. \n
  234. \n
  235. \n
  236. \n
  237. \n
  238. \n
  239. \n
  240. \n
  241. \n
  242. \n
  243. \n
  244. \n
  245. \n
  246. \n