tc39 / proposal-seeded-random

Proposal for an options argument to be added to JS's Math.random() function, and some options to start it with.
MIT License
156 stars 6 forks source link

correct first example #7

Closed GrosSacASac closed 5 years ago

GrosSacASac commented 6 years ago

for(const [i,r] of enumerate(Math.seededPRNG({seed:0}))) { // do something with each value if(i >= limit) break; }

enumerate is not defined

bakkot commented 6 years ago

I imagine you're intended to assume something like

function* enumerate(it) {
  let i = 0;
  for (let r of it) {
    yield [i++, r];
  }
}

(Which isn't to say the example shouldn't be clarified, just explaining what it presumably means.)

GrosSacASac commented 6 years ago

thanks, I think the example should not use a function that only exists in Python by default.

Proposal: What if Math.seededPRNG just returned a function instead of an iterator, it could then be more polymorphic with Math.random which is also just a function and not an iterator.

const seededRandom = Math.seededPRNG({seed:0});
const normalRandom = Math.random;
// ... use normalRandom() and seededRandom() interchangeably
tabatkins commented 6 years ago

You get that from the .next() function on the returned iterator.

GrosSacASac commented 6 years ago

.next() returns {done, value} instead of value like Math.random

tabatkins commented 5 years ago

I've changed up the API a bit, dropping the generator in favor of just making it in invocable function that generates a series of values.