why you should care about functional programming

37
Why you should care about Functional Programming Dhananjay Nene TechWeekend 5 December 18 th , 2010

Upload: dhananjay-nene

Post on 05-Dec-2014

2.542 views

Category:

Documents


5 download

DESCRIPTION

A introductory talk about the benefits of functional programming

TRANSCRIPT

Page 1: Why you should care about functional programming

Why you should care about Functional Programming

Dhananjay Nene

TechWeekend 5December 18th, 2010

Page 2: Why you should care about functional programming

What is functional programming about ?

Page 3: Why you should care about functional programming

Functions = input + transformation + output

Page 4: Why you should care about functional programming

def add(x,y) : return x + y

def double(x) : return 2 * x

Page 5: Why you should care about functional programming

NO side effects

Page 6: Why you should care about functional programming

Unit Testing or Integration Testingwhen NO side effects ?

Page 7: Why you should care about functional programming

Benefit 1 : Testability

Page 8: Why you should care about functional programming

Benefit 2 : Easier Integration

Page 9: Why you should care about functional programming

All variables are final and are an alias to the result of a computation

Page 10: Why you should care about functional programming

A variable thus cannot have a different value at different points in time

Page 11: Why you should care about functional programming

Benefit 3 : Concurrency (no locking)

Page 12: Why you should care about functional programming

Constructs to easily parallelise operations

Page 13: Why you should care about functional programming

Benefit 4 : Ability to leverage multicores (if applicable)

Page 14: Why you should care about functional programming

Higher Order Functions :Functions which take functions as parameters

Page 15: Why you should care about functional programming

def repeat(f,x) : return f(x,x)

double(x)

OR

repeat(add,x)

Page 16: Why you should care about functional programming

I like to imagine HOFs as policy injection

Page 17: Why you should care about functional programming

Brevity

Page 18: Why you should care about functional programming

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;}

Page 19: Why you should care about functional programming

qsort1 :: Ord a => [a] -> [a]qsort1 [] = []qsort1 (p:xs) = qsort1 lesser ++ [p] ++ qsort1 greater where lesser = filter (< p) xs greater = filter (>= p) xs

Page 20: Why you should care about functional programming

Think different :

Page 21: Why you should care about functional programming

Recursion or Comprehensions instead of Loops

Page 22: Why you should care about functional programming

Pattern Matching instead of if conditions

Page 23: Why you should care about functional programming

Pattern Matching instead of State Machines

Page 24: Why you should care about functional programming

Information Transformation instead of sequence of tasks

Page 25: Why you should care about functional programming

Persistent Data Structures

Page 26: Why you should care about functional programming

Powerful concurrency constructs : Actors

Page 27: Why you should care about functional programming

Software Transactional Memory

Page 28: Why you should care about functional programming

At what cost ?

Page 29: Why you should care about functional programming

Retraining the brain: Fixed Cost

(Remember OO?)

Page 30: Why you should care about functional programming

Actual development costs (variable) likelyto be lesser.

Could not find specific case studies.

Page 31: Why you should care about functional programming

Why ?

Page 32: Why you should care about functional programming

Programs are often smaller

Page 33: Why you should care about functional programming

Have a cleaner expression of intent(what instead of how)

Page 34: Why you should care about functional programming

Big ball of mud is harder to achieve with pure functions

Page 35: Why you should care about functional programming

Better future proofing in terms of beingable to leverage multicore

Page 36: Why you should care about functional programming

Once you are able to read code written in functional style, its a lot more enjoyable

Page 37: Why you should care about functional programming

Faster, better, (cheaper?) and Enjoyable!