From fc3a7c6cf98d3473a99e030e27f98882a82dccfc Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Thu, 1 Sep 2022 19:11:49 +0400 Subject: [PATCH] support for __result__ in web components and SqLambda --- .../SquiggleViewer/ExpressionViewer.tsx | 2 +- packages/squiggle-lang/src/js/SqLambda.ts | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/components/src/components/SquiggleViewer/ExpressionViewer.tsx b/packages/components/src/components/SquiggleViewer/ExpressionViewer.tsx index 4dc6ef98..1aa106c8 100644 --- a/packages/components/src/components/SquiggleViewer/ExpressionViewer.tsx +++ b/packages/components/src/components/SquiggleViewer/ExpressionViewer.tsx @@ -248,7 +248,7 @@ export const ExpressionViewer: React.FC = ({ value, width }) => { {(_) => value.value .entries() - .filter(([key, _]) => !key.match(/^(Math|System)\./)) + .filter(([key, _]) => !key.match(/^(__result__)$/)) .map(([key, r]) => ( `"${arg.replace(new RegExp('"', "g"), '\\"')}"`; const argsSource = args .map((arg) => (typeof arg === "number" ? arg : quote(arg))) .join(","); - const functionNameSource = this.location.path.items + + // end expression values are exposed in bindings via secret `__result__` variable and we can access them through it + const pathItems = [ + ...(this.location.path.root === "result" ? ["__result__"] : []), + ...this.location.path.items, + ]; + + // full function name, e.g. foo.bar[3].baz + const functionNameSource = pathItems .map((item, i) => typeof item === "string" ? (i ? "." + item : item) : `[${item}]` ) .join(""); + + // something like: foo.bar[3].baz(1,2,3) const source = `${functionNameSource}(${argsSource})`; + project.setSource(callId, source); project.setContinues(callId, [sourceId]); project.run(callId);