john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

switcheroo

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


    function sub( first, second) { return first - second; }


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

    /*
    function switcheroogen( func )
    {
        return function( ...args ){
            return func(...(args.reverse() );
        }
    }
    */


    var bus = switcheroo(sub)
    document.writeln( bus(3,2) );               // -1




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

  • « increment with functional programming
  • composeu »

Published

Oct 5, 2013

Category

javascript

~106 words

Tags

  • javascript 43
  • switcheroo 1