functional programming
DESCRIPTION
A small overview of functional programming.TRANSCRIPT
Is OOPs, oops?• NullPointer problems• Inheritance issues• “new” operator issues
Is OOPs, oops?• NullPointer problems• Inheritance issues• “new” operator issues
Is OOPs, oops?• NullPointer problems• Inheritance issues• “new” operator issues
Why FP?• I Have to Be Good at Writing Concurrent Programs• Most Programs Are Just Data Management Problems
(why ORM?)• More Modular• I Have to Work Faster and Faster• Code is written once, but read many times
Why FP?• I Have to Be Good at Writing Concurrent Programs• Most Programs Are Just Data Management Problems
(why ORM?)• More Modular• I Have to Work Faster and Faster• Code is written once, but read many times
Why FP?• I Have to Be Good at Writing Concurrent Programs• Most Programs Are Just Data Management Problems
(why ORM?)• More Modular• I Have to Work Faster and Faster• Code is written once, but read many times
Why FP?• I Have to Be Good at Writing Concurrent Programs• Most Programs Are Just Data Management Problems
(why ORM?)• More Modular• I Have to Work Faster and Faster• Code is written once, but read many times
Why FP?• I Have to Be Good at Writing Concurrent Programs• Most Programs Are Just Data Management Problems
(why ORM?)• More Modular• I Have to Work Faster and Faster• Code is written once, but read many times
What is FP?• RECURSION• ABSTRACTION• HIGHER ORDER FUNCTIONS
• IMPACT MOST PROGRAMMING LANGUAGES
PROGRAMS AS FUNCTIONS• PROGRAM = DESCRIPTION OF A SPECIFIC
COMPUTATION• Y = F(X)• F: X ->Y
• MATHEMATICS• VARIABLES = ACTUAL VALUES• NO MEMEORY ALLOCATION CONCEPT
• IMPERATIVE LANGUAGE• VARIABLES = MEMORY LOCATIONS + VALUES
PROGRAMS AS FUNCTIONS• NO LOOPS BUT RECURSION
• NO VARIABLE EXCEPT AS A NAME FOR A VALUE
• NO ASSIGNMENT OPERATION (x = x+1 , MEANINGLESS)
• ONLY CONSTANTS, PARAMETERS AND VALUES
EXAMPLE
Void GCD (int u, int v, int* x) {
Int y, t, z;
z = u;
y = v;
While (y!=0) {
…
…
}
…
}
Void GCD(int u, int v) {
if(v==0)
return u;
else
return GCD(v, u%v);
}
imperative functional
NO VARIABLES, NO ASSIGNMENT
• No notion of the internal state of a function.• Referential Transparency.• Value Semantics.
FP vs Others• Recursions instead of loops• Pattern matching instead of “if”• Pattern matching instead of state machines• Information transformation instead of sequence of tasks
FP vs Others• Persistent data structures• Powerful concurrency constructs : Actors• Software transactional memory• Avoid “Null”
What it really means?• Immutability is good
What it really means?• Immutability is good• No bugs (due to nasty side effects)
What it really means?• Immutability is good• No bugs (due to nasty side effects)• Benefits from concurrency
What it really means?• Immutability is good• No bugs (due to nasty side effects)• Benefits from concurrency• No more messy loops
What it really means?• Immutability is good• No bugs (due to nasty side effects)• Benefits from concurrency• No more messy loops• Lazy evaluation
FP Examples• Erlang, Haskell, Clojure• F#• JAVA 8 (prject lambda), Scala, Groovy• R, Mathematica etc. (specialized languages)
CAUTION CAUTION
Maintaining, Maintainability• Use functional style where\till it makes the intent more
readable.
Sort(extract(filter(having(on(Pet.class).getSpecies(), is(Dog)), pets, on(Pet.class).getName()), on(String.class));
List<Pet> dogs = filter(having(on(Pet.class).getSpecies(), is(Dog)), pets);List<String> dogNames = extract(dogs, on(Pet.class).getName());List<String> sortedDogNames = sort(dogNames, on(String.class));
Maintaining, Maintainability• One liners are always not better.
Private Converter<Pet, VetStay> toVetStay () {@override public VetStay converter(Pet pet) {
return new VetStay(pet, new Date(), “….”);}});
Convert(pets, new Convert<Pet, VetStay>() {@override public VetStay converter(Pet pet) {
return new VetStay(pet, new Date(), “….”);}});
Convert(pets, toVetStay());
FP - Adoption• Facebook (tchat), Linkedin uses Erlang• Twitter, UBS, Credit Suisse uses Scala
QUESTIONS?Questions?
FEEDBACKFeedback