john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

javascript functional twice

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

    function log(arg) {
        document.writeln( arg );
    }
    function identity(x) {
        return x;
    }

    var id = function identity( x ){ return x; };

    log( identity(3) );
    log( id(3) );



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


    function addf( first )
    {
        return function( second )   // closure means it can access outer variable
        {
            return first + second;
        };
    }


    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);
                };
    }


    function twice( binary ){
        return function( a ){
            return binary( a, a );
        };
    }




    document.writeln( add(11,11) );               // 22
    var doubl = twice( add );
    doubl(11)                           //22
    var square = twice( mul )
    square( 11 )                    //121

    document.writeln( square(11) );               // 22






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

  • « hmac signature rest client
  • Rdp rdesktop install usage gui grdesktop remmina »

Published

Oct 7, 2013

Category

javascript

~112 words

Tags

  • functional 2
  • javascript 43