SHARE
TWEET

Untitled

a guest Jun 15th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class DataStore {
  2.   constructor(values) {
  3.     this.values = values;
  4.   }
  5.   forEachData(callback) {
  6.     // the callback is invoked with items specific to the logic of DataStore
  7.     callback(0);
  8.     callback(1);
  9.     for (const x of this.values) {
  10.       callback(x);
  11.     }
  12.     for (const x of this.values) {
  13.       callback(2 * x);
  14.     }
  15.   }
  16. }
  17.      
  18. // Inside my function
  19. const data_store = ...; // DataStore object
  20. const aggregate_op = (accumulator, current_value) => Math.min(accumulator, f(current_value));
  21.  
  22. // I'm wondering if there is a built-in language or library feature that does this kind of reduction:
  23. let answer = 0;
  24. data_store.forEachData(x => {
  25.   answer = aggregate_op(answer, x);
  26. });
  27.      
  28. const arr = ...;
  29. let answer = 0;
  30. arr.forEach(x => {
  31.   arr = aggregate_op(arr, x);
  32. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top