Your SlideShare is downloading. ×
Introduction to Functional Programming in JavaScript
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Introduction to Functional Programming in JavaScript

12,717
views

Published on

A presentation I did for work on functional programming. It's meant as an introduction to functional programming, and I implemented the fundamentals of functional programming (Church Numerals, …

A presentation I did for work on functional programming. It's meant as an introduction to functional programming, and I implemented the fundamentals of functional programming (Church Numerals, Y-Combinator, etc.) in JavaScript.

Published in: Technology

0 Comments
20 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
12,717
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
161
Comments
0
Likes
20
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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, …)?
  • 21.
    • very painfully
  • 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 );
    • }
    • }
  • 30. Holy crap
    • http://uxul.wordpress.com/2009/03/02/generating-church-numbers-with-javascript/
  • 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 .
  • 34.
    • Got that?
  • 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.  
  • 44. Questions…