Download - Functional programming
![Page 2: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/2.jpg)
Is OOPs, oops?• NullPointer problems• Inheritance issues• “new” operator issues
![Page 3: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/3.jpg)
Is OOPs, oops?• NullPointer problems• Inheritance issues• “new” operator issues
![Page 4: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/4.jpg)
Is OOPs, oops?• NullPointer problems• Inheritance issues• “new” operator issues
![Page 5: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/5.jpg)
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
![Page 6: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/6.jpg)
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
![Page 7: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/7.jpg)
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
![Page 8: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/8.jpg)
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
![Page 9: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/9.jpg)
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
![Page 10: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/10.jpg)
What is FP?• RECURSION• ABSTRACTION• HIGHER ORDER FUNCTIONS
• IMPACT MOST PROGRAMMING LANGUAGES
![Page 11: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/11.jpg)
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
![Page 12: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/12.jpg)
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
![Page 13: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/13.jpg)
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
![Page 14: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/14.jpg)
NO VARIABLES, NO ASSIGNMENT
• No notion of the internal state of a function.• Referential Transparency.• Value Semantics.
![Page 15: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/15.jpg)
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
![Page 16: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/16.jpg)
FP vs Others• Persistent data structures• Powerful concurrency constructs : Actors• Software transactional memory• Avoid “Null”
![Page 17: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/17.jpg)
What it really means?• Immutability is good
![Page 18: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/18.jpg)
What it really means?• Immutability is good• No bugs (due to nasty side effects)
![Page 19: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/19.jpg)
What it really means?• Immutability is good• No bugs (due to nasty side effects)• Benefits from concurrency
![Page 20: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/20.jpg)
What it really means?• Immutability is good• No bugs (due to nasty side effects)• Benefits from concurrency• No more messy loops
![Page 21: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/21.jpg)
What it really means?• Immutability is good• No bugs (due to nasty side effects)• Benefits from concurrency• No more messy loops• Lazy evaluation
![Page 22: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/22.jpg)
FP Examples• Erlang, Haskell, Clojure• F#• JAVA 8 (prject lambda), Scala, Groovy• R, Mathematica etc. (specialized languages)
![Page 23: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/23.jpg)
CAUTION CAUTION
![Page 24: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/24.jpg)
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));
![Page 25: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/25.jpg)
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());
![Page 26: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/26.jpg)
FP - Adoption• Facebook (tchat), Linkedin uses Erlang• Twitter, UBS, Credit Suisse uses Scala
![Page 27: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/27.jpg)
QUESTIONS?Questions?
![Page 28: Functional programming](https://reader035.vdocuments.net/reader035/viewer/2022081413/54907a06b47959a4028b4a16/html5/thumbnails/28.jpg)
FEEDBACKFeedback