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

This is great and allows us to do some slicker functional code:

    function take(num, list) {
      if (num <= 0 || !list || list === []) return [];
      var [head, ...tail] = list;
      return [head].concat(take(num - 1, tail));
    }
It's not as elegant as haskell but it's still nicer than it would be without the new syntax.

Once we have support for tail call optimization, things will get really interesting.



Note: list === [] doesn't do what you expect it to do. It's going to return false all the time because it is comparing the actual objects and not their content.


Ah, yeah, of course. That always threw me off, I've been working too much in other languages.


Use `!list.length` instead.




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

Search: