Skip to content

Instantly share code, notes, and snippets.

@gilbert
Last active September 20, 2016 15:09
Show Gist options
  • Save gilbert/13d4593b00b4ee7cea33 to your computer and use it in GitHub Desktop.
Save gilbert/13d4593b00b4ee7cea33 to your computer and use it in GitHub Desktop.

Revisions

  1. gilbert revised this gist Sep 20, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion functions.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    //
    // var add = function (x,y) { return x + y }
    // var add5 = add.papp(5)
    // add5(7) //=> 11
    // add5(7) //=> 12
    //
    Function.prototype.papp = function () {
    var slice = Array.prototype.slice
  2. gilbert revised this gist Sep 20, 2016. 1 changed file with 29 additions and 15 deletions.
    44 changes: 29 additions & 15 deletions functions.js
    Original file line number Diff line number Diff line change
    @@ -49,20 +49,34 @@ Function.prototype.coldPapp = function() {
    }


    // Compose two functions together,
    // piping the result of one to the other.
    // Example:
    //
    // var double = function (x) { return x * 2 }
    // var addOne = function (x) { return x + 1 }
    // var doublePlus = double.andThen(addOne)
    // doublePlus(3) //=> 7
    //
    Function.prototype.andThen = function(f) {
    var g = this
    return function() {
    var slice = Array.prototype.slice
    var args = slice.call(arguments)
    return f.call(this, g.apply(this, args))
    Function.prototype.throttle = function (fn, waittime, threshhold, scope) {
    threshhold || (threshhold = 250);
    var last, deferTimer

    var go = function () {
    var context = scope || this

    var now = +new Date,
    args = arguments

    if (last && now < last + threshhold) {
    // hold on to it
    clearTimeout(deferTimer)

    deferTimer = setTimeout(function () {
    last = now
    fn.apply(context, args)
    }, threshhold)

    } else {
    last = now
    fn.apply(context, args)
    }
    }

    var goTimeout = null
    return function () {
    clearTimeout(goTimeout)
    goTimeout = setTimeout(go, waittime)
    }
    }
  3. gilbert revised this gist May 22, 2015. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions functions.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    // specific data to an event handler.
    // Example:
    //
    // var add = function (x,y) { return x + y; }
    // var add = function (x,y) { return x + y }
    // var add5 = add.papp(5)
    // add5(7) //=> 11
    //
    @@ -19,7 +19,7 @@ Function.prototype.papp = function () {
    // anchor click page jumps and form submits.
    // Example:
    //
    // var x = 0;
    // var x = 0
    // var increment = function () { x += 1 }
    // myAnchorEl.addEventListener('click', increment.chill())
    //
    @@ -28,13 +28,13 @@ Function.prototype.chill = function() {
    return function(e) {
    e.preventDefault()
    return fn()
    };
    };
    }
    }

    // Both prevent default and partially apply in one go.
    // Example:
    //
    // var x = 0;
    // var x = 0
    // var increment = function (amount) { x += amount }
    // myAnchorEl.addEventListener('click', increment.coldPapp(17))
    //
    @@ -45,8 +45,8 @@ Function.prototype.coldPapp = function() {
    return function(e) {
    e.preventDefault()
    return fn.apply(this, args.concat(slice.call(arguments, 1)))
    };
    };
    }
    }


    // Compose two functions together,
  4. gilbert revised this gist May 21, 2015. 1 changed file with 0 additions and 20 deletions.
    20 changes: 0 additions & 20 deletions functions.js
    Original file line number Diff line number Diff line change
    @@ -48,26 +48,6 @@ Function.prototype.coldPapp = function() {
    };
    };

    // Apply the combination of two arrays of arguments.
    // Useful for dealing with fancy functions using the `arguments` keyword.
    // Example:
    //
    // function sum () {
    // var result = 0
    // for (var i=0; i < arguments.length; i++) { result += arguments[i] }
    // return result
    // }
    // function sumPlusTen () {
    // return sum.apply2(null, [10], arguments)
    // }
    // sumPlusTen(100,200) //=> 310
    //
    Function.prototype.apply2 = function (context, args1, args2) {
    var slice = [].slice
    var args = slice.call(args1).concat(slice.call(args2))
    return this.apply(context, args)
    }


    // Compose two functions together,
    // piping the result of one to the other.
  5. gilbert revised this gist Mar 19, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions functions.js
    Original file line number Diff line number Diff line change
    @@ -60,6 +60,7 @@ Function.prototype.coldPapp = function() {
    // function sumPlusTen () {
    // return sum.apply2(null, [10], arguments)
    // }
    // sumPlusTen(100,200) //=> 310
    //
    Function.prototype.apply2 = function (context, args1, args2) {
    var slice = [].slice
  6. gilbert revised this gist Mar 19, 2015. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions functions.js
    Original file line number Diff line number Diff line change
    @@ -48,6 +48,25 @@ Function.prototype.coldPapp = function() {
    };
    };

    // Apply the combination of two arrays of arguments.
    // Useful for dealing with fancy functions using the `arguments` keyword.
    // Example:
    //
    // function sum () {
    // var result = 0
    // for (var i=0; i < arguments.length; i++) { result += arguments[i] }
    // return result
    // }
    // function sumPlusTen () {
    // return sum.apply2(null, [10], arguments)
    // }
    //
    Function.prototype.apply2 = function (context, args1, args2) {
    var slice = [].slice
    var args = slice.call(args1).concat(slice.call(args2))
    return this.apply(context, args)
    }


    // Compose two functions together,
    // piping the result of one to the other.
  7. gilbert revised this gist Mar 16, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion functions.js
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ Function.prototype.chill = function() {
    };
    };

    // Both prevent default and partially apply in one go
    // Both prevent default and partially apply in one go.
    // Example:
    //
    // var x = 0;
  8. gilbert revised this gist Mar 16, 2015. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions functions.js
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    // Curry a function. Useful for binding specific data
    // to an event handler.
    // Partially apply arguments to a function. Useful for binding
    // specific data to an event handler.
    // Example:
    //
    // var add = function (x,y) { return x + y; }
    // var add5 = add.curry(5)
    // var add5 = add.papp(5)
    // add5(7) //=> 11
    //
    Function.prototype.curry = function () {
    Function.prototype.papp = function () {
    var slice = Array.prototype.slice
    var fn = this
    var args = slice.call(arguments)
    @@ -15,7 +15,7 @@ Function.prototype.curry = function () {
    }
    }

    // Simply prevent the default action. Useful for preventing
    // Prevents the default action. Useful for preventing
    // anchor click page jumps and form submits.
    // Example:
    //
    @@ -31,14 +31,14 @@ Function.prototype.chill = function() {
    };
    };

    // Both prevent default and curry in one go
    // Both prevent default and partially apply in one go
    // Example:
    //
    // var x = 0;
    // var increment = function (amount) { x += amount }
    // myAnchorEl.addEventListener('click', increment.coldCurry(17))
    // myAnchorEl.addEventListener('click', increment.coldPapp(17))
    //
    Function.prototype.coldCurry = function() {
    Function.prototype.coldPapp = function() {
    var slice = Array.prototype.slice
    var fn = this
    var args = slice.call(arguments)
  9. gilbert revised this gist Jan 7, 2015. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions functions.js
    Original file line number Diff line number Diff line change
    @@ -47,3 +47,22 @@ Function.prototype.coldCurry = function() {
    return fn.apply(this, args.concat(slice.call(arguments, 1)))
    };
    };


    // Compose two functions together,
    // piping the result of one to the other.
    // Example:
    //
    // var double = function (x) { return x * 2 }
    // var addOne = function (x) { return x + 1 }
    // var doublePlus = double.andThen(addOne)
    // doublePlus(3) //=> 7
    //
    Function.prototype.andThen = function(f) {
    var g = this
    return function() {
    var slice = Array.prototype.slice
    var args = slice.call(arguments)
    return f.call(this, g.apply(this, args))
    }
    }
  10. gilbert revised this gist Nov 3, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion functions.js
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ Function.prototype.curry = function () {
    var fn = this
    var args = slice.call(arguments)
    return function () {
    fn.apply(this, args.concat(slice.call(arguments)))
    return fn.apply(this, args.concat(slice.call(arguments)))
    }
    }

  11. gilbert revised this gist Nov 3, 2014. 1 changed file with 20 additions and 2 deletions.
    22 changes: 20 additions & 2 deletions functions.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,11 @@
    // Curry a function. Useful for binding specific data
    // to an event handler.
    // Example:
    //
    // var add = function (x,y) { return x + y; }
    // var add5 = add.curry(5)
    // add5(7) //=> 11
    //
    Function.prototype.curry = function () {
    var slice = Array.prototype.slice
    var fn = this
    @@ -9,8 +15,14 @@ Function.prototype.curry = function () {
    }
    }

    // Simply prevent the default action. Useful for binding
    // to anchor tag clicks and form submits.
    // Simply prevent the default action. Useful for preventing
    // anchor click page jumps and form submits.
    // Example:
    //
    // var x = 0;
    // var increment = function () { x += 1 }
    // myAnchorEl.addEventListener('click', increment.chill())
    //
    Function.prototype.chill = function() {
    var fn = this
    return function(e) {
    @@ -20,6 +32,12 @@ Function.prototype.chill = function() {
    };

    // Both prevent default and curry in one go
    // Example:
    //
    // var x = 0;
    // var increment = function (amount) { x += amount }
    // myAnchorEl.addEventListener('click', increment.coldCurry(17))
    //
    Function.prototype.coldCurry = function() {
    var slice = Array.prototype.slice
    var fn = this
  12. gilbert created this gist Nov 3, 2014.
    31 changes: 31 additions & 0 deletions functions.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    // Curry a function. Useful for binding specific data
    // to an event handler.
    Function.prototype.curry = function () {
    var slice = Array.prototype.slice
    var fn = this
    var args = slice.call(arguments)
    return function () {
    fn.apply(this, args.concat(slice.call(arguments)))
    }
    }

    // Simply prevent the default action. Useful for binding
    // to anchor tag clicks and form submits.
    Function.prototype.chill = function() {
    var fn = this
    return function(e) {
    e.preventDefault()
    return fn()
    };
    };

    // Both prevent default and curry in one go
    Function.prototype.coldCurry = function() {
    var slice = Array.prototype.slice
    var fn = this
    var args = slice.call(arguments)
    return function(e) {
    e.preventDefault()
    return fn.apply(this, args.concat(slice.call(arguments, 1)))
    };
    };