0
Upcoming SlideShare
×

# Functional Programming

194

Published on

Functional Programming

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

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

• Be the first to like this

Views
Total Views
194
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
5
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript of "Functional Programming"

1. 1. Functional Programming Contact me: hadoope@gmail.com Let’s Rock!
2. 2. Why FP ??? Single Threading can’t scale, but multi-ple threading is hard! Simple is Beautiful!
3. 3. Beautiful Concurrency ??? Thread Safe Synchronized Deadlock Livelock Races Debugging Mutable shared state!!!
4. 4. Functional Programming No Mutable
5. 5. In computer science, functional programming is a programming paradigm, a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids state and mutable data.
6. 6. First Class Function Higher-Order Function Purify Curry Lazy Evaluation Monoid Monad Tail Recursive
7. 7. First Class Function http://jsfiddle.net/hadoope/RAYy7/ Number Versus Function
8. 8. Higher-Order Function Functions that take other functions Functions that return other functions var people = [{name: "Fred", age: 65}, {name: "Lucy", age: 36}]; _.max(people, function(p) { return p.age }); function always(VALUE) { return function() { return VALUE; }; };
9. 9. Currying
10. 10. Currying using Lambda calculus (x, y) -> x * x + y *y x -> y -> x * x + y *y =
11. 11. Currying in JavaScript function curry3(fun) { return function(last) { return function(middle) { return function(first) { return fun(first, middle, last); }; }; }; }; function no_curry(fun, last, middle, first){ return fun(last, middle, first); } Define a Method Define a Method in Currying Way
12. 12. Recursion and Tail Recursive def factorial(n) if (n == 1) return 1 else return n* factorial(n-1) end end @tailrec def factorial(acc: Int, n:Int ) = { if (n <= 1) acc else factorial( n * acc, n - 1) }
13. 13. Lazy Evaluation In programming language theory, lazy evaluation, or call-by-need is an evaluation strategy which delays the evaluation of an expression until its value is needed (non-strict evaluation) and which also avoids repeated evaluations (sharing).
14. 14. Lazy Evaluation  Performance increases by avoiding needless calculations, and error conditions in evaluating compound expressions  The ability to construct potentially infinite data structures.  The ability to define control flow (structures) as abstractions instead of primitives
15. 15. Pattern Matching • Object Matching Import scala.util.Random var randomInt = new Random().nextInt(10) randomInt match { case 7 => println(“lucky seven”) case otherNumber => println( “get” + otherNumber) }
16. 16. Pattern Matching • Type Matching var items = List(1, “foo”, 3.5) for (item <- items) { item match { case i: Int => println(“got an Integer: ” + i) case s: String => println(“got a String: ” + s) case d: Double => println(“got a double: ” + d) case other => println(“got others” + other) } }
17. 17. Pattern Matching Functionalpolymorphism Versus OOpolymorphism
18. 18. Monoid Just what is a monoid, then? It is simply an implementation of an interface governed by some laws. Stated tersely, a monoid is a type together with an associative binary operation (op) which has an identity element (zero). trait Monoid[A] { def op(a1: A, a2: A): A def zero: A } def listMonoid[A] = new Monoid[List[A]] { def op(a1: List[A], a2: List[A]) = a1 ++ a2 def zero = Nil }
19. 19. Monoid will buy us ??? Associativity brings high parallelism op(a, op(b, op(c,d)))Folding to the right: After folding in parallel: op( op(a,b), op(c,d)) op(op(op(a, b), c), d)Folding to the right:
20. 20. Monad In functional programming, a monad is a structure that represents computations defined as sequences of steps. A type with a monad structure defines what it means to chain operations, or nest functions of that type together. This allows the programmer to build pipelines that process data in steps, in which each action is decorated with additional processing rules provided by the monad.
21. 21. Functional Javascript • Collection-Centric Programming
22. 22. Functional Javascript • Collection-Centric Programming (map, reduce and filter) http://jsfiddle.net/hadoope/xhQ4P/
23. 23. Monad using Javascript var stack = []; stack.push(4); stack.push(5); stack.pop(); // 5 stack.pop(); // 4 Normal Style http://igstan.ro/posts/2011-05-02-understanding-monads-with-javascript.html