time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

sampleset.js (6779B)


      1 import * as SampleSetDist from "../dist/SampleSetDist/index.js";
      2 import { makeDefinition, } from "../library/registry/fnDefinition.js";
      3 import { frArray, frDist, frLambda, frLambdaN, frNumber, } from "../library/registry/frTypes.js";
      4 import { FnFactory, doNumberLambdaCall, repackDistResult, } from "../library/registry/helpers.js";
      5 import { REExpectedType } from "../errors/messages.js";
      6 import { Ok } from "../utility/result.js";
      7 import { vArray, vNumber } from "../value/index.js";
      8 const maker = new FnFactory({
      9     nameSpace: "SampleSet",
     10     requiresNamespace: true,
     11 });
     12 export function sampleSetAssert(dist) {
     13     if (dist instanceof SampleSetDist.SampleSetDist) {
     14         return;
     15     }
     16     throw new REExpectedType("SampleSetDist", dist.toString());
     17 }
     18 const fromDist = makeDefinition([frDist], ([dist], { environment }) => repackDistResult(SampleSetDist.SampleSetDist.fromDist(dist, environment)));
     19 const fromNumber = makeDefinition([frNumber], ([number], context) => repackDistResult(SampleSetDist.SampleSetDist.make(new Array(context.environment.sampleCount).fill(number))));
     20 const fromList = makeDefinition([frArray(frNumber)], ([numbers]) => repackDistResult(SampleSetDist.SampleSetDist.make(numbers)));
     21 const fromFn = (lambda, context, fn) => repackDistResult(SampleSetDist.SampleSetDist.fromFn((index) => {
     22     return doNumberLambdaCall(lambda, fn(index), context);
     23 }, context.environment));
     24 const fromFnDefinitions = [
     25     makeDefinition([frLambdaN(0)], ([lambda], context) => {
     26         return fromFn(lambda, context, () => []);
     27     }),
     28     makeDefinition([frLambdaN(1)], ([lambda], context) => {
     29         return fromFn(lambda, context, (index) => [vNumber(index)]);
     30     }),
     31 ];
     32 const baseLibrary = [
     33     maker.make({
     34         name: "fromDist",
     35         examples: [`SampleSet.fromDist(normal(5,2))`],
     36         definitions: [fromDist],
     37     }),
     38     maker.make({
     39         name: "fromNumber",
     40         examples: [`SampleSet.fromNumber(3)`],
     41         definitions: [fromNumber],
     42     }),
     43     maker.make({
     44         name: "fromList",
     45         examples: [`SampleSet.fromList([3,5,2,3,5,2,3,5,2,3,3,5,3,2,3,1,1,3])`],
     46         output: "Dist",
     47         definitions: [fromList],
     48     }),
     49     maker.make({
     50         name: "toList",
     51         examples: [`SampleSet.toList(SampleSet.fromDist(normal(5,2)))`],
     52         output: "Array",
     53         definitions: [
     54             makeDefinition([frDist], ([dist]) => {
     55                 sampleSetAssert(dist);
     56                 return vArray(dist.samples.map(vNumber));
     57             }),
     58         ],
     59     }),
     60     maker.make({
     61         name: "fromFn",
     62         examples: [`SampleSet.fromFn({|i| sample(normal(5,2))})`],
     63         output: "Dist",
     64         definitions: fromFnDefinitions,
     65     }),
     66     maker.make({
     67         name: "make",
     68         output: "Dist",
     69         definitions: [fromDist, fromNumber, fromList, ...fromFnDefinitions],
     70     }),
     71     maker.make({
     72         name: "map",
     73         examples: [`SampleSet.map(SampleSet.fromDist(normal(5,2)), {|x| x + 1})`],
     74         output: "Dist",
     75         definitions: [
     76             makeDefinition([frDist, frLambda], ([dist, lambda], context) => {
     77                 sampleSetAssert(dist);
     78                 return repackDistResult(dist.samplesMap((r) => Ok(doNumberLambdaCall(lambda, [vNumber(r)], context))));
     79             }),
     80         ],
     81     }),
     82     maker.make({
     83         name: "map2",
     84         examples: [
     85             `SampleSet.map2(SampleSet.fromDist(normal(5,2)), SampleSet.fromDist(normal(5,2)), {|x, y| x + y})`,
     86         ],
     87         output: "Dist",
     88         definitions: [
     89             makeDefinition([frDist, frDist, frLambda], ([dist1, dist2, lambda], context) => {
     90                 sampleSetAssert(dist1);
     91                 sampleSetAssert(dist2);
     92                 return repackDistResult(SampleSetDist.map2({
     93                     fn: (a, b) => Ok(doNumberLambdaCall(lambda, [vNumber(a), vNumber(b)], context)),
     94                     t1: dist1,
     95                     t2: dist2,
     96                 }));
     97             }),
     98         ],
     99     }),
    100     maker.make({
    101         name: "map3",
    102         examples: [
    103             `SampleSet.map3(SampleSet.fromDist(normal(5,2)), SampleSet.fromDist(normal(5,2)), SampleSet.fromDist(normal(5,2)), {|x, y, z| max([x,y,z])})`,
    104         ],
    105         output: "Dist",
    106         definitions: [
    107             makeDefinition([frDist, frDist, frDist, frLambda], ([dist1, dist2, dist3, lambda], context) => {
    108                 sampleSetAssert(dist1);
    109                 sampleSetAssert(dist2);
    110                 sampleSetAssert(dist3);
    111                 return repackDistResult(SampleSetDist.map3({
    112                     fn: (a, b, c) => Ok(doNumberLambdaCall(lambda, [vNumber(a), vNumber(b), vNumber(c)], context)),
    113                     t1: dist1,
    114                     t2: dist2,
    115                     t3: dist3,
    116                 }));
    117             }),
    118         ],
    119     }),
    120     maker.make({
    121         name: "mapN",
    122         examples: [
    123             `SampleSet.mapN([SampleSet.fromDist(normal(5,2)), SampleSet.fromDist(normal(5,2)), SampleSet.fromDist(normal(5,2))], {|x| max(x)})`,
    124         ],
    125         output: "Dist",
    126         definitions: [
    127             makeDefinition([frArray(frDist), frLambda], ([dists, lambda], context) => {
    128                 const sampleSetDists = dists.map((d) => {
    129                     sampleSetAssert(d);
    130                     return d;
    131                 });
    132                 return repackDistResult(SampleSetDist.mapN({
    133                     fn: (a) => Ok(doNumberLambdaCall(lambda, [vArray(a.map(vNumber))], context)),
    134                     t1: sampleSetDists,
    135                 }));
    136             }),
    137         ],
    138     }),
    139 ];
    140 const mkComparison = (name, withDist, withFloat) => maker.make({
    141     name,
    142     requiresNamespace: false,
    143     examples: [
    144         `SampleSet.${name}(SampleSet.fromDist(normal(5,2)), SampleSet.fromDist(normal(6,2)))`,
    145         `SampleSet.${name}(SampleSet.fromDist(normal(5,2)), 3.0)`,
    146         `SampleSet.${name}(4.0, SampleSet.fromDist(normal(6,2)))`,
    147     ],
    148     output: "Dist",
    149     definitions: [
    150         makeDefinition([frDist, frDist], ([dist1, dist2]) => {
    151             sampleSetAssert(dist1);
    152             sampleSetAssert(dist2);
    153             return repackDistResult(withDist(dist1, dist2));
    154         }),
    155         makeDefinition([frDist, frNumber], ([dist, f]) => {
    156             sampleSetAssert(dist);
    157             return repackDistResult(withFloat(dist, f));
    158         }),
    159         makeDefinition([frNumber, frDist], ([f, dist]) => {
    160             sampleSetAssert(dist);
    161             return repackDistResult(withFloat(dist, f));
    162         }),
    163     ],
    164 });
    165 const comparisonLibrary = [
    166     mkComparison("min", SampleSetDist.minOfTwo, SampleSetDist.minOfFloat),
    167     mkComparison("max", SampleSetDist.maxOfTwo, SampleSetDist.maxOfFloat),
    168 ];
    169 export const library = [...baseLibrary, ...comparisonLibrary];
    170 //# sourceMappingURL=sampleset.js.map