3 ms·
This is great and allows us to do some slicker functional code: function take(num, list) { if (num <= 0 || !list || list === []) return []; var
by STRML 13y ago
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.
- vjeux 13y agoNote: 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.
- sebastianmck 13y agoUse `!list.length` instead.
- STRML 13y agoAh, yeah, of course. That always threw me off, I've been working too much in other languages.