From 8de8cc3633bb0e6d0616fea98929ba2fe794252b Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Tue, 23 Aug 2022 13:59:18 +0200 Subject: [PATCH] tutorial calling functions --- ...erProject_tutorial_4_calling_functions.res | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_4_calling_functions.res diff --git a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_4_calling_functions.res b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_4_calling_functions.res new file mode 100644 index 00000000..d595f35a --- /dev/null +++ b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_tutorial_4_calling_functions.res @@ -0,0 +1,39 @@ +@@warning("-44") +module InternalExpressionValue = ReducerInterface_InternalExpressionValue +module Project = ForTS_ReducerProject +module Bindings = Reducer_Bindings + +open Jest +open Expect +open Expect.Operators + +describe("ReducerProject Tutorial", () => { + /* Let's build a project to provide a function. */ + /* But we will call that function on an array of user input. */ + let project = Project.createProject() + Project.setSource(project, "library", "double(x) = x * 2") + /* userCode is not here yet but its dependency is fixed. So we can set it once and for all */ + Project.setContinues(project, "userCode", ["library"]) + + let userValues = [1, 2, 3, 4, 5] + + let userResults = Belt.Array.map(userValues, aUserValue => { + let userCode = `double(${aUserValue->Js.Int.toString})` + /* Put the constructed source in the project */ + /* We have already set that it depends on "library" */ + Project.setSource(project, "userCode", userCode) + /* Run the project */ + Project.runAll(project) + /* Get the result */ + Project.getResult(project, "userCode") + /* I have to remind you that the "library" is run only once and for all. + The library is not run for each user value. */ + }) + + test("userResults", () => { + let userResultsAsString = Belt.Array.map(userResults, aResult => + aResult->InternalExpressionValue.toStringOptionResult + ) + userResultsAsString->expect == ["Ok(2)", "Ok(4)", "Ok(6)", "Ok(8)", "Ok(10)"] + }) +})