Why you should care about Functional Programming       Dhananjay Nene       TechWeekend 5     December 18th, 2010
What is functional programming about ?
Functions = input + transformation + output
def add(x,y) : return x + ydef double(x) : return 2 * x
NO side effects
Unit Testing or Integration Testing     when NO side effects ?
Benefit 1 : Testability
Benefit 2 : Easier Integration
All variables are final and are an alias to the            result of a computation
A variable thus cannot have a different value at             different points in time
Benefit 3 : Concurrency (no locking)
Constructs to easily parallelise operations
Benefit 4 : Ability to leverage multicores              (if applicable)
Higher Order Functions :Functions which take functions as parameters
def repeat(f,x) : return f(x,x)          double(x)             OR        repeat(add,x)
I like to imagine HOFs as policy injection
Brevity
public static <E extends Comparable<? super E>> List<E> quickSort(List<E> arr) {  if (arr.size() <= 1)      return arr;  E...
qsort1 :: Ord a => [a] -> [a]qsort1 [] = []qsort1 (p:xs) = qsort1 lesser ++ [p] ++ qsort1 greater  where     lesser = filt...
Think different :
Recursion or Comprehensions instead of Loops
Pattern Matching instead of if conditions
Pattern Matching instead of State Machines
Information Transformation instead of sequence                    of tasks
Persistent Data Structures
Powerful concurrency constructs : Actors
Software Transactional Memory
At what cost ?
Retraining the brain: Fixed Cost        (Remember OO?)
Actual development costs (variable) likely              to be lesser.  Could not find specific case studies.
Why ?
Programs are often smaller
Have a cleaner expression of intent      (what instead of how)
Big ball of mud is harder to achieve with pure                   functions
Better future proofing in terms of being       able to leverage multicore
Once you are able to read code written in functional style, its a lot more enjoyable
Faster, better, (cheaper?) and Enjoyable!
Upcoming SlideShare
Loading in …5
×

Why you should care about functional programming

2,401 views

Published on

A introductory talk about the benefits of functional programming

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,401
On SlideShare
0
From Embeds
0
Number of Embeds
23
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Why you should care about functional programming

  1. 1. Why you should care about Functional Programming Dhananjay Nene TechWeekend 5 December 18th, 2010
  2. 2. What is functional programming about ?
  3. 3. Functions = input + transformation + output
  4. 4. def add(x,y) : return x + ydef double(x) : return 2 * x
  5. 5. NO side effects
  6. 6. Unit Testing or Integration Testing when NO side effects ?
  7. 7. Benefit 1 : Testability
  8. 8. Benefit 2 : Easier Integration
  9. 9. All variables are final and are an alias to the result of a computation
  10. 10. A variable thus cannot have a different value at different points in time
  11. 11. Benefit 3 : Concurrency (no locking)
  12. 12. Constructs to easily parallelise operations
  13. 13. Benefit 4 : Ability to leverage multicores (if applicable)
  14. 14. Higher Order Functions :Functions which take functions as parameters
  15. 15. def repeat(f,x) : return f(x,x) double(x) OR repeat(add,x)
  16. 16. I like to imagine HOFs as policy injection
  17. 17. Brevity
  18. 18. public static <E extends Comparable<? super E>> List<E> quickSort(List<E> arr) { if (arr.size() <= 1) return arr; E pivot = arr.getFirst(); //This pivot can change to get faster results List<E> less = new LinkedList<E>(); List<E> pivotList = new LinkedList<E>(); List<E> more = new LinkedList<E>(); // Partition for (E i: arr) { if (i.compareTo(pivot) < 0) less.add(i); else if (i.compareTo(pivot) > 0) more.add(i); else pivotList.add(i); } // Recursively sort sublists less = quickSort(less); more = quickSort(more); // Concatenate results less.addAll(pivotList); less.addAll(more); return less;}
  19. 19. qsort1 :: Ord a => [a] -> [a]qsort1 [] = []qsort1 (p:xs) = qsort1 lesser ++ [p] ++ qsort1 greater where lesser = filter (< p) xs greater = filter (>= p) xs
  20. 20. Think different :
  21. 21. Recursion or Comprehensions instead of Loops
  22. 22. Pattern Matching instead of if conditions
  23. 23. Pattern Matching instead of State Machines
  24. 24. Information Transformation instead of sequence of tasks
  25. 25. Persistent Data Structures
  26. 26. Powerful concurrency constructs : Actors
  27. 27. Software Transactional Memory
  28. 28. At what cost ?
  29. 29. Retraining the brain: Fixed Cost (Remember OO?)
  30. 30. Actual development costs (variable) likely to be lesser. Could not find specific case studies.
  31. 31. Why ?
  32. 32. Programs are often smaller
  33. 33. Have a cleaner expression of intent (what instead of how)
  34. 34. Big ball of mud is harder to achieve with pure functions
  35. 35. Better future proofing in terms of being able to leverage multicore
  36. 36. Once you are able to read code written in functional style, its a lot more enjoyable
  37. 37. Faster, better, (cheaper?) and Enjoyable!

×