Functional Programming Tommy “is awesome” Montgomery 2009-03-06
What is functional programming? Uses functions Lambda Calculus Very academic Kinda goofy
Elements of functional languages Recursion Functions (der) No state
Functional Languages Haskell Erlang F# OCaml Scheme Smalltalk J K Mathematica XSLT LISP (kinda)
Fun Fact #1 Tommy was once a professional musician
λ -c alculus Anonymous functions JavaScript PHP 4.0.1 – PHP 5.2.x (kinda) PHP 5.3 (more kinda) C# 2.0 Java sucks, as usual Unary Functions take one argument, and return one value
Example λ  x . x + 2
Example λ  x . x + 2   input
Example λ  x . x + 2   input return value
Example λ  x . x + 2   input return value f(x) = x + 2
Higher order functions Functions that take functions as arguments and return functions Where the real power of functional programming lies
Higher order function example Mathematical derivative
Higher order function example //f is a function function  derivative ( f )   { return  function ( x )   { //approximation of derivative return   ( f ( x  +   0.00001 )   –  f ( x ))   /   0.00001 ; } }
Higher order function example //evaluate derivative of x 2 : var  deriv_x_squared  =  derivative ( function ( x ) { return  x * x ; } ); alert ( deriv_x_squared ( 3 ));  //alerts 6ish
Fun Fact #2 Tommy used to be a gangsta
Bound variables The  λ  operator binds its variables to its scope All other variables are “free” Freedom  
Bound variables λ  x . x + y
Bound variables λ  x . x + y   bound variable bound variable free variable
Bound variables λ  x . x + y   bound variable bound variable free variable f(x) = x + y
Natural numbers Everything is a function, remember? How do we define the natural numbers in functional programming (1, 2, 3, …)?
very painfully
Church numerals 0  ≡  λ  f .  λ  x . x 1  ≡   λ  f .  λ  x . f x 2  ≡   λ  f .  λ  x . f (f x) 3  ≡   λ  f .  λ  x . f (f (f x)) ...
Church numerals in JavaScript //identity function //   λ  x . x function  identity ( x ) { return  x ; } //Church numeral zero //  λ  f .   λ  x . x function  zero ( f ) { return  identity ; }
Church numerals in JavaScript //successor function (succ(n) = n + 1) //  λ  n .  λ  f . f (n f x) function  succ ( n ) { return   function ( f ) { return   function ( x ) { return  f ( n ( f )( x )); } } }
Church numerals in JavaScript //gets a function representing //the nth church number function  getChurchNumber ( n ) { var  ret  =  zero ; for   ( var  i  =   0 ;  i  <  n ;  i ++) { ret  =  succ ( ret ); } return  ret ; }
Church numerals in JavaScript //gets the nth church number function  getNaturalNumber ( n ) { var  value  =   0 ; for   ( var  i  =   0 ;  i  <  n ;  i ++) { value  +=  getChurchNumber ( i )( function ( x ) { return  x  +   1 ; } )( 0 ); } return  value ; }
Fun fact #3 Wombats are unfairly cute
Addition //  λ  m .  λ  n .  λ  f . n f (m f x) function  add ( m ) { return   function ( n ) { return   function ( f ) { return   function ( x ) { return  n ( f )( m ( f )( x )); } } } }
Addition using successor //  λ  m .  λ  n . m succ n function  addWithSucc ( m ) { return   function ( n ) { return   m ( succ )( n ); } }
Holy crap http://uxul.wordpress.com/2009/03/02/generating-church-numbers-with-javascript/
Recursion Local variables are the devil! Also, a paradox In lambda calculus, you cannot define a function that includes itself i.e. the definition of recursion
The Y Combinator Cool name Also known as “The Paradoxical Operator”
Recursion To properly define recursion, a recursive function  g  must take as an argument a function  f , which expands to  g  which takes an argument  f .
Got that?
Recursion f = g(f) f  is a  fixed point  of  g Known as the Y-Combinator
The Y-Combinator Y = λ g . (λ x . g (x x)) (λ x . g (x x))
The Y-Combinator in JavaScript function  Y ( g ) { return   function ( x ) { return  g ( function ( y ) { return  x ( x )( y ); } ); }( function ( x ) { return  g ( function ( y ) { return  x ( x )( y ); } ); } ); } http://matt.might.net/articles/implementation-of-recursive-fixed-point-y-combinator-in-javascript-for-memoization/
Functional Factorial Y factorial n Y  : definition of Y-Combinator factorial : functional definition of factorial n  : integer
Functional Factorial function  factorial ( f ) { return   function ( n ) { return  ( n  ==  0 ) ?  1  :  n  *  f ( n  –  1 ); } } //call like so: alert ( Y ( factorial )( 5 ));  //alerts 120
What is happening? Recursive calls are abstracted to an inner function, which is evaluated as a lambda function (which by very definition are not recursive, remember?) Interesting, but not all that useful…
 
Other topics… Currying Mapping Reduction Substitution Elaboration on statelessness Imperative vs. Functional Performance
 
Questions…

More Related Content

PPTX
Functional programming in JavaScript
PDF
Intro to Functional Programming
PPTX
Functional Programming in Javascript - IL Tech Talks week
PDF
Reasoning about laziness
PDF
Functional Programming with JavaScript
PPTX
Functional Programming in JavaScript by Luis Atencio
PDF
Thinking in Functions: Functional Programming in Python
PDF
Learning Functional Programming Without Growing a Neckbeard
Functional programming in JavaScript
Intro to Functional Programming
Functional Programming in Javascript - IL Tech Talks week
Reasoning about laziness
Functional Programming with JavaScript
Functional Programming in JavaScript by Luis Atencio
Thinking in Functions: Functional Programming in Python
Learning Functional Programming Without Growing a Neckbeard

What's hot (20)

PDF
Scala categorytheory
PDF
Functional Programming Patterns (BuildStuff '14)
ODP
Clojure basics
ODP
Functional Programming With Scala
PDF
A taste of Functional Programming
PDF
Practical Functional Programming Presentation by Bogdan Hodorog
PPT
Scala functions
PDF
Introduction to functional programming (In Arabic)
PDF
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
PDF
Functional programming in Python
PDF
Introduction to functional programming using Ocaml
PPTX
Functional programming
PDF
Functional programming ii
PPTX
Functions in c++
PPTX
Functional programming
PPTX
An Introduction to Functional Programming with Javascript
PDF
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
PDF
Introduction to functional programming
PDF
Demystifying functional programming with Scala
PDF
Implicit conversion and parameters
Scala categorytheory
Functional Programming Patterns (BuildStuff '14)
Clojure basics
Functional Programming With Scala
A taste of Functional Programming
Practical Functional Programming Presentation by Bogdan Hodorog
Scala functions
Introduction to functional programming (In Arabic)
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Functional programming in Python
Introduction to functional programming using Ocaml
Functional programming
Functional programming ii
Functions in c++
Functional programming
An Introduction to Functional Programming with Javascript
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Introduction to functional programming
Demystifying functional programming with Scala
Implicit conversion and parameters

Viewers also liked (12)

PDF
Functional JavaScript Fundamentals
PDF
Functional Programming in JavaScript
PPTX
Functional Programming with JavaScript
PDF
Interactive Scientific Image Analysis using Spark
PDF
Functional programming
PPTX
Functional Programming Fundamentals
PDF
Lambda Calculus by Dustin Mulcahey
KEY
Machine Learning with Apache Mahout
PPTX
Functional programming
PDF
The Lambda Calculus and The JavaScript
PDF
Modeling with Hadoop kdd2011
PDF
Predictive Analytics Project in Automotive Industry
Functional JavaScript Fundamentals
Functional Programming in JavaScript
Functional Programming with JavaScript
Interactive Scientific Image Analysis using Spark
Functional programming
Functional Programming Fundamentals
Lambda Calculus by Dustin Mulcahey
Machine Learning with Apache Mahout
Functional programming
The Lambda Calculus and The JavaScript
Modeling with Hadoop kdd2011
Predictive Analytics Project in Automotive Industry

Similar to Introduction to Functional Programming in JavaScript (20)

PDF
TI1220 Lecture 6: First-class Functions
PDF
Composition birds-and-recursion
PPTX
Functional Programming Concepts for Imperative Programmers
PPT
User defined functions
ODP
Functors, applicatives, monads
PDF
Functional programming java
PPT
Functions123
PPT
Functions12
PDF
Deriving the Y Combinator
PPT
Functional object
PPS
Let Us Learn Lambda Using C# 3.0
PPTX
functions
PPTX
Столпы функционального программирования для адептов ООП, Николай Мозговой
PPT
Module 2 topic 2 notes
PPT
LECTURE 5-Function in Matlab how to use mathlab
PPTX
Advanced JavaScript
PDF
List-based Monadic Computations for Dynamic Languages
PPT
Part 3-functions
PPTX
OOPS Object oriented Programming PPT Tutorial
PDF
Programming in Scala - Lecture Two
TI1220 Lecture 6: First-class Functions
Composition birds-and-recursion
Functional Programming Concepts for Imperative Programmers
User defined functions
Functors, applicatives, monads
Functional programming java
Functions123
Functions12
Deriving the Y Combinator
Functional object
Let Us Learn Lambda Using C# 3.0
functions
Столпы функционального программирования для адептов ООП, Николай Мозговой
Module 2 topic 2 notes
LECTURE 5-Function in Matlab how to use mathlab
Advanced JavaScript
List-based Monadic Computations for Dynamic Languages
Part 3-functions
OOPS Object oriented Programming PPT Tutorial
Programming in Scala - Lecture Two

Recently uploaded (20)

PPTX
Presentation - Principles of Instructional Design.pptx
PPTX
Rise of the Digital Control Grid Zeee Media and Hope and Tivon FTWProject.com
PPTX
Blending method and technology for hydrogen.pptx
PPTX
Information-Technology-in-Human-Society (2).pptx
PPTX
From XAI to XEE through Influence and Provenance.Controlling model fairness o...
PPT
Storage Area Network Best Practices from HP
PPTX
How to Convert Tickets Into Sales Opportunity in Odoo 18
PDF
Addressing the challenges of harmonizing law and artificial intelligence tech...
PDF
CEH Module 2 Footprinting CEH V13, concepts
PDF
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
PDF
Streamline Vulnerability Management From Minimal Images to SBOMs
PDF
Fitaura: AI & Machine Learning Powered Fitness Tracker
PDF
State of AI in Business 2025 - MIT NANDA
PDF
The AI Revolution in Customer Service - 2025
PPTX
Digital Convergence: How GIS, BIM, and CAD Revolutionize Asset Management
PPTX
maintenance powerrpoint for adaprive and preventive
PDF
Optimizing bioinformatics applications: a novel approach with human protein d...
PPTX
Build automations faster and more reliably with UiPath ScreenPlay
PDF
Advancements in abstractive text summarization: a deep learning approach
PPTX
How to use fields_get method in Odoo 18
Presentation - Principles of Instructional Design.pptx
Rise of the Digital Control Grid Zeee Media and Hope and Tivon FTWProject.com
Blending method and technology for hydrogen.pptx
Information-Technology-in-Human-Society (2).pptx
From XAI to XEE through Influence and Provenance.Controlling model fairness o...
Storage Area Network Best Practices from HP
How to Convert Tickets Into Sales Opportunity in Odoo 18
Addressing the challenges of harmonizing law and artificial intelligence tech...
CEH Module 2 Footprinting CEH V13, concepts
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
Streamline Vulnerability Management From Minimal Images to SBOMs
Fitaura: AI & Machine Learning Powered Fitness Tracker
State of AI in Business 2025 - MIT NANDA
The AI Revolution in Customer Service - 2025
Digital Convergence: How GIS, BIM, and CAD Revolutionize Asset Management
maintenance powerrpoint for adaprive and preventive
Optimizing bioinformatics applications: a novel approach with human protein d...
Build automations faster and more reliably with UiPath ScreenPlay
Advancements in abstractive text summarization: a deep learning approach
How to use fields_get method in Odoo 18

Introduction to Functional Programming in JavaScript

  • 1. Functional Programming Tommy “is awesome” Montgomery 2009-03-06
  • 2. What is functional programming? Uses functions Lambda Calculus Very academic Kinda goofy
  • 3. Elements of functional languages Recursion Functions (der) No state
  • 4. Functional Languages Haskell Erlang F# OCaml Scheme Smalltalk J K Mathematica XSLT LISP (kinda)
  • 5. Fun Fact #1 Tommy was once a professional musician
  • 6. λ -c alculus Anonymous functions JavaScript PHP 4.0.1 – PHP 5.2.x (kinda) PHP 5.3 (more kinda) C# 2.0 Java sucks, as usual Unary Functions take one argument, and return one value
  • 7. Example λ x . x + 2
  • 8. Example λ x . x + 2 input
  • 9. Example λ x . x + 2 input return value
  • 10. Example λ x . x + 2 input return value f(x) = x + 2
  • 11. Higher order functions Functions that take functions as arguments and return functions Where the real power of functional programming lies
  • 12. Higher order function example Mathematical derivative
  • 13. Higher order function example //f is a function function derivative ( f ) { return function ( x ) { //approximation of derivative return ( f ( x + 0.00001 ) – f ( x )) / 0.00001 ; } }
  • 14. Higher order function example //evaluate derivative of x 2 : var deriv_x_squared = derivative ( function ( x ) { return x * x ; } ); alert ( deriv_x_squared ( 3 )); //alerts 6ish
  • 15. Fun Fact #2 Tommy used to be a gangsta
  • 16. Bound variables The λ operator binds its variables to its scope All other variables are “free” Freedom 
  • 17. Bound variables λ x . x + y
  • 18. Bound variables λ x . x + y bound variable bound variable free variable
  • 19. Bound variables λ x . x + y bound variable bound variable free variable f(x) = x + y
  • 20. Natural numbers Everything is a function, remember? How do we define the natural numbers in functional programming (1, 2, 3, …)?
  • 22. Church numerals 0 ≡ λ f . λ x . x 1 ≡ λ f . λ x . f x 2 ≡ λ f . λ x . f (f x) 3 ≡ λ f . λ x . f (f (f x)) ...
  • 23. Church numerals in JavaScript //identity function // λ x . x function identity ( x ) { return x ; } //Church numeral zero // λ f . λ x . x function zero ( f ) { return identity ; }
  • 24. Church numerals in JavaScript //successor function (succ(n) = n + 1) // λ n . λ f . f (n f x) function succ ( n ) { return function ( f ) { return function ( x ) { return f ( n ( f )( x )); } } }
  • 25. Church numerals in JavaScript //gets a function representing //the nth church number function getChurchNumber ( n ) { var ret = zero ; for ( var i = 0 ; i < n ; i ++) { ret = succ ( ret ); } return ret ; }
  • 26. Church numerals in JavaScript //gets the nth church number function getNaturalNumber ( n ) { var value = 0 ; for ( var i = 0 ; i < n ; i ++) { value += getChurchNumber ( i )( function ( x ) { return x + 1 ; } )( 0 ); } return value ; }
  • 27. Fun fact #3 Wombats are unfairly cute
  • 28. Addition // λ m . λ n . λ f . n f (m f x) function add ( m ) { return function ( n ) { return function ( f ) { return function ( x ) { return n ( f )( m ( f )( x )); } } } }
  • 29. Addition using successor // λ m . λ n . m succ n function addWithSucc ( m ) { return function ( n ) { return m ( succ )( n ); } }
  • 31. Recursion Local variables are the devil! Also, a paradox In lambda calculus, you cannot define a function that includes itself i.e. the definition of recursion
  • 32. The Y Combinator Cool name Also known as “The Paradoxical Operator”
  • 33. Recursion To properly define recursion, a recursive function g must take as an argument a function f , which expands to g which takes an argument f .
  • 35. Recursion f = g(f) f is a fixed point of g Known as the Y-Combinator
  • 36. The Y-Combinator Y = λ g . (λ x . g (x x)) (λ x . g (x x))
  • 37. The Y-Combinator in JavaScript function Y ( g ) { return function ( x ) { return g ( function ( y ) { return x ( x )( y ); } ); }( function ( x ) { return g ( function ( y ) { return x ( x )( y ); } ); } ); } http://matt.might.net/articles/implementation-of-recursive-fixed-point-y-combinator-in-javascript-for-memoization/
  • 38. Functional Factorial Y factorial n Y : definition of Y-Combinator factorial : functional definition of factorial n : integer
  • 39. Functional Factorial function factorial ( f ) { return function ( n ) { return ( n == 0 ) ? 1 : n * f ( n – 1 ); } } //call like so: alert ( Y ( factorial )( 5 )); //alerts 120
  • 40. What is happening? Recursive calls are abstracted to an inner function, which is evaluated as a lambda function (which by very definition are not recursive, remember?) Interesting, but not all that useful…
  • 41.  
  • 42. Other topics… Currying Mapping Reduction Substitution Elaboration on statelessness Imperative vs. Functional Performance
  • 43.