3 ms·
Pretty cool. Few suggestions (which may or may not require babel): - `f.apply(null, args)` calls can be replaced with `f(...args)` - Your head function return
by olalonde 11y ago
Pretty cool. Few suggestions (which may or may not require babel):
- `f.apply(null, args)` calls can be replaced with `f(...args)`
- Your head function returns the first argument while your last function returns an array containing the last argument. I'd rewrite those functions as:
const head = (x) => x;
const last = (...xs) => xs[xs.length-1];
const tail = (x, ...xs) => xs;
const init = (...xs) => xs.slice(0, -1);
(Unfortunately it seems the rest operator can only appear as the last argument.)
- Why put everything on the prototype? It doesn't seem necessary since you are not using the `this` keyword. I would just stick the `export` keyword in front of functions to be exported.
Looking forward to part 2.
- nunull 11y ago> head extracts the first element of a list, which must be non-empty. Those functions operate on lists. I think it should read as follows. const head = (xs) => xs[0]; const last = (xs) => xs[xs.length-1]; const tail = (xs) => xs.slice(1); const init = (xs) => xs.slice(0, -1); Am I missing something?
- olalonde 11y agoFor some reason, the original author decided the arguments themselves were the "list" to operate on which is indeed a bit weird and non idiomatic.
- heydenberk 11y agoYou can also avoid reversing the arrays before reducing them by calling the method `reduceRight`.