Added dict keys, values, fromList, toList

This commit is contained in:
Ozzie Gooen 2022-06-08 07:24:06 -07:00
parent 8d1d08eaa0
commit 12cdb4ed36
3 changed files with 81 additions and 4 deletions

View File

@ -62,7 +62,7 @@ module FRType = {
let input = ((name, frType): frTypeRecordParam) => `${name}: ${toString(frType)}`
`record({${r->E.A2.fmap(input)->E.A2.joinWith(", ")}})`
}
| FRTypeArray(r) => `record(${toString(r)})`
| FRTypeArray(r) => `list(${toString(r)})`
| FRTypeLambda => `lambda`
| FRTypeString => `string`
| FRTypeVariant(_) => "variant"
@ -73,6 +73,7 @@ module FRType = {
let rec toFrValue = (r: expressionValue): option<frValue> =>
switch r {
| EvNumber(f) => Some(FRValueNumber(f))
| EvString(f) => Some(FRValueString(f))
| EvDistribution(f) => Some(FRValueDistOrNumber(FRValueDist(f)))
| EvLambda(f) => Some(FRValueLambda(f))
| EvArray(elements) =>
@ -88,6 +89,7 @@ module FRType = {
let rec matchWithExpressionValue = (t: t, r: expressionValue): option<frValue> =>
switch (t, r) {
| (FRTypeAny, f) => toFrValue(f)
| (FRTypeString, EvString(f)) => Some(FRValueString(f))
| (FRTypeNumber, EvNumber(f)) => Some(FRValueNumber(f))
| (FRTypeDistOrNumber, EvNumber(f)) => Some(FRValueDistOrNumber(FRValueNumber(f)))
| (FRTypeDistOrNumber, EvDistribution(Symbolic(#Float(f)))) =>

View File

@ -8,6 +8,7 @@ module Wrappers = {
let evNumber = r => ReducerInterface_ExpressionValue.EvNumber(r)
let evArray = r => ReducerInterface_ExpressionValue.EvArray(r)
let evRecord = r => ReducerInterface_ExpressionValue.EvRecord(r)
let evString = r => ReducerInterface_ExpressionValue.EvString(r)
let symbolicEvDistribution = r => r->DistributionTypes.Symbolic->evDistribution
}
@ -37,6 +38,12 @@ module Prepare = {
| FRValueArray(n) => Ok(n)
| _ => Error(impossibleError)
}
let arrayOfArrays = (inputs: t): result<array<ts>, err> =>
switch inputs {
| FRValueArray(n) => n->E.A2.fmap(openA)->E.A.R.firstErrorOrOpen
| _ => Error(impossibleError)
}
}
}
@ -110,9 +117,7 @@ module Prepare = {
}
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

@ -214,7 +214,7 @@ let registry = [
),
],
),
//TODO: Make sure that two functions cant have the same name. This causes chaos elsewhere.
//TODO: Make sure that two functions can't have the same name. This causes chaos elsewhere.
Function.make(
~name="Dict.mergeMany",
~definitions=[
@ -230,6 +230,76 @@ let registry = [
),
],
),
Function.make(
~name="Dict.keys",
~definitions=[
FnDefinition.make(~name="keys", ~inputs=[FRTypeDict(FRTypeAny)], ~run=(inputs, _) =>
switch inputs {
| [FRValueDict(d1)] => Js.Dict.keys(d1)->E.A2.fmap(Wrappers.evString)->Wrappers.evArray->Ok
| _ => Error(impossibleError)
}
),
],
),
Function.make(
~name="Dict.values",
~definitions=[
FnDefinition.make(~name="values", ~inputs=[FRTypeDict(FRTypeAny)], ~run=(inputs, _) =>
switch inputs {
| [FRValueDict(d1)] =>
Js.Dict.values(d1)
->E.A2.fmap(FunctionRegistry_Core.FRType.matchReverse)
->Wrappers.evArray
->Ok
| _ => Error(impossibleError)
}
),
],
),
Function.make(
~name="Dict.toList",
~definitions=[
FnDefinition.make(~name="toList", ~inputs=[FRTypeDict(FRTypeAny)], ~run=(inputs, _) =>
switch inputs {
| [FRValueDict(dict)] =>
dict
->Js.Dict.entries
->E.A2.fmap(((key, value)) =>
Wrappers.evArray([
Wrappers.evString(key),
FunctionRegistry_Core.FRType.matchReverse(value),
])
)
->Wrappers.evArray
->Ok
| _ => Error(impossibleError)
}
),
],
),
Function.make(
~name="Dict.fromList",
~definitions=[
FnDefinition.make(~name="fromList", ~inputs=[FRTypeArray(FRTypeArray(FRTypeAny))], ~run=(
inputs,
_,
) => {
let convertInternalItems = items =>
items
->E.A2.fmap(item => {
switch item {
| [FRValueString(string), value] =>
(string, FunctionRegistry_Core.FRType.matchReverse(value))->Ok
| _ => Error(impossibleError)
}
})
->E.A.R.firstErrorOrOpen
->E.R2.fmap(Js.Dict.fromArray)
->E.R2.fmap(Wrappers.evRecord)
inputs->E.A.unsafe_get(0)->Prepare.ToValueArray.Array.arrayOfArrays |> E.R2.bind(convertInternalItems)
}),
],
),
Function.make(
~name="List.make",
~definitions=[