Skip to content

Instantly share code, notes, and snippets.

@machty
Created January 9, 2013 22:48
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 machty/4497731 to your computer and use it in GitHub Desktop.
Save machty/4497731 to your computer and use it in GitHub Desktop.
Proposal: add block level lambda ~> operator to CoffeeScript to hackishly achieve block level lambda functionality in pre ES6 Javascript. An example of block level lambdas is returning a value from within a .each loop in Ruby. See below.
findTheThingWeWant = (things) ->
things.each (thing) ~>
return item if thing.isWhatWeWant
var _CSBlockReturn, findTheThingWeWant;
// Generated due to use of ~>
_CSBlockReturn = (function() {
_CSBlockReturn.name = '_CSBlockReturn';
function _CSBlockReturn(value) {
this.value = value;
}
return _CSBlockReturn;
})();
findTheThingWeWant = function(things) {
try {
things.each(function() {
if(thing.isWhatWeWant) {
throw new _CSBlockReturn(thing);
}
})
} catch(e) {
if(e.constructor === _CSBlockReturn) {
return e.value;
}
else
{
throw e;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment