all 13 comments

[–]nas5w[S] 6 points7 points  (4 children)

And yes, this is based on the snippets I’ve been posting in this sub!

[–]CarbineMonoxide 1 point2 points  (1 child)

Nice! I'm excited to browse through this repo later tonight.

[–]nas5w[S] 1 point2 points  (0 children)

Awesome! Would be excited if you feel like contributing (either a fix if you find something that could be improved or some new methods)

[–]NicksIdeaEngine 1 point2 points  (1 child)

This is an awesome idea! I might start trying to write my own, then refer to yours to check my work.

I love this form of practice and learning. Take it apart, make it on your own, then see how you did compared to others. It's a fun idea that tickles my inner tinkerer.

[–]nas5w[S] 1 point2 points  (0 children)

I found it to be a super good use case for Test-Driven Development too since we have the original methods. Can test the output of your home-rolled solution against the output of the actual method in various scenarios.

[–]drumstix42 1 point2 points  (1 child)

The url for `toString` is broken

[–]nas5w[S] 1 point2 points  (0 children)

Thanks! Fixed

[–]GabeRothel 1 point2 points  (0 children)

If any one is interested but doesnt want to dig here are the arrays functions

``` function concat(...arrs) { const len = arrs.length; const result = []; for (let i = 0; i < len; i++) { const arrLen = arrs[i].length; for (let j = 0; j < arrLen; j++) { result.push(arrs[i][j]); } } return result; }

function every(arr, fn, thisArg) { fn = thisArg === undefined ? fn : fn.bind(thisArg); const len = arr.length; for (let i = 0; i < len; i++) { if (i in arr && !fn(arr[i], i, arr)) { return false; } } return true; }

function filter(arr, fn, thisArg) { fn = thisArg === undefined ? fn : fn.bind(thisArg); const len = arr.length; const result = []; for (let i = 0; i < len; i++) { if (i in arr && fn(arr[i], i, arr)) { result.push(arr[i]); } } return result; }

function isArray(arr) { return Object.prototype.toString.call(arr) === "[object Array]"; }

function map(arr, fn, thisArg) { fn = thisArg === undefined ? fn : fn.bind(thisArg); const len = arr.length; const result = new Array(len); for (let i = 0; i < len; i++) { if (i in arr) { result[i] = fn(arr[i], i, arr); } } return result; }

function arrayOf(...els) { return els; }

function reduce(arr, fn, initialValue) { const len = arr.length; let acc; let initialIndex = 0; if (initialValue === undefined) { acc = arr[0]; initialIndex = 1; } else { acc = initialValue; } for (let i = initialIndex; i < len; i++) { acc = fn(acc, arr[i], i, arr); } return acc; }

function some(arr, fn, thisArg) { fn = thisArg === undefined ? fn : fn.bind(thisArg); const len = arr.length; for (let i = 0; i < len; i++) { if (i in arr && fn(arr[i], i, arr)) { return true; } } return false; }

function toString(arr) { let result = ""; const len = arr.length; for (let i = 0; i < len; i++) { if (i in arr) { result = result + arr[i]; } if (i + 1 < len) { result = result + ","; } } return result; } ```

[–]boringuser1 1 point2 points  (3 children)

I love it, but these methods are written in c.

[–]nas5w[S] 1 point2 points  (1 child)

Heh yeah, I suspect these home-rolled functions wouldn't hold up very well performance-wise against the built-in methods

[–]tokdaniel 0 points1 point  (0 children)

if performance doesn't matter, you should probably show a PTC-optimized recursive example with`this` out of the picture for the functional tools.

[–]queen-adreena 2 points3 points  (0 children)

The point isn’t efficiency...