Merge pull request #495 from quantified-uncertainty/merge-bindings
Merge bindings function
This commit is contained in:
commit
d07c3e7e81
|
@ -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";
|
||||||
|
|
|
@ -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", () => {
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user