SlideShare a Scribd company logo
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

What's hot (20)

Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Domenic Denicola
 
JDBC
JDBCJDBC
JDBC
Sunil OS
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
Harbinger Systems - HRTech Builder of Choice
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
L&T Technology Services Limited
 
Hibernate
Hibernate Hibernate
Hibernate
Sunil OS
 
04 Handling Exceptions
04 Handling Exceptions04 Handling Exceptions
04 Handling Exceptions
rehaniltifat
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
JavaScript: Ajax & DOM Manipulation
JavaScript: Ajax & DOM ManipulationJavaScript: Ajax & DOM Manipulation
JavaScript: Ajax & DOM Manipulation
borkweb
 
Introduction to mobile application development
Introduction to mobile application developmentIntroduction to mobile application development
Introduction to mobile application development
Chandan Maurya
 
XML-RPC (XML Remote Procedure Call)
XML-RPC (XML Remote Procedure Call)XML-RPC (XML Remote Procedure Call)
XML-RPC (XML Remote Procedure Call)
Peter R. Egli
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
Eyal Vardi
 
Java Basics
Java BasicsJava Basics
Java Basics
Dhanunjai Bandlamudi
 
Structure chart
Structure chartStructure chart
Structure chart
Roy Antony Arnold G
 
Android webservices
Android webservicesAndroid webservices
Android webservices
Krazy Koder
 
Introduction to Android and Android Studio
Introduction to Android and Android StudioIntroduction to Android and Android Studio
Introduction to Android and Android Studio
Suyash Srijan
 
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...
Mr. Akaash
 
All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...
All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...
All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...
Ernie Souhrada
 
Recyclerview in action
Recyclerview in action Recyclerview in action
Recyclerview in action
Pratama Nur Wijaya
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
Abid Kohistani
 
From framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytvFrom framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytv
CodelyTV
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Domenic Denicola
 
Hibernate
Hibernate Hibernate
Hibernate
Sunil OS
 
04 Handling Exceptions
04 Handling Exceptions04 Handling Exceptions
04 Handling Exceptions
rehaniltifat
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
JavaScript: Ajax & DOM Manipulation
JavaScript: Ajax & DOM ManipulationJavaScript: Ajax & DOM Manipulation
JavaScript: Ajax & DOM Manipulation
borkweb
 
Introduction to mobile application development
Introduction to mobile application developmentIntroduction to mobile application development
Introduction to mobile application development
Chandan Maurya
 
XML-RPC (XML Remote Procedure Call)
XML-RPC (XML Remote Procedure Call)XML-RPC (XML Remote Procedure Call)
XML-RPC (XML Remote Procedure Call)
Peter R. Egli
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
Eyal Vardi
 
Android webservices
Android webservicesAndroid webservices
Android webservices
Krazy Koder
 
Introduction to Android and Android Studio
Introduction to Android and Android StudioIntroduction to Android and Android Studio
Introduction to Android and Android Studio
Suyash Srijan
 
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...
Mr. Akaash
 
All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...
All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...
All Your IOPS Are Belong To Us - A Pinteresting Case Study in MySQL Performan...
Ernie Souhrada
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
Abid Kohistani
 
From framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytvFrom framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytv
CodelyTV
 

Viewers also liked (14)

Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScript
WebF
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScript
Aung Baw
 
Interactive Scientific Image Analysis using Spark
Interactive Scientific Image Analysis using SparkInteractive Scientific Image Analysis using Spark
Interactive Scientific Image Analysis using Spark
Kevin Mader
 
Functional programming
Functional programmingFunctional programming
Functional programming
edusmildo
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
Shahriar Hyder
 
Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey
Hakka Labs
 
Machine Learning with Apache Mahout
Machine Learning with Apache MahoutMachine Learning with Apache Mahout
Machine Learning with Apache Mahout
Daniel Glauser
 
Functional programming
Functional programmingFunctional programming
Functional programming
Prateek Jain
 
The Lambda Calculus and The JavaScript
The Lambda Calculus and The JavaScriptThe Lambda Calculus and The JavaScript
The Lambda Calculus and The JavaScript
Norman Richards
 
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
Luis Atencio
 
Modeling with Hadoop kdd2011
Modeling with Hadoop kdd2011Modeling with Hadoop kdd2011
Modeling with Hadoop kdd2011
Milind Bhandarkar
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
Prashant Kalkar
 
Predictive Analytics Project in Automotive Industry
Predictive Analytics Project in Automotive IndustryPredictive Analytics Project in Automotive Industry
Predictive Analytics Project in Automotive Industry
Matouš Havlena
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScript
WebF
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScript
Aung Baw
 
Interactive Scientific Image Analysis using Spark
Interactive Scientific Image Analysis using SparkInteractive Scientific Image Analysis using Spark
Interactive Scientific Image Analysis using Spark
Kevin Mader
 
Functional programming
Functional programmingFunctional programming
Functional programming
edusmildo
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
Shahriar Hyder
 
Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey
Hakka Labs
 
Machine Learning with Apache Mahout
Machine Learning with Apache MahoutMachine Learning with Apache Mahout
Machine Learning with Apache Mahout
Daniel Glauser
 
Functional programming
Functional programmingFunctional programming
Functional programming
Prateek Jain
 
The Lambda Calculus and The JavaScript
The Lambda Calculus and The JavaScriptThe Lambda Calculus and The JavaScript
The Lambda Calculus and The JavaScript
Norman Richards
 
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
Luis Atencio
 
Modeling with Hadoop kdd2011
Modeling with Hadoop kdd2011Modeling with Hadoop kdd2011
Modeling with Hadoop kdd2011
Milind Bhandarkar
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
Prashant Kalkar
 
Predictive Analytics Project in Automotive Industry
Predictive Analytics Project in Automotive IndustryPredictive Analytics Project in Automotive Industry
Predictive Analytics Project in Automotive Industry
Matouš Havlena
 
Ad

Similar to Introduction to Functional Programming in JavaScript (20)

TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
Composition birds-and-recursion
Composition birds-and-recursionComposition birds-and-recursion
Composition birds-and-recursion
David Atchley
 
Functional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative ProgrammersFunctional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative Programmers
Chris
 
User defined functions
User defined functionsUser defined functions
User defined functions
shubham_jangid
 
Functors, applicatives, monads
Functors, applicatives, monadsFunctors, applicatives, monads
Functors, applicatives, monads
rkaippully
 
Functional programming java
Functional programming javaFunctional programming java
Functional programming java
Maneesh Chaturvedi
 
Functions123
Functions123 Functions123
Functions123
sandhubuta
 
Functions12
Functions12Functions12
Functions12
sandhubuta
 
Deriving the Y Combinator
Deriving the Y CombinatorDeriving the Y Combinator
Deriving the Y Combinator
Yuta Okazaki
 
Functional object
Functional objectFunctional object
Functional object
ruchijindal87
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
Sheik Uduman Ali
 
functions
functionsfunctions
functions
Makwana Bhavesh
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
Module 2 topic 2 notes
Module 2 topic 2 notesModule 2 topic 2 notes
Module 2 topic 2 notes
chrystal_brinson
 
Advanced JavaScript
Advanced JavaScript Advanced JavaScript
Advanced JavaScript
Zsolt Mészárovics
 
List-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic LanguagesList-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic Languages
Wim Vanderbauwhede
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functions
ankita44
 
OOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT TutorialOOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT Tutorial
amitnitpatna
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
Angelo Corsaro
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
Composition birds-and-recursion
Composition birds-and-recursionComposition birds-and-recursion
Composition birds-and-recursion
David Atchley
 
Functional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative ProgrammersFunctional Programming Concepts for Imperative Programmers
Functional Programming Concepts for Imperative Programmers
Chris
 
User defined functions
User defined functionsUser defined functions
User defined functions
shubham_jangid
 
Functors, applicatives, monads
Functors, applicatives, monadsFunctors, applicatives, monads
Functors, applicatives, monads
rkaippully
 
Deriving the Y Combinator
Deriving the Y CombinatorDeriving the Y Combinator
Deriving the Y Combinator
Yuta Okazaki
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
Sheik Uduman Ali
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
List-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic LanguagesList-based Monadic Computations for Dynamic Languages
List-based Monadic Computations for Dynamic Languages
Wim Vanderbauwhede
 
Part 3-functions
Part 3-functionsPart 3-functions
Part 3-functions
ankita44
 
OOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT TutorialOOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT Tutorial
amitnitpatna
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
Angelo Corsaro
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
 
Ad

Recently uploaded (20)

Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Extend-Microsoft365-with-Copilot-agents.pptx
Extend-Microsoft365-with-Copilot-agents.pptxExtend-Microsoft365-with-Copilot-agents.pptx
Extend-Microsoft365-with-Copilot-agents.pptx
hoang971
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
Create Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent BuilderCreate Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent Builder
DianaGray10
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical PodcastDevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
soulmaite review - Find Real AI soulmate review
soulmaite review - Find Real AI soulmate reviewsoulmaite review - Find Real AI soulmate review
soulmaite review - Find Real AI soulmate review
Soulmaite
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Compliance-as-a-Service document pdf text
Compliance-as-a-Service document pdf textCompliance-as-a-Service document pdf text
Compliance-as-a-Service document pdf text
Earthling security
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptxISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
IntroSlides-May-BuildWithAi-EarthEngine.pdf
IntroSlides-May-BuildWithAi-EarthEngine.pdfIntroSlides-May-BuildWithAi-EarthEngine.pdf
IntroSlides-May-BuildWithAi-EarthEngine.pdf
Luiz Carneiro
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Mark Zuckerberg teams up with frenemy Palmer Luckey to shape the future of XR...
Mark Zuckerberg teams up with frenemy Palmer Luckey to shape the future of XR...Mark Zuckerberg teams up with frenemy Palmer Luckey to shape the future of XR...
Mark Zuckerberg teams up with frenemy Palmer Luckey to shape the future of XR...
Scott M. Graffius
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Extend-Microsoft365-with-Copilot-agents.pptx
Extend-Microsoft365-with-Copilot-agents.pptxExtend-Microsoft365-with-Copilot-agents.pptx
Extend-Microsoft365-with-Copilot-agents.pptx
hoang971
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
Create Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent BuilderCreate Your First AI Agent with UiPath Agent Builder
Create Your First AI Agent with UiPath Agent Builder
DianaGray10
 
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025Developing Schemas with FME and Excel - Peak of Data & AI 2025
Developing Schemas with FME and Excel - Peak of Data & AI 2025
Safe Software
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical PodcastDevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
soulmaite review - Find Real AI soulmate review
soulmaite review - Find Real AI soulmate reviewsoulmaite review - Find Real AI soulmate review
soulmaite review - Find Real AI soulmate review
Soulmaite
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Compliance-as-a-Service document pdf text
Compliance-as-a-Service document pdf textCompliance-as-a-Service document pdf text
Compliance-as-a-Service document pdf text
Earthling security
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptxISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
IntroSlides-May-BuildWithAi-EarthEngine.pdf
IntroSlides-May-BuildWithAi-EarthEngine.pdfIntroSlides-May-BuildWithAi-EarthEngine.pdf
IntroSlides-May-BuildWithAi-EarthEngine.pdf
Luiz Carneiro
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Mark Zuckerberg teams up with frenemy Palmer Luckey to shape the future of XR...
Mark Zuckerberg teams up with frenemy Palmer Luckey to shape the future of XR...Mark Zuckerberg teams up with frenemy Palmer Luckey to shape the future of XR...
Mark Zuckerberg teams up with frenemy Palmer Luckey to shape the future of XR...
Scott M. Graffius
 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
 
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdfBoosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Boosting MySQL with Vector Search -THE VECTOR SEARCH CONFERENCE 2025 .pdf
Alkin Tezuysal
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 

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.