Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

Why you should care about functional programming

2,119
views

Published on

A introductory talk about the benefits of functional programming

A introductory talk about the benefits of functional programming

1 Like
Statistics
Notes
• Full Name
Comment goes here.

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

Views
Total Views
2,119
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
10
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript

• 1. Why you should care about Functional Programming Dhananjay Nene TechWeekend 5 December 18th, 2010
• 2. What is functional programming about ?
• 3. Functions = input + transformation + output
• 4. def add(x,y) : return x + ydef double(x) : return 2 * x
• 5. NO side effects
• 6. Unit Testing or Integration Testing when NO side effects ?
• 7. Benefit 1 : Testability
• 8. Benefit 2 : Easier Integration
• 9. All variables are final and are an alias to the result of a computation
• 10. A variable thus cannot have a different value at different points in time
• 11. Benefit 3 : Concurrency (no locking)
• 12. Constructs to easily parallelise operations
• 13. Benefit 4 : Ability to leverage multicores (if applicable)
• 14. Higher Order Functions :Functions which take functions as parameters
• 15. def repeat(f,x) : return f(x,x) double(x) OR repeat(add,x)
• 16. I like to imagine HOFs as policy injection
• 17. Brevity
• 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. qsort1 :: Ord a => [a] -> [a]qsort1 [] = []qsort1 (p:xs) = qsort1 lesser ++ [p] ++ qsort1 greater where lesser = filter (< p) xs greater = filter (>= p) xs
• 20. Think different :
• 21. Recursion or Comprehensions instead of Loops
• 22. Pattern Matching instead of if conditions
• 23. Pattern Matching instead of State Machines