Last active
August 29, 2015 14:04
-
-
Save BrendanEich/a748220b894a341bc7a3 to your computer and use it in GitHub Desktop.
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
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