Successfully reported this slideshow.
Upcoming SlideShare
×

# Why you should care about functional programming

2,473 views

Published on

A introductory talk about the benefits of functional programming

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### 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