Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DataStore {
- constructor(values) {
- this.values = values;
- }
- forEachData(callback) {
- // the callback is invoked with items specific to the logic of DataStore
- callback(0);
- callback(1);
- for (const x of this.values) {
- callback(x);
- }
- for (const x of this.values) {
- callback(2 * x);
- }
- }
- }
- // Inside my function
- const data_store = ...; // DataStore object
- const aggregate_op = (accumulator, current_value) => Math.min(accumulator, f(current_value));
- // I'm wondering if there is a built-in language or library feature that does this kind of reduction:
- let answer = 0;
- data_store.forEachData(x => {
- answer = aggregate_op(answer, x);
- });
- const arr = ...;
- let answer = 0;
- arr.forEach(x => {
- arr = aggregate_op(arr, x);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement