 varemenos
10/4/2015 - 9:09 PM

## Utilizing Array.prototype.reduce to replicate methods in Array's prototype

Utilizing Array.prototype.reduce to replicate methods in Array's prototype

# Reduce.js

This is an exercise and a challenge to see how far can I go with reduce and how many built-in Array methods I can replicate with it.

## Notes

1. I use ES2015 arrow functions in all the examples below, that's because arrow functions make the code easier to read and in most cases turn the code snippet into a bite-sized one.

2. The first part of the code is used to generate random values for the reduce function to work with:

``````var arr = new Array(100)
.fill('')
.map(() => Math.floor(Math.random() * 10))
``````

## Example

So for example if we want to use concat, we will do the following:

``````// define the arr array which contains our sample
var arr = new Array(100)
.fill('')
.map(() => Math.floor(Math.random() * 10));

// definition the concat function utilizing reduce
var concat = (arr, arr2) =>
arr2.reduce((memo, current) => {
return memo.push(current) && memo;
}, arr);

// and then use concat on arr and another array as the 2nd parameter of concat
concat(arr, [1,2,3]);
``````
``````// definition
var concat = (arr, arr2) =>
arr2.reduce((memo, current) => {
return memo.push(current) && memo;
}, arr);

// usage
concat(arr, [1,2,3]);``````
``````// definition
var every = (arr, check) =>
arr.reduce((memo, current) => {
return memo && check(current) ? true : false;
}, true);

// usage
every(arr, (num) => num >= 0);
every(arr, (num) => num % 2 === 0);
``````
``````// definition
var filter = (arr, check) =>
arr.reduce((memo, current) => {
return check(current) ? memo.push(current) && memo : memo;
}, []);

// usage
filter(arr, (num) => num === 5);
filter(arr, (num) => num % 2 === 0);
``````
``````// definition
var findIndex = (arr, check) =>
arr.reduce((memo, current, index) => {
return !memo && check(current) ? index : memo;
}, undefined);

// usage
findIndex(arr, (num) => num === 5);
findIndex(arr, (num) => num % 2 === 0);
``````
``````// definition
var forEach = (arr, check) =>
arr.reduce((memo, current, index) => {
check(current, index);
});

// usage
forEach(arr, (item, index) => console.log(item, index));
``````
``````// definition
var indexOf = (arr, val) =>
arr.reduce((memo, current, index) => {
return memo === -1 && val === current ? index : memo;
}, -1);

// usage
indexOf(arr, 5);
``````
``````// definition
var join = (arr, separator) =>
arr.reduce((memo, current) => {
return memo === '' ? memo + current : memo + (separator || '') + current;
}, '');

// usage
join(['a', 'b', 3], '.');``````
``````// definition
var map = (arr, check) =>
arr.reduce((memo, current) => {
return memo.push(check(current)) && memo;
}, []);

// usage
map(arr, (item) => item * 5);``````
``````// definition
var reverse = (arr) =>
arr.reduce((memo, current) => {
return memo.unshift(current) && memo;
}, []);

// usage
reverse(arr);
``````
``````// definition
var some = (arr, check) =>
arr.reduce((memo, current) => {
return !memo && check(current) ? true : memo;
}, false);

// usage
some(arr);
``````