by neimado · 15y ago · 0 comments
A function that does nothing useful but illustrates some pain points:
function frob(obj) {
var rest = [].slice.call(arguments, 1)
, results = []
for (var k in obj) if (obj.hasOwnProperty(k)) {
v = obj[k]
if (blurgh(v, rest)) {
results.push(v)
}
}
return results
}
This is roughly the same function in CS: frob = (obj, rest...) -> [v for v of obj if blurgh(v, rest)]
That's at least 5x more expressive. And it works in browsers, node, and pretty much every other JS environment. I don't write CS but you have to be blind to deny its benefits.edited: Fixed the CS to iterate over keys of obj using 'of' based on other comments here
Nitpicking aside, I totally agree about the expressiveness. I find myself writing about half the number of lines in CS as compared to JS, and the syntax fits my brain better.
frob = (obj, rest...) -> v for v of obj when blurgh(v, rest)
edit: parens dropped for noise reduction