Merge pull request #495 from quantified-uncertainty/merge-bindings

Merge bindings function
This commit is contained in:
Ozzie Gooen 2022-05-09 14:12:24 -04:00 committed by GitHub
commit d07c3e7e81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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