Skip to content

Instantly share code, notes, and snippets.

@varemenos
Last active June 25, 2017 22:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save varemenos/9fabf3b09e86b8eac6f2 to your computer and use it in GitHub Desktop.
Save varemenos/9fabf3b09e86b8eac6f2 to your computer and use it in GitHub Desktop.
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.

ToC

  1. concat
  2. every
  3. filter
  4. findIndex
  5. forEach
  6. indexOf
  7. join
  8. map
  9. reverse
  10. some

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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment