Successfully reported this slideshow.
Your SlideShare is downloading. ×

Introduction to Functional Programming in JavaScript

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 44 Ad

Introduction to Functional Programming in JavaScript

Download to read offline

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.

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.

Advertisement
Advertisement

More Related Content

Slideshows for you (20)

Advertisement

Similar to Introduction to Functional Programming in JavaScript (20)

Recently uploaded (20)

Advertisement

Introduction to Functional Programming in JavaScript

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

×