Skip to content

Instantly share code, notes, and snippets.

@stammy
Forked from jimtla/underscoreR.coffee
Created May 7, 2012 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stammy/2625625 to your computer and use it in GitHub Desktop.
Save stammy/2625625 to your computer and use it in GitHub Desktop.
Add CoffeeScript friendly argument order to Underscore.js
# Five lines of code that will make your underscore + CoffeeScript use cleaner.
# Creates an underscore function for each function in to_reverse with R (short for Reversed) appended to the name.
# The R version moves the function argument (first argument in normal underscore) to the end,
# so you can write:
$(window).scroll _.throttleR 500, ->
console.log "This print's at most every 500ms"
# Instead of:
$(window).scroll _.throttle ->
console.log "This prints at most every 500ms too"
,
500
# The code (Add immediately after you include underscore.js):
to_reverse = ['bind', 'delay', 'defer', 'throttle', 'debounce']
mixin = {}
for name in to_reverse then do (name) ->
mixin["#{name}R"] = (args..., f) -> _(f)[name](args...)
_.mixin mixin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment