Created
January 9, 2013 22:48
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
findTheThingWeWant = (things) -> | |
things.each (thing) ~> | |
return item if thing.isWhatWeWant |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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