127.0.0.1Aslam Khan :: @aslamkhn :: aslam.khan@factor10.com :: http://aslamkhan.net   factor10
Not Quite Object Oriented                       a frustrating and incomplete journey from OO to FPAslam Khan :: @aslamkhn ...
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 thatIn t...
My journey in trying to write code                       it felt like a language journey, but it’s actually far from thatI...
My journey in trying to write code                       it felt like a language journey, but it’s actually far from thatI...
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 ...
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 ...
My journey in trying to write code                         it felt like a language journey, but it’s actually far from tha...
My journey in trying to write code                         it felt like a language journey, but it’s actually far from tha...
My journey in trying to write code                         it felt like a language journey, but it’s actually far from tha...
My journey in trying to write code                         it felt like a language journey, but it’s actually far from tha...
My journey in trying to write code                         it felt like a language journey, but it’s actually far from tha...
My journey in trying to write code                         it felt like a language journey, but it’s actually far from tha...
My journey in trying to write code                         it felt like a language journey, but it’s actually far from tha...
My journey in trying to write code                         it felt like a language journey, but it’s actually far from tha...
My journey in trying to write code                         it felt like a language journey, but it’s actually far from tha...
My journey in trying to write code                         it felt like a language journey, but it’s actually far from tha...
My functional learning loopDon’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track                ...
My functional learning loopDon’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track                ...
My functional learning loopDon’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track                ...
My functional learning loop           Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track     ...
My functional learning loop           Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track     ...
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                                     ...
The language of functional         And there are quite a few new concepts to understand                                   ...
Let’s write some Java      Find all even integers in a range   List<Integer> xs = range(1,20);   List<Integer> evenRange =...
Let’s write some Java                                       Find all even integers in a range                             ...
Let’s write some Java                                       Find all even integers in a range                             ...
Let’s write some Java                                       Find all even integers in a range                             ...
The Haskell way?Remember: We are using Haskell to understand functional conceptsevens xs = [ x | x <- xs, even x ]
The Haskell way?      Remember: We are using Haskell to understand functional concepts      evens xs = [ x | x <- xs, even...
The Haskell way?      Remember: We are using Haskell to understand functional concepts      evens xs = [ x | x <- xs, even...
The Haskell way?      Remember: We are using Haskell to understand functional concepts      evens xs = [ x | x <- xs, even...
The Haskell way?      Remember: We are using Haskell to understand functional concepts      evens xs = [ x | x <- xs, even...
The Haskell way?      Remember: We are using Haskell to understand functional concepts      evens xs = [ x | x <- xs, even...
The Haskell way?      Remember: We are using Haskell to understand functional concepts      evens xs = [ x | x <- xs, even...
And in Scala list comprehensions are sometimes called for-comprehensions in Scala-landval evens = (xs: List[Int]) =>   for...
And in Scala list comprehensions are sometimes called for-comprehensions in Scala-landval evens = (xs: List[Int]) =>   for...
And in Scala list comprehensions are sometimes called for-comprehensions in Scala-landval evens = (xs: List[Int]) =>   for...
And in Scala list comprehensions are sometimes called for-comprehensions in Scala-landval evens = (xs: List[Int]) =>   for...
And in Scala list comprehensions are sometimes called for-comprehensions in Scala-landval evens = (xs: List[Int]) =>   for...
And in Scala list comprehensions are sometimes called for-comprehensions in Scala-landval evens = (xs: List[Int]) =>   for...
And in Scala list comprehensions are sometimes called for-comprehensions in Scala-landval evens = (xs: List[Int]) =>   for...
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   applie...
And in Clojure                   Let’s use the built in “filter” function(defn evens [xs] (filter even? xs)) evens   applie...
And in Clojure                   Let’s use the built in “filter” function(defn evens [xs] (filter even? xs)) evens   applie...
What if we have lots of “filters”   Remember: We are using Haskell to understand functional conceptsevens xs = [ x | x <- ...
What if we have lots of “filters”   Remember: We are using Haskell to understand functional conceptsevens xs = [ x | x <- ...
What if we have lots of “filters”   Remember: We are using Haskell to understand functional conceptsevens xs = [ x | x <- ...
What if we have lots of “filters”         Remember: We are using Haskell to understand functional concepts evens xs = [ x ...
What if we have lots of “filters”         Remember: We are using Haskell to understand functional concepts evens xs = [ x ...
What if we have lots of “filters”         Remember: We are using Haskell to understand functional concepts evens xs = [ x ...
What if we have lots of “filters”         Remember: We are using Haskell to understand functional concepts evens xs = [ x ...
High Order Functions      helps you get DRY and leads to clean compositionsval find = (predicate :Int => Boolean, xs :List[...
High Order Functions       helps you get DRY and leads to clean compositionsval find = (predicate :Int => Boolean, xs :List...
High Order Functions       helps you get DRY and leads to clean compositionsval find = (predicate :Int => Boolean, xs :List...
High Order Functions       helps you get DRY and leads to clean compositionsval find = (predicate :Int => Boolean, xs :List...
High Order Functions       helps you get DRY and leads to clean compositionsval find = (predicate :Int => Boolean, xs :List...
And in Clojure       notice how we compose more powerful functions from smaller functions(defn find [predicate, xs] (filter ...
And in Clojure       notice how we compose more powerful functions from smaller functions(defn find [predicate, xs] (filter ...
And in Clojure       notice how we compose more powerful functions from smaller functions(defn find [predicate, xs] (filter ...
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     ...
Let’s apply this learning                          how can we model a few rules?   Non Substitutablehave to downcast to ge...
Let’s apply this learning                          how can we model a few rules?   Non Substitutable                      ...
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 {                    publi...
It’s the rules that vary    In Java, using the strategy patterns                  class Project {                    publi...
It’s the rules that vary    In Java, using the strategy patterns                  class Project {                    publi...
In ScalaUsing high order functions
In ScalaUsing high order functions     class Project {       val check = (rule :Prj => Boolean) => rule(this)     }
In ScalaUsing high order functions     class Project {       val check = (rule :Prj => Boolean) => rule(this)     }       ...
In ScalaUsing high order functions     class Project {       val check = (rule :Prj => Boolean) => rule(this)     }       ...
A Model for Shapes                             Shape                      abstract area()                    Circle       ...
Haskell                     not inheritance, but “alternatives” of cases of the typedata Shape = Circle Float Float Float ...
Haskell                     not inheritance, but “alternatives” of cases of the typedata Shape = Circle Float Float Float ...
Haskell                     not inheritance, but “alternatives” of cases of the typedata Shape = Circle Float Float Float ...
Haskell                     not inheritance, but “alternatives” of cases of the typedata Shape = Circle Float Float Float ...
Haskell                     not inheritance, but “alternatives” of cases of the typedata Shape = Circle Float Float Float ...
Haskell                        not inheritance, but “alternatives” of cases of the typedata Shape = Circle Float Float Flo...
Haskell                        not inheritance, but “alternatives” of cases of the typedata Shape = Circle Float Float Flo...
Haskell                        not inheritance, but “alternatives” of cases of the typedata Shape = Circle Float Float Flo...
Haskell                        not inheritance, but “alternatives” of cases of the typedata Shape = Circle Float Float Flo...
Shapes in Scala
Shapes in Scalasealed abstract class Shape
Shapes in Scalasealed abstract class Shapecase class Circle( x: Int, y :Int, r :Int) extends Shape
Shapes in Scalasealed abstract class Shapecase class Circle( x: Int, y :Int, r :Int) extends Shapecase class Rectangle(x: ...
Shapes in Scalasealed abstract class Shapecase class Circle( x: Int, y :Int, r :Int) extends Shapecase class Rectangle(x: ...
Shapes in Scala                                Algebraic Types                   to describe how to construct alternatives...
Shapes in Scala                                Algebraic Types                   to describe how to construct alternatives...
Shapes in Scala                                Algebraic Types                   to describe how to construct alternatives...
Shapes in Scala                                Algebraic Types                   to describe how to construct alternatives...
Shapes in Scala                                    Algebraic Types                       to describe how to construct alte...
What has been our journey so far?     Maybe we should apply it to something a bit more substantial                      Li...
What has been our journey so far?     Maybe we should apply it to something a bit more substantial                      Li...
What has been our journey so far?     Maybe we should apply it to something a bit more substantial                      Li...
What has been our journey so far?     Maybe we should apply it to something a bit more substantial                      Li...
A better model for a Project        Using the composite pattern                                              Activity     ...
A better model for a Project           Using the composite pattern                    ActivityPhase   Milestone           ...
A better model for a Project        Using the composite pattern                                              Activity     ...
A better model for a Project                                 Using the composite patternsealed abstract class Activity    ...
A better model for a Project                                          Using the composite patternsealed abstract class Act...
A better model for a Project                                          Using the composite patternsealed abstract class Act...
A better model for a Project                                          Using the composite patternsealed abstract class Act...
A better model for a Project                                          Using the composite patternsealed abstract class Act...
A better model for a Project                                          Using the composite patternsealed abstract class Act...
A better model for a Project                                              Using the composite patternsealed abstract class...
A better model for a Project                                              Using the composite patternsealed abstract class...
A better model for a Project                                              Using the composite patternsealed abstract class...
A better model for a Project                                              Using the composite patternsealed abstract class...
Actions for this project            Using the visitor pattern                        Activity                    process (...
Actions for this project            Using the visitor pattern                        Activity                    process (...
With functions                                   we don’t need the object oriented visitor patternsdef process(action :Act...
With functions                                   we don’t need the object oriented visitor patternsdef process(action :Act...
With functions                                   we don’t need the object oriented visitor patternsdef process(action :Act...
With functions                                   we don’t need the object oriented visitor patternsdef process(action :Act...
With functions                                     we don’t need the object oriented visitor patterns def process(action :...
With functions                                     we don’t need the object oriented visitor patterns def process(action :...
With functions                                     we don’t need the object oriented visitor patterns def process(action :...
With functions                                     we don’t need the object oriented visitor patterns def process(action :...
With functions                                     we don’t need the object oriented visitor patterns def process(action :...
With functions                                     we don’t need the object oriented visitor patterns def process(action :...
With functions                                     we don’t need the object oriented visitor patterns def process(action :...
Can you take the same journey with Clojure?                             Use each step to build confidence, and go pure if ...
Some changes in thinking                as a result of going on this functional journey  Simple Structures                ...
Some changes in thinking                as a result of going on this functional journey  Simple Structures                ...
Some changes in thinking                as a result of going on this functional journey  Simple Structures                ...
Some changes in thinking                as a result of going on this functional journey  Simple Structures                ...
Other things I work hard at     Stop thinking about                      Remind myself of                               an...
Other things I work hard at     Stop thinking about                           Remind myself of                            ...
Other things I work hard at     Stop thinking about                           Remind myself of                            ...
Other things I work hard at     Stop thinking about                           Remind myself of                            ...
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        ...
Benefits that I’ve observed   and some that I have an anticipation that will happen                DRY, really DRY        ...
Benefits that I’ve observed   and some that I have an anticipation that will happen                DRY, really DRY        ...
Benefits that I’ve observed   and some that I have an anticipation that will happen                DRY, really DRY        ...
Benefits that I’ve observed   and some that I have an anticipation that will happen                DRY, really DRY        ...
How I now think about code                  Primitive expressions               food, I, put, my, mouth, in               ...
Programs are the samethey express our understanding and show the organisation of our thoughts
Programs are the samethey express our understanding and show the organisation of our thoughts         Think         large ...
Programs are the samethey express our understanding and show the organisation of our thoughts         Think         large ...
Programs are the same     they express our understanding and show the organisation of our thoughts Th ink  la rge  st atic...
The language of functional       And there are quite a few new concepts to understand                                     ...
My functional learning loop           Don’t forget the Haskell talk by Simon Peyton Jones on the Cool Languages Track     ...
What How do you want to build?      factor10.com :: @aslamkhn :: aslamkhan.net
Upcoming SlideShare
Loading in...5
×

Not Quite Object Oriented

444

Published on

The JVM seems to have a fresh breeze blowing throw it with alternative languages like Groovy and Ruby. But for me, the standouts are Scala and Clojure. Many of us grew up with OO and Java was our language of expression. But Scala and Clojure are different. They have a functional side and expressing OO thoughts functionally is painful. We we will explore what it takes to shift your thinking gradually (not overnight) to take advantage of Scala and Clojure's functional side.

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
444
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Not Quite Object Oriented

    1. 1. 127.0.0.1Aslam Khan :: @aslamkhn :: aslam.khan@factor10.com :: http://aslamkhan.net factor10
    2. 2. Not Quite Object Oriented a frustrating and incomplete journey from OO to FPAslam Khan :: @aslamkhn :: +Aslam Khan :: factor10.com :: aslamkhan.net factor10
    3. 3. My journey in trying to write code it felt like a language journey, but it’s actually far from that
    4. 4. My journey in trying to write code it felt like a language journey, but it’s actually far from thatIn the beginning, I used C
    5. 5. My journey in trying to write code it felt like a language journey, but it’s actually far from thatIn the beginning, Procedural I used C C++
    6. 6. My journey in trying to write code it felt like a language journey, but it’s actually far from thatIn the beginning, Procedural Procedural I used C C++ Java
    7. 7. My journey in trying to write code it felt like a language journey, but it’s actually far from that PatternsIn the beginning, Procedural Procedural and I used C C++ Java Polymorphism
    8. 8. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java PatternsIn the beginning, Procedural Procedural and Ruby I used C C++ Java Polymorphism C#
    9. 9. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns FinallyIn the beginning, Procedural Procedural and Ruby Object I used C C++ Java Polymorphism Oriented! C#
    10. 10. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns FinallyIn 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. 11. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns FinallyIn 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. 12. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns FinallyIn 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. 13. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns FinallyIn 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. 14. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns FinallyIn 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. 15. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns FinallyIn 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. 16. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns FinallyIn 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. 17. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns FinallyIn 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. 18. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns FinallyIn 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. 19. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns FinallyIn 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. 20. My journey in trying to write code it felt like a language journey, but it’s actually far from that Java Patterns FinallyIn 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. 21. My functional learning loopDon’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. 22. My functional learning loopDon’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. 23. My functional learning loopDon’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. 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. 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. 26. The language of functional And there are quite a few new concepts to understand
    27. 27. The language of functional And there are quite a few new concepts to understand Currying Algebraic TypesClosures Type Classes Monads High Order Functions Pattern Matching Monoids Functors Lambdas Polymorphic Functions List Comprehension
    28. 28. The language of functional And there are quite a few new concepts to understand Currying Closures Type Classes Monads Monoids Functors Lambdas Polymorphic FunctionsList Comprehension High Order Functions Algebraic Types Pattern Matching
    29. 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. 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. 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. 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. 33. The Haskell way?Remember: We are using Haskell to understand functional conceptsevens xs = [ x | x <- xs, even x ]
    34. 34. The Haskell way? Remember: We are using Haskell to understand functional concepts evens xs = [ x | x <- xs, even x ]function evens
    35. 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. 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. 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. 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. 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. 40. And in Scala list comprehensions are sometimes called for-comprehensions in Scala-landval evens = (xs: List[Int]) => for (x <- xs; even(x) ) yield x
    41. 41. And in Scala list comprehensions are sometimes called for-comprehensions in Scala-landval evens = (xs: List[Int]) => for (x <- xs; even(x) ) yield x evens
    42. 42. And in Scala list comprehensions are sometimes called for-comprehensions in Scala-landval evens = (xs: List[Int]) => for (x <- xs; even(x) ) yield x evens takes from a list xs
    43. 43. And in Scala list comprehensions are sometimes called for-comprehensions in Scala-landval evens = (xs: List[Int]) => for (x <- xs; even(x) ) yield x evens takes from a list xs each x
    44. 44. And in Scala list comprehensions are sometimes called for-comprehensions in Scala-landval evens = (xs: List[Int]) => for (x <- xs; even(x) ) yield x evens takes from a list xs each x in xs
    45. 45. And in Scala list comprehensions are sometimes called for-comprehensions in Scala-landval 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. 46. And in Scala list comprehensions are sometimes called for-comprehensions in Scala-landval evens = (xs: List[Int]) => for (x <- xs; even(x) ) yield x evens takes from a list xs each x in xs where x is evenval even = (x:Int) => x % 2 == 0
    47. 47. And in Clojure Let’s use the built in “filter” function(defn evens [xs] (filter even? xs))
    48. 48. And in Clojure Let’s use the built in “filter” function(defn evens [xs] (filter even? xs)) evens
    49. 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. 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. 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. 52. What if we have lots of “filters” Remember: We are using Haskell to understand functional conceptsevens xs = [ x | x <- xs, even x ] odds xs = [ x | x <- xs, odd x ]
    53. 53. What if we have lots of “filters” Remember: We are using Haskell to understand functional conceptsevens xs = [ x | x <- xs, even x ] odds xs = [ x | x <- xs, odd x ]
    54. 54. What if we have lots of “filters” Remember: We are using Haskell to understand functional conceptsevens 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. 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. 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. 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. 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. 59. High Order Functions helps you get DRY and leads to clean compositionsval 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. 60. High Order Functions helps you get DRY and leads to clean compositionsval 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. 61. High Order Functions helps you get DRY and leads to clean compositionsval 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. 62. High Order Functions helps you get DRY and leads to clean compositionsval 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. 63. High Order Functions helps you get DRY and leads to clean compositionsval 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. 64. And in Clojure notice how we compose more powerful functions from smaller functions(defn find [predicate, xs] (filter predicate xs))
    65. 65. And in Clojure notice how we compose more powerful functions from smaller functions(defn find [predicate, xs] (filter predicate xs)) function
    66. 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. 67. Let’s apply this learning how can we model a few rules?
    68. 68. Let’s apply this learning how can we model a few rules? Non Substitutable have to downcast to get the specific type
    69. 69. Let’s apply this learning how can we model a few rules? Non Substitutablehave to downcast to get the specific type
    70. 70. Let’s apply this learning how can we model a few rules? Non Substitutable Semantically Wronghave to downcast to get how can a FixedTime the specific type project be over budget?
    71. 71. It’s the rules that vary In Java, using the strategy patterns
    72. 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. 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. 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. 75. In ScalaUsing high order functions
    76. 76. In ScalaUsing high order functions class Project { val check = (rule :Prj => Boolean) => rule(this) }
    77. 77. In ScalaUsing high order functions class Project { val check = (rule :Prj => Boolean) => rule(this) } val overBudgetRule = (p :Project) => p.costToDate() > p.budget()
    78. 78. In ScalaUsing 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. 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. 80. Haskell not inheritance, but “alternatives” of cases of the typedata 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. 81. Haskell not inheritance, but “alternatives” of cases of the typedata 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. 82. Haskell not inheritance, but “alternatives” of cases of the typedata 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. 83. Haskell not inheritance, but “alternatives” of cases of the typedata 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. 84. Haskell not inheritance, but “alternatives” of cases of the typedata 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. 85. Haskell not inheritance, but “alternatives” of cases of the typedata 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 -> Floatarea (Circle _ _ r) = pi * r^2 Squarearea (Rectangle _ _ w h) = w * h Square(x, y, l)area (Square _ _ l) = l * l
    86. 86. Haskell not inheritance, but “alternatives” of cases of the typedata 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 -> Floatarea (Circle _ _ r) = pi * r^2 Squarearea (Rectangle _ _ w h) = w * h Square(x, y, l)area (Square _ _ l) = l * l
    87. 87. Haskell not inheritance, but “alternatives” of cases of the typedata 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 -> Floatarea (Circle _ _ r) = pi * r^2 Squarearea (Rectangle _ _ w h) = w * h Square(x, y, l)area (Square _ _ l) = l * l
    88. 88. Haskell not inheritance, but “alternatives” of cases of the typedata 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 -> Floatarea (Circle _ _ r) = pi * r^2 Squarearea (Rectangle _ _ w h) = w * h Square(x, y, l)area (Square _ _ l) = l * l Algebraic Types used to construct values for specific cases
    89. 89. Shapes in Scala
    90. 90. Shapes in Scalasealed abstract class Shape
    91. 91. Shapes in Scalasealed abstract class Shapecase class Circle( x: Int, y :Int, r :Int) extends Shape
    92. 92. Shapes in Scalasealed abstract class Shapecase class Circle( x: Int, y :Int, r :Int) extends Shapecase class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shape
    93. 93. Shapes in Scalasealed abstract class Shapecase class Circle( x: Int, y :Int, r :Int) extends Shapecase class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shapecase class Square(x :Int, y:Int, l :Int) extends Shape
    94. 94. Shapes in Scala Algebraic Types to describe how to construct alternativessealed abstract class Shapecase class Circle( x: Int, y :Int, r :Int) extends Shapecase class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shapecase class Square(x :Int, y:Int, l :Int) extends Shape
    95. 95. Shapes in Scala Algebraic Types to describe how to construct alternativessealed abstract class Shapecase class Circle( x: Int, y :Int, r :Int) extends Shapecase class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shapecase class Square(x :Int, y:Int, l :Int) extends Shape Pattern Matching to specify use cases based on values
    96. 96. Shapes in Scala Algebraic Types to describe how to construct alternativessealed abstract class Shapecase class Circle( x: Int, y :Int, r :Int) extends Shapecase class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shapecase class Square(x :Int, y:Int, l :Int) extends Shape Pattern Matching to specify use cases based on valuesval area : Shape => Int = _ match {}
    97. 97. Shapes in Scala Algebraic Types to describe how to construct alternativessealed abstract class Shapecase class Circle( x: Int, y :Int, r :Int) extends Shapecase class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shapecase class Square(x :Int, y:Int, l :Int) extends Shape Pattern Matching to specify use cases based on valuesval area : Shape => Int = _ match {    case Circle( _, _, radius ) => Pi * ( pow( radius, 2.0 ) )     case Rectangle( _, _, width, height ) => width * height     case Square( _, _, length ) => length * length}
    98. 98. Shapes in Scala Algebraic Types to describe how to construct alternativessealed abstract class Shapecase class Circle( x: Int, y :Int, r :Int) extends Shapecase class Rectangle(x: Int, y :Int, w :Int, h :Int) extends Shapecase class Square(x :Int, y:Int, l :Int) extends Shape Pattern Matching to specify use cases based on valuesval 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. 99. What has been our journey so far? Maybe we should apply it to something a bit more substantial List Comprehension
    100. 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. 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. 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. 103. A better model for a Project Using the composite pattern Activity Phase Milestone Task Project
    104. 104. A better model for a Project Using the composite pattern ActivityPhase Milestone Task Project
    105. 105. A better model for a Project Using the composite pattern Activity Phase Milestone Task Project
    106. 106. A better model for a Project Using the composite patternsealed abstract class Activity Activity Phase Milestone Task Project
    107. 107. A better model for a Project Using the composite patternsealed abstract class Activity Activitycase class Task(description: String, person :String) extends Activity Phase Milestone Task Project
    108. 108. A better model for a Project Using the composite patternsealed abstract class Activity Activitycase class Task(description: String, person :String) extends Activitycase class Milestone(name :String) extends Activity Phase Milestone Task Project
    109. 109. A better model for a Project Using the composite patternsealed abstract class Activity Activitycase class Task(description: String, person :String) extends Activitycase class Milestone(name :String) extends Activitycase class Project(name :String) extends Activity lazy val activities = List ( Phase(1), Phase Milestone Task Project Task("Develop", "Peter"), Milestone("Release to UAT"), )}
    110. 110. A better model for a Project Using the composite patternsealed abstract class Activity Activitycase class Task(description: String, person :String) extends Activitycase class Milestone(name :String) extends Activitycase 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. 111. A better model for a Project Using the composite patternsealed abstract class Activity Activitycase class Task(description: String, person :String) extends Activitycase class Milestone(name :String) extends Activitycase 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. 112. A better model for a Project Using the composite patternsealed abstract class Activity Activitytrait ManyActivities { def activities :Seq[Activity] }case class Task(description: String, person :String) extends Activitycase class Milestone(name :String) extends Activitycase 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. 113. A better model for a Project Using the composite patternsealed abstract class Activity Activitytrait ManyActivities { def activities :Seq[Activity] }case class Task(description: String, person :String) extends Activitycase class Milestone(name :String) extends Activitycase 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. 114. A better model for a Project Using the composite patternsealed abstract class Activity Activitytrait ManyActivities { def activities :Seq[Activity] }case class Task(description: String, person :String) extends Activitycase class Milestone(name :String) extends Activitycase 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. 115. A better model for a Project Using the composite patternsealed abstract class Activity Activitytrait ManyActivities { def activities :Seq[Activity] }case class Task(description: String, person :String) extends Activitycase class Milestone(name :String) extends Activitycase 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. 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. 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. 118. With functions we don’t need the object oriented visitor patternsdef 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. 119. With functions we don’t need the object oriented visitor patternsdef 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. 120. With functions we don’t need the object oriented visitor patternsdef 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. 121. With functions we don’t need the object oriented visitor patternsdef 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. 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. 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. 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. 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. 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. 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. 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. 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. 130. Some changes in thinking as a result of going on this functional journey Simple Structures Simple Transformations andlists and dictionaries list comprehensions, map, reduce
    131. 131. Some changes in thinking as a result of going on this functional journey Simple Structures Simple Transformations andlists and dictionaries list comprehensions, map, reduce Find a starting set of elements [ a, b, c, d, e, f ]
    132. 132. Some changes in thinking as a result of going on this functional journey Simple Structures Simple Transformations andlists 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. 133. Some changes in thinking as a result of going on this functional journey Simple Structures Simple Transformations andlists 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. 134. Other things I work hard at Stop thinking about Remind myself of andinteractions between objects values of data and transformations
    135. 135. Other things I work hard at Stop thinking about Remind myself of andinteractions between objects values of data and transformations Keep it immutable with val var feels dirty in Scala
    136. 136. Other things I work hard at Stop thinking about Remind myself of andinteractions between objects values of data and transformations Keep it immutable with val var feels dirty in Scala No unexpected side effects
    137. 137. Other things I work hard at Stop thinking about Remind myself of andinteractions 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. 138. Benefits that I’ve observed and some that I have an anticipation that will happen
    139. 139. Benefits that I’ve observed and some that I have an anticipation that will happen DRY, really DRY more than I expected
    140. 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. 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. 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. 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. 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. 145. Programs are the samethey express our understanding and show the organisation of our thoughts
    146. 146. Programs are the samethey express our understanding and show the organisation of our thoughts Think large static structures
    147. 147. Programs are the samethey express our understanding and show the organisation of our thoughts Think large Think static small structures composable structures
    148. 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 Thinkstru small composable structures
    149. 149. The language of functional And there are quite a few new concepts to understand Currying Algebraic TypesClosures Type Classes Monads High Order Functions Pattern Matching Monoids Functors Lambdas Polymorphic Functions List Comprehension
    150. 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. 151. What How do you want to build? factor10.com :: @aslamkhn :: aslamkhan.net
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×