Simple dict merge and mergeMany

This commit is contained in:
Ozzie Gooen 2022-06-07 22:35:40 -07:00
parent 0fcf7e197e
commit 8d1d08eaa0
4 changed files with 62 additions and 2 deletions

View File

@ -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)

View File

@ -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<Js.Dict.t<frValue>, err> => {
switch values {
| FRValueDict(a1) => Ok(a1)
| _ => Error(impossibleError)
}
}
module ToTypedArray = {
let numbers = (inputs: ts): result<array<float>, err> => {
let openNumbers = (elements: array<t>) =>
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<array<Js.Dict.t<frValue>>, err> => {
Js.log2("HIHIHI", "HI")
let openDicts = (elements: array<t>) => 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)
}
}
}

View File

@ -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=[

View File

@ -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
}