diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res index a0487e80..82e58eff 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res @@ -100,7 +100,8 @@ module FRType = { let el = elements->E.A2.fmap(matchWithExpressionValue(intendedType)) E.A.O.openIfAllSome(el)->E.O2.fmap(r => FRValueArray(r)) } - | (FRTypeDict(r), EvRecord(record)) => record + | (FRTypeDict(r), EvRecord(record)) => + record ->Js.Dict.entries ->E.A2.fmap(((key, item)) => matchWithExpressionValue(r, item)->E.O2.fmap(o => (key, o))) ->E.A.O.openIfAllSome @@ -133,7 +134,10 @@ module FRType = { } | FRValueDict(frValueRecord) => { let record = - frValueRecord->Js.Dict.entries->E.A2.fmap(((name, value)) => (name, matchReverse(value)))->E.Dict.fromArray + frValueRecord + ->Js.Dict.entries + ->E.A2.fmap(((name, value)) => (name, matchReverse(value))) + ->E.Dict.fromArray EvRecord(record) } | FRValueLambda(l) => EvLambda(l) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res index 29535c05..ecbb461c 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res @@ -7,6 +7,7 @@ module Wrappers = { let evDistribution = r => ReducerInterface_ExpressionValue.EvDistribution(r) let evNumber = r => ReducerInterface_ExpressionValue.EvNumber(r) let evArray = r => ReducerInterface_ExpressionValue.EvArray(r) + let evRecord = r => ReducerInterface_ExpressionValue.EvRecord(r) let symbolicEvDistribution = r => r->DistributionTypes.Symbolic->evDistribution } @@ -94,12 +95,26 @@ module Prepare = { } } + let oneDict = (values: t): result, err> => { + switch values { + | FRValueDict(a1) => Ok(a1) + | _ => Error(impossibleError) + } + } + module ToTypedArray = { let numbers = (inputs: ts): result, err> => { let openNumbers = (elements: array) => elements->E.A2.fmap(oneNumber)->E.A.R.firstErrorOrOpen inputs->E.A.unsafe_get(0)->ToValueArray.Array.openA->E.R.bind(openNumbers) } + + let dicts = (inputs: ts): Belt.Result.t>, err> => { + Js.log2("HIHIHI", "HI") + let openDicts = (elements: array) => elements->E.A2.fmap(oneDict)->E.A.R.firstErrorOrOpen + Js.log2("Inputs?", inputs); + inputs->E.A.unsafe_get(0)->ToValueArray.Array.openA->E.R.bind(openDicts) + } } } diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res index 21d61508..2d8c09c0 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res @@ -193,6 +193,43 @@ let registry = [ ), ], ), + Function.make( + ~name="Dict.merge", + ~definitions=[ + FnDefinition.make( + ~name="merge", + ~inputs=[FRTypeDict(FRTypeAny), FRTypeDict(FRTypeAny)], + ~run=(inputs, _) => { + switch inputs { + | [FRValueDict(d1), FRValueDict(d2)] => { + let newDict = + E.Dict.concat(d1, d2) |> Js.Dict.map((. r) => + FunctionRegistry_Core.FRType.matchReverse(r) + ) + newDict->Wrappers.evRecord->Ok + } + | _ => Error(impossibleError) + } + }, + ), + ], + ), + //TODO: Make sure that two functions cant have the same name. This causes chaos elsewhere. + Function.make( + ~name="Dict.mergeMany", + ~definitions=[ + FnDefinition.make(~name="mergeMany", ~inputs=[FRTypeArray(FRTypeDict(FRTypeAny))], ~run=( + inputs, + _, + ) => + inputs + ->Prepare.ToTypedArray.dicts + ->E.R2.fmap(E.Dict.concatMany) + ->E.R2.fmap(Js.Dict.map((. r) => FunctionRegistry_Core.FRType.matchReverse(r))) + ->E.R2.fmap(Wrappers.evRecord) + ), + ], + ), Function.make( ~name="List.make", ~definitions=[ diff --git a/packages/squiggle-lang/src/rescript/Utility/E.res b/packages/squiggle-lang/src/rescript/Utility/E.res index 7dd1d5f2..d170a930 100644 --- a/packages/squiggle-lang/src/rescript/Utility/E.res +++ b/packages/squiggle-lang/src/rescript/Utility/E.res @@ -540,6 +540,7 @@ module A = { let hasBy = (r, fn) => Belt.Array.getBy(r, fn) |> O.isSome let fold_left = Array.fold_left let fold_right = Array.fold_right + let concat = Belt.Array.concat let concatMany = Belt.Array.concatMany let keepMap = Belt.Array.keepMap let slice = Belt.Array.slice @@ -874,4 +875,7 @@ module Dict = { let get = Js.Dict.get let keys = Js.Dict.keys let fromArray = Js.Dict.fromArray + let toArray = Js.Dict.entries + let concat = (a, b) => A.concat(toArray(a), toArray(b))->fromArray + let concatMany = ts => ts->A2.fmap(toArray)->A.concatMany->fromArray }