Merge bindings function

Value: [0.0001 to 0.005]
This commit is contained in:
Sam Nolan 2022-05-09 14:18:50 +00:00
parent 6ed0d0cd6a
commit 2860f28e32
3 changed files with 28 additions and 4 deletions

View File

@ -9,3 +9,5 @@ import SquigglePlayground, {
renderSquigglePlaygroundToDom, renderSquigglePlaygroundToDom,
} from "./components/SquigglePlayground"; } from "./components/SquigglePlayground";
export { SquigglePlayground, renderSquigglePlaygroundToDom }; export { SquigglePlayground, renderSquigglePlaygroundToDom };
export { mergeBindings } from "@quri/squiggle-lang";

View File

@ -1,4 +1,9 @@
import { Distribution, resultMap, defaultBindings } from "../../src/js/index"; import {
Distribution,
resultMap,
defaultBindings,
mergeBindings,
} from "../../src/js/index";
import { testRun, testRunPartial } from "./TestHelpers"; import { testRun, testRunPartial } from "./TestHelpers";
function Ok<b>(x: b) { function Ok<b>(x: b) {
@ -66,6 +71,17 @@ describe("Partials", () => {
value: 10, value: 10,
}); });
}); });
test("Can merge bindings from three partials", () => {
let bindings1 = testRunPartial(`x = 1`);
let bindings2 = testRunPartial(`y = 2`);
let bindings3 = testRunPartial(`z = 3`);
expect(
testRun(`x + y + z`, mergeBindings([bindings1, bindings2, bindings3]))
).toEqual({
tag: "number",
value: 6,
});
});
}); });
describe("JS Imports", () => { describe("JS Imports", () => {

View File

@ -48,7 +48,7 @@ export function run(
let i = imports ? imports : defaultImports; let i = imports ? imports : defaultImports;
let e = environment ? environment : defaultEnvironment; let e = environment ? environment : defaultEnvironment;
let res: result<expressionValue, errorValue> = evaluateUsingOptions( let res: result<expressionValue, errorValue> = evaluateUsingOptions(
{ externalBindings: mergeImports(b, i), environment: e }, { externalBindings: mergeImportsWithBindings(b, i), environment: e },
squiggleString squiggleString
); );
return resultMap(res, (x) => createTsExport(x, e)); return resultMap(res, (x) => createTsExport(x, e));
@ -67,12 +67,12 @@ export function runPartial(
return evaluatePartialUsingExternalBindings( return evaluatePartialUsingExternalBindings(
squiggleString, squiggleString,
mergeImports(b, i), mergeImportsWithBindings(b, i),
e e
); );
} }
function mergeImports( function mergeImportsWithBindings(
bindings: externalBindings, bindings: externalBindings,
imports: jsImports imports: jsImports
): externalBindings { ): externalBindings {
@ -90,6 +90,12 @@ type jsImports = { [key: string]: jsValue };
export let defaultImports: jsImports = {}; export let defaultImports: jsImports = {};
export let defaultBindings: externalBindings = {}; export let defaultBindings: externalBindings = {};
export function mergeBindings(
allBindings: externalBindings[]
): externalBindings {
return allBindings.reduce((acc, x) => ({ ...acc, ...x }));
}
function createTsExport( function createTsExport(
x: expressionValue, x: expressionValue,
environment: environment environment: environment