From 2d59898b4fa1147e5220f9a8482f60ef1b0a3c5c Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Thu, 25 Aug 2022 21:59:15 +0200 Subject: [PATCH] topology work in progress --- .../ReducerProject_Topology_test.res | 16 ++++++++++++ .../ReducerProject_Topology.res | 25 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_Topology_test.res diff --git a/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_Topology_test.res b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_Topology_test.res new file mode 100644 index 00000000..fa96a15b --- /dev/null +++ b/packages/squiggle-lang/__tests__/ReducerProject/ReducerProject_Topology_test.res @@ -0,0 +1,16 @@ +@@warning("-44") +module Topology = ReducerProject_Topology + +open Jest +open Expect +open Expect.Operators + +describe("Topology", () => { + Only.test("when equal 1x", () => { + Topology.runOrderDiff(["a"], ["a"])->expect == [] + }) + + test("when equal 3x", () => { + Topology.runOrderDiff(["a", "b", "c"], ["a", "b", "c"])->expect == [] + }) +}) diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_Topology.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_Topology.res index f23c19c1..0a955998 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_Topology.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_Topology.res @@ -70,3 +70,28 @@ let getDependents = (this: t, sourceId: string): array => { let (_, dependents) = getTopologicalSortFor(this, sourceId) dependents } + +let runOrderDiff = (current: array, previous0: array): array => { + let extraLength = + Belt.Array.length(current) > Belt.Array.length(previous0) + ? Belt.Array.length(current) - Belt.Array.length(previous0) + : 0 + let previous = Belt.Array.copy(previous0) + let filler = Belt.Array.make(extraLength, "") + Belt.Array.forEach(filler, _ => { + let _ = Js.Array2.push(previous, "") + }) + let zipped = Belt.Array.zip(current, previous) + let (_, affected) = Belt.Array.reduce(zipped, (true, []), ((wasEqual, acc), (curr, prev)) => { + switch wasEqual { + | true => + if curr == prev { + (true, Belt.Array.concat(acc, [curr])) + } else { + (false, acc) + } + | false => (false, acc) + } + }) + affected +}