Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That's like saying you won't use Rails because it's written in Ruby and not C.

Also, it's nice to work in a language that has the right features and not just all the features. I like CoffeeScript as much for what it doesn't have as for what it does have. I recently saw this piece of code:

    new Array(26)
      .map(function (item, index) {
        return String.fromCharCode(65 + index);
    })
as a "heads up" for map lovers. That's the kind of code that people write when they're tacking the latest new feature onto someplace that it doesn't belong. This code isn't just semantically wrong, it's stylistically wrong. Having a less schizophrenic language helps jr guys avoid code like the above.


    const caps = new Array(26).map((item, index) => String.fromCharCode(65 + index));
It's not wrong. Could maybe be better though.


Longer, harder to reason against...

    const caps = new Array(26).map((item, index) => String.fromCharCode(65 + index));
Shorter, easier to reason against.

    const caps = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');


LGTM


It is wrong. "new Array(26)" creates an Array with preallocated space for 26 elements (instead of 26 'undefined' elements, as one might expect), and calling .map() on it will return another empty array. The semantics here are particularly difficult to reason about because map is specified to not execute its callback on undefined elements.

Someone will probably chime in here with a correction, but the point is this -- JS is NOT a friendly language to deal with.


It will iterate just fine, there are no values (undefined) for each `0` through `length-1`

    new Array(26).length === 26; //true


Seems like it doesn't, I recently read about this gotcha so I tested it out in JSBin. Using lodash map works as expected, though.

JSBin: http://jsbin.com/vasulumeti/1/edit?js,console

StackOverflow: http://stackoverflow.com/a/5501711/211291


Actually this code is very stupid and you're half correct. The standard built in Array methods like map or forEach don't work on implicitly defined undefined values but on explicit one they do.

For example: [1,2,3,undefined].forEach(ele => console.log(ele)) > 1 > 2 > 3 > undefined On the other hand: [1,2,3,,].forEach(ele => console.log(ele)) > 1 > 2 > 3

So, take the time you dedicate to hating JavaScript to understand the language instead of bitching here you and that stupid jerk above.


As others have pointed out, the semantics are pretty hard to deal with. But, it's the style that I think is the really bad part of this snippet. Why would you create an array of empty elements to iterate over just to grab the index? In CoffeeScript, however, the language guides you to this solution:

    [65..90].map (number) -> String.fromCharCode(number)
where you're operating on the elements of your array and you don't care about their index.


> Why would you create an array of empty elements to iterate over just to grab the index?

Because it's the shortest way to do it in that language.

I think something like:

    [A-Z].reverse
or:

    reverse([A-Z])
might even be better. Do you know of a language with that syntax?


If you want an index, use an index:

    for (1 = 65; i <= 90; i++){...}
It's not like the syntax is that bad. But, iterators are simpler than indexing a collection. Iterators work with data structures like linked lists where you don't really have an index. And, they work when the collection changes while you're iterating it.


But you can't also output an array without additional lines of code.


You still haven't realized that _your_ solution won't output anything without additional lines of code, have you?


I have, except that it's not really my solution. I was just cleaning up the example to make it more readable, as that seemed to be one of the complaints of the OP.


Unfortunately that won't work as expected, you'll need something ugly like:

  Array.apply(null, Array(26)).map((_, index) => String.fromCharCode(65 + index))




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: