john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

increment with functional programming

<!DOCTYPE html><html><head><title></title></head><body><pre><script>


    function add( a , b ) {
        return a + b;
    }
    function mul( a , b ) {
        return a * b;
    }

    function applyf( x ){
        return function ( first ) {
                return function( second )   // closure means it can access outer variable
                {   return x( first , second);
                };
        };
    }



    function curry( func, first ){
        return function ( second ) {
                    return func( first , second);
                };
    }

    var add0 = curry( add, 0)
    var inc = curry( add, 1)
    document.writeln( inc(5) );               // 6
    document.writeln( inc( inc( 5 )) );               // 7


    var inc = applyf( add, 1);
    document.writeln( inc(5) );               // 6
    document.writeln( inc( inc( 5 )) );               // 7


    var inc = addf( 1);







</script></pre></body></html>

  • « curry
  • switcheroo »

Published

Oct 5, 2013

Category

javascript

~83 words

Tags

  • functional 2
  • increment 2
  • javascript 43