29
Functional Programming Contact me: [email protected] Let’s Rock!

Functional Programming

Embed Size (px)

DESCRIPTION

Functional Programming

Citation preview

Page 1: Functional Programming

Functional Programming

Contact me: [email protected]

Let’s Rock!

Page 2: Functional Programming

Why FP ???

Single Threading can’t scale, but multi-ple threading is hard!

Simple is Beautiful!

Page 3: Functional Programming

Beautiful Concurrency ???

Thread Safe

Synchronized Deadlock

Livelock

Races Debugging

Mutable shared state!!!

Page 4: Functional Programming

Functional Programming No Mutable

Page 5: Functional Programming

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

First Class Function Higher-Order Function

Purify

Curry

Lazy Evaluation

Monoid

Monad

Tail Recursive

Page 7: Functional Programming

First Class Function

http://jsfiddle.net/hadoope/RAYy7/

Number Versus Function

Page 8: Functional Programming

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

Currying

Page 10: Functional Programming

Currying using Lambda calculus

(x, y) -> x * x + y *y

x -> y -> x * x + y *y

=

Page 11: Functional Programming

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

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

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

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

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

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

Pattern Matching

Functional polymorphism

Versus

OO polymorphism

Page 18: Functional Programming

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

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

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

Functional Javascript

• Collection-Centric Programming

Page 22: Functional Programming

Functional Javascript

• Collection-Centric Programming (map, reduce and filter)

http://jsfiddle.net/hadoope/xhQ4P/

Page 23: Functional Programming

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

Monad using Javascript

http://jsfiddle.net/hadoope/FcD5S/

Continuation-Passing Style

Page 25: Functional Programming

Monad using Javascript

• Currying push and pop

http://jsfiddle.net/hadoope/62bfe/2/

Page 26: Functional Programming

Monad using Javascript

• Preparing bind to Handle Intermediate Stacks

http://jsfiddle.net/hadoope/Lsgw5/

Page 27: Functional Programming

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 28: Functional Programming

https://www.coursera.org/course/progfun

Page 29: Functional Programming

Thank You

29