Created
September 25, 2019 07:40
-
-
Save Sidd27/f420d400906aa60059906abc6aafea53 to your computer and use it in GitHub Desktop.
Init+each+accumulate+push -> scan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.scan = function (callback, initialValue) { | |
const appendAggregate = (acc, item) => { | |
const aggregate = acc[acc.length-1] //get last item | |
const newAggregate = callback(aggregate, item) | |
return [...acc, newAggregate] | |
} | |
const accumulator = [initialValue] | |
return this.reduce(appendAggregate, accumulator) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment