Skip to content

Instantly share code, notes, and snippets.

@tr00gle
Last active December 3, 2018 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tr00gle/06720cec0917bace762d8e3929284d97 to your computer and use it in GitHub Desktop.
Save tr00gle/06720cec0917bace762d8e3929284d97 to your computer and use it in GitHub Desktop.
// a meaningful use of reducing an array to an object
//steps:
// 1. use function to generate random ints in [0,n); we'll be using 1500 here.
// 2. use another function to create an array of given length filled random ints in
// given range
// 3. declare an object to divide numbers into low, mid, and high buckets
// 4. reduce the array of random integers into the sorting object
// using either anonymous or named callback
// define a helper function for printing the sorting object.
function objectPrinter(sortingResults) {
console.log(`Here are the results of sorting your array: `);
Object.entries(sortingResults).forEach(pair => {
console.log(`There are ${pair[1]} numbers in the ${pair[0]} bucket.`)
});
}
// 1. generate random integer in interval [0, n)
const randomNum = n => Math.floor(Math.random() * n);
// 2. fill an array of len with random integers in [0, 1500)
const randomArrayGenerator = arrayLength => {
const output = [];
for (let i = 0; i < arrayLength; i += 1) {
output.push(randomNum(1500));
}
return output;
}
// alternate function to create array of random integers [0, range) with length len
const randomIntArrayCreator = (len, range) => Array.from({length: len}, () => randomNum(range))
const randomArray = randomArrayGenerator(100);
// 3. sorting object declared
const ranges = {
low: 0,
mid: 0,
high: 0,
}
// 4. reduce with anonymous callback,
// Note: we're using the spread operator to make a shallow copy of ranges object
const firstRun = randomArray.reduce((acc, num) => {
if (num < 500) {
acc.low++
} else if (num < 1000) {
acc.mid++
} else {
acc.high++
}
// to view accumulator status at the end of each iteration
// comment the next line out if you don't need the status of the accumulator at every step
console.log(`current accumulator status: ${JSON.stringify(acc)}`);
return acc;
}, {...ranges});
// 4a. defining named callback
function getBuckets(trackingObject, num) {
if (num < 500) {
trackingObject.low++
} else if (num < 1000) {
trackingObject.mid++
} else {
trackingObject.high++
}
// comment the next line out if you don't need the status of the accumulator at every step
console.log(`current accumulator status: ${JSON.stringify(trackingObject)}`);
return trackingObject;
}
// 5. using reduce with named initialValue and callback
// Note 1: notice we reduce directly on the return value of randomArrayGenerator
// again, using spread operator to shallow copy
const secondRun = randomArrayGenerator(100).reduce(getBuckets, {...ranges});
// feel free to keep following the example on line 72
// change the arguments to fillRandomArray, adjust the ranges in getBuckets and have fun :)
objectPrinter(firstRun);
objectPrinter(secondRun);
console.log('status of ranges:');
objectPrinter(ranges);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment