Skip to content

Instantly share code, notes, and snippets.

@BrendanEich
Last active August 29, 2015 14:04
Show Gist options
  • Save BrendanEich/a748220b894a341bc7a3 to your computer and use it in GitHub Desktop.
Save BrendanEich/a748220b894a341bc7a3 to your computer and use it in GitHub Desktop.
let nums = [1, 53, 0, 42, 3, 99, 2, 67, 4];
// Suppose we're in a function here...
let strs = nums.map(function (x) {
switch (x) {
case 1: return x + 'st';
case 2: return x + 'nd';
case 3: return x + 'rd';
default: return x + 'th';
}
});
// Now rewrite using proposed block lambda syntax.
// But we cannot use return!
let strs = nums.map({|x|
if (x === 1) {
x + 'st';
} else if (x === 2) {
x + 'nd';
} else if (x === 3) {
x + 'rd';
} else {
x + 'th';
}
});
// Under maintenance a n00b adds logic for i18n to special-case 0
// and (not remembering 'return' returns from outer function) uses
// an early return -- and sees how that saves one 'else' keyword,
// converts all completion values in tail positions into 'return's
// and removes the 'else's.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment