Skip to content

Instantly share code, notes, and snippets.

@jed
Created December 19, 2011 06:19
Show Gist options
  • Save jed/1495681 to your computer and use it in GitHub Desktop.
Save jed/1495681 to your computer and use it in GitHub Desktop.
recursive IIFEs
upperCase = (str, cb) ->
do fn = (err, str) ->
if err
cb err
else if typeof str is 'function'
str fn # fn is this function, as of 1.2.1-pre
else
str = String str
cb null, str.toUpperCase()
upperCase = function(str, cb) {
var fn;
return (fn = function(err, str) {
if (err) {
return cb(err);
} else if (typeof str === 'function') {
return str(fn);
} else {
str = String(str);
return cb(null, str.toUpperCase());
}
})(err, str);
};
}
@jed
Copy link
Author

jed commented Dec 19, 2011

here's how i'd write it in javascript:

function upperCase(str, cb) {
  return function fn(err, str) {
    return err ? cb(err) :

    typeof str === 'function' ? str(fn) :

    cb(null, String(str).toUpperCase())
  }(null, str)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment