Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

Part 1: Generators

node v14.20.1
version: 2.0.0
endpointsharetweet
Let's define our minimal framework
const prand = require('pure-rand'); class Random { constructor(rng) { this.rng = rng; } next(min, max) { const g = prand.uniformIntDistribution(min, max, this.rng); this.rng = g[1]; return g[0]; } } function map(g, mapper) { return { generate(mrng) { const value = g.generate(mrng); return mapper(value); } }; } const miniFc = {} miniFc.integer = (min, max) => { return { generate(mrng) { return mrng.next(min, max); } }; } miniFc.boolean = () => map( miniFc.integer(0, 1), Boolean ) miniFc.character = () => map( miniFc.integer(0, 25), n => String.fromCharCode(97 + n) ) miniFc.tuple = (...itemGenerators) => { return { generate(mrng) { return itemGenerators.map(g => g.generate(mrng)); } }; } miniFc.array = (itemGenerator) => { return { generate(mrng) { const size = mrng.next(0, 10); const content = []; for (let index = 0 ; index !== size ; ++index) { content.push(itemGenerator.generate(mrng)); } return content; } }; } miniFc.string = () => map( miniFc.array(miniFc.character()), characters => characters.join('') ) miniFc.dictionary = (valueGenerator) => map( miniFc.array( miniFc.tuple( miniFc.string(), valueGenerator ) ), Object.fromEntries )
Let's use it!
const seed = 0; const mrng = new Random(prand.xoroshiro128plus(seed)); const generator = miniFc.array(miniFc.character()); console.log(generator.generate(mrng)); console.log(generator.generate(mrng));
Loading…

no comments

    sign in to comment