SlideShare uma empresa Scribd logo
1 de 29
Baixar para ler offline
Functional Programming
Contact me: hadoope@gmail.com
Let’s Rock!
Why FP ???
Single Threading can’t scale,
but multi-ple threading is hard!
Simple is Beautiful!
Beautiful Concurrency ???
Thread Safe
Synchronized
Deadlock
Livelock
Races
Debugging
Mutable shared state!!!
Functional Programming
No Mutable
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.
First Class Function
Higher-Order Function
Purify
Curry
Lazy Evaluation
Monoid
Monad
Tail Recursive
First Class Function
http://jsfiddle.net/hadoope/RAYy7/
Number Versus Function
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;
};
};
Currying
Currying using Lambda calculus
(x, y) -> x * x + y *y
x -> y -> x * x + y *y
=
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
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)
}
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).
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
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)
}
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)
}
}
Pattern Matching
Functionalpolymorphism
Versus
OOpolymorphism
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
}
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:
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.
Functional Javascript
• Collection-Centric Programming
Functional Javascript
• Collection-Centric Programming (map, reduce
and filter)
http://jsfiddle.net/hadoope/xhQ4P/
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
Monad using Javascript
http://jsfiddle.net/hadoope/FcD5S/
Continuation-Passing Style
Monad using Javascript
• Currying push and pop
http://jsfiddle.net/hadoope/62bfe/2/
Monad using Javascript
• Preparing bind to Handle Intermediate Stacks
http://jsfiddle.net/hadoope/Lsgw5/
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)
https://www.coursera.org/course/progfun
Thank You
29

Mais conteúdo relacionado

Mais procurados (20)

Chapter 7: Queue data structure
Chapter 7:  Queue data structureChapter 7:  Queue data structure
Chapter 7: Queue data structure
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
 
List
ListList
List
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
 
Chapter 5: linked list data structure
Chapter 5: linked list data structureChapter 5: linked list data structure
Chapter 5: linked list data structure
 
Algorithms: II
Algorithms: IIAlgorithms: II
Algorithms: II
 
02 Stack
02 Stack02 Stack
02 Stack
 
String predefined functions in C programming
String predefined functions in C  programmingString predefined functions in C  programming
String predefined functions in C programming
 
Scilab
Scilab Scilab
Scilab
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Queue-Data Structure
Queue-Data StructureQueue-Data Structure
Queue-Data Structure
 
multiple linear regression
multiple linear regressionmultiple linear regression
multiple linear regression
 
Stack
StackStack
Stack
 
Queue
QueueQueue
Queue
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Array within a class
Array within a classArray within a class
Array within a class
 
R: Apply Functions
R: Apply FunctionsR: Apply Functions
R: Apply Functions
 
Queue
QueueQueue
Queue
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 

Semelhante a Functional Programming

Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software DevelopmentNaveenkumar Muguda
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAdam Getchell
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioLuis Atencio
 
Pydiomatic
PydiomaticPydiomatic
Pydiomaticrik0
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingRichardWarburton
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With ScalaKnoldus Inc.
 
The Fuss about || Haskell | Scala | F# ||
The Fuss about || Haskell | Scala | F# ||The Fuss about || Haskell | Scala | F# ||
The Fuss about || Haskell | Scala | F# ||Ashwin Rao
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with ScalaNeelkanth Sachdeva
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming LanguagesConcepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languagesppd1961
 
JS Responsibilities
JS ResponsibilitiesJS Responsibilities
JS ResponsibilitiesBrendan Eich
 
Scala clojure techday_2011
Scala clojure techday_2011Scala clojure techday_2011
Scala clojure techday_2011Thadeu Russo
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java ProgrammersEric Pederson
 
Standardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonStandardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonRalf Gommers
 
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdfconceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdfSahajShrimal1
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingShine Xavier
 

Semelhante a Functional Programming (20)

Functional programming in C++
Functional programming in C++Functional programming in C++
Functional programming in C++
 
Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software Development
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis Atencio
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Pydiomatic
PydiomaticPydiomatic
Pydiomatic
 
Python idiomatico
Python idiomaticoPython idiomatico
Python idiomatico
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
The Fuss about || Haskell | Scala | F# ||
The Fuss about || Haskell | Scala | F# ||The Fuss about || Haskell | Scala | F# ||
The Fuss about || Haskell | Scala | F# ||
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming LanguagesConcepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languages
 
JS Responsibilities
JS ResponsibilitiesJS Responsibilities
JS Responsibilities
 
Scala clojure techday_2011
Scala clojure techday_2011Scala clojure techday_2011
Scala clojure techday_2011
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 
Functional programming 101
Functional programming 101Functional programming 101
Functional programming 101
 
Standardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonStandardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for Python
 
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdfconceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
conceptsinobjectorientedprogramminglanguages-12659959597745-phpapp02.pdf
 
Fuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional ProgrammingFuel Up JavaScript with Functional Programming
Fuel Up JavaScript with Functional Programming
 

Último

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 

Último (20)

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Functional Programming

  • 1. Functional Programming Contact me: hadoope@gmail.com Let’s Rock!
  • 2. Why FP ??? Single Threading can’t scale, but multi-ple threading is hard! Simple is Beautiful!
  • 3. Beautiful Concurrency ??? Thread Safe Synchronized Deadlock Livelock Races Debugging Mutable shared state!!!
  • 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. First Class Function Higher-Order Function Purify Curry Lazy Evaluation Monoid Monad Tail Recursive
  • 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; }; };
  • 10. Currying using Lambda calculus (x, y) -> x * x + y *y x -> y -> x * x + y *y =
  • 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. 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. 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. 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. 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. 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) } }
  • 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. 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. 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.
  • 22. Functional Javascript • Collection-Centric Programming (map, reduce and filter) http://jsfiddle.net/hadoope/xhQ4P/
  • 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
  • 25. Monad using Javascript • Currying push and pop http://jsfiddle.net/hadoope/62bfe/2/
  • 26. Monad using Javascript • Preparing bind to Handle Intermediate Stacks http://jsfiddle.net/hadoope/Lsgw5/
  • 27. 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)