Functional Programming
-
Upload
yuan-wang -
Category
Technology
-
view
171 -
download
0
description
Transcript of Functional Programming
![Page 2: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/2.jpg)
Why FP ???
Single Threading can’t scale, but multi-ple threading is hard!
Simple is Beautiful!
![Page 3: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/3.jpg)
Beautiful Concurrency ???
Thread Safe
Synchronized Deadlock
Livelock
Races Debugging
Mutable shared state!!!
![Page 4: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/4.jpg)
Functional Programming No Mutable
![Page 5: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/5.jpg)
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.
![Page 6: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/6.jpg)
First Class Function Higher-Order Function
Purify
Curry
Lazy Evaluation
Monoid
Monad
Tail Recursive
![Page 7: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/7.jpg)
First Class Function
http://jsfiddle.net/hadoope/RAYy7/
Number Versus Function
![Page 8: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/8.jpg)
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; }; };
![Page 9: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/9.jpg)
Currying
![Page 10: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/10.jpg)
Currying using Lambda calculus
(x, y) -> x * x + y *y
x -> y -> x * x + y *y
=
![Page 11: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/11.jpg)
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
![Page 12: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/12.jpg)
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) }
![Page 13: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/13.jpg)
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).
![Page 14: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/14.jpg)
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
![Page 15: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/15.jpg)
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) }
![Page 16: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/16.jpg)
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) } }
![Page 17: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/17.jpg)
Pattern Matching
Functional polymorphism
Versus
OO polymorphism
![Page 18: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/18.jpg)
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 }
![Page 19: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/19.jpg)
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:
![Page 20: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/20.jpg)
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.
![Page 21: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/21.jpg)
Functional Javascript
• Collection-Centric Programming
![Page 22: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/22.jpg)
Functional Javascript
• Collection-Centric Programming (map, reduce and filter)
http://jsfiddle.net/hadoope/xhQ4P/
![Page 23: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/23.jpg)
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
![Page 24: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/24.jpg)
Monad using Javascript
http://jsfiddle.net/hadoope/FcD5S/
Continuation-Passing Style
![Page 25: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/25.jpg)
Monad using Javascript
• Currying push and pop
http://jsfiddle.net/hadoope/62bfe/2/
![Page 26: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/26.jpg)
Monad using Javascript
• Preparing bind to Handle Intermediate Stacks
http://jsfiddle.net/hadoope/Lsgw5/
![Page 27: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/27.jpg)
Monad Using Haskell
computation = push 4 >>= \_ -> push 5 >>= \_ -> pop >> \a -> pop >>= \b-> return $ (show a) ++ “ : ” ++ (show b)
Computation = do push 4 push 5 a <- pop b <- pop return $ (show a) ++ “ : “ ++ (show b)
![Page 29: Functional Programming](https://reader031.fdocuments.in/reader031/viewer/2022020217/554f41e3b4c90572088b53a9/html5/thumbnails/29.jpg)
Thank You
29