First attempt at integrating namespaces

This commit is contained in:
Ozzie Gooen 2022-07-07 12:01:02 -07:00
parent 012112336f
commit 9f0e4f34fe
3 changed files with 190 additions and 105 deletions

View File

@ -39,6 +39,8 @@ and frValueDictParam = (string, frValue)
and frValueDistOrNumber = FRValueNumber(float) | FRValueDist(DistributionTypes.genericDist) and frValueDistOrNumber = FRValueNumber(float) | FRValueDist(DistributionTypes.genericDist)
type fnDefinition = { type fnDefinition = {
nameSpace: option<string>,
requiresNamespace: bool,
name: string, name: string,
inputs: array<frType>, inputs: array<frType>,
run: (array<frValue>, DistributionOperation.env) => result<internalExpressionValue, string>, run: (array<frValue>, DistributionOperation.env) => result<internalExpressionValue, string>,
@ -327,8 +329,10 @@ module FnDefinition = {
} }
} }
let make = (~name, ~inputs, ~run): t => { let make = (~nameSpace=None, ~requiresNamespace=true, ~name, ~inputs, ~run, ()): t => {
name: name, name: name,
nameSpace: nameSpace,
requiresNamespace: requiresNamespace,
inputs: inputs, inputs: inputs,
run: run, run: run,
} }

View File

@ -201,8 +201,11 @@ module TwoArgDist = {
->E.R2.fmap(Wrappers.evDistribution) ->E.R2.fmap(Wrappers.evDistribution)
let make = (name, fn) => { let make = (name, fn) => {
FnDefinition.make(~name, ~inputs=[FRTypeDistOrNumber, FRTypeDistOrNumber], ~run=(inputs, env) => FnDefinition.make(
inputs->Prepare.ToValueTuple.twoDistOrNumber->process(~fn, ~env) ~name,
~inputs=[FRTypeDistOrNumber, FRTypeDistOrNumber],
~run=(inputs, env) => inputs->Prepare.ToValueTuple.twoDistOrNumber->process(~fn, ~env),
(),
) )
} }
@ -211,6 +214,7 @@ module TwoArgDist = {
~name, ~name,
~inputs=[FRTypeRecord([("p5", FRTypeDistOrNumber), ("p95", FRTypeDistOrNumber)])], ~inputs=[FRTypeRecord([("p5", FRTypeDistOrNumber), ("p95", FRTypeDistOrNumber)])],
~run=(inputs, env) => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env), ~run=(inputs, env) => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env),
(),
) )
} }
@ -219,6 +223,7 @@ module TwoArgDist = {
~name, ~name,
~inputs=[FRTypeRecord([("mean", FRTypeDistOrNumber), ("stdev", FRTypeDistOrNumber)])], ~inputs=[FRTypeRecord([("mean", FRTypeDistOrNumber), ("stdev", FRTypeDistOrNumber)])],
~run=(inputs, env) => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env), ~run=(inputs, env) => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env),
(),
) )
} }
} }
@ -230,35 +235,51 @@ module OneArgDist = {
->E.R2.fmap(Wrappers.evDistribution) ->E.R2.fmap(Wrappers.evDistribution)
let make = (name, fn) => let make = (name, fn) =>
FnDefinition.make(~name, ~inputs=[FRTypeDistOrNumber], ~run=(inputs, env) => FnDefinition.make(
inputs->Prepare.ToValueTuple.oneDistOrNumber->process(~fn, ~env) ~name,
~inputs=[FRTypeDistOrNumber],
~run=(inputs, env) => inputs->Prepare.ToValueTuple.oneDistOrNumber->process(~fn, ~env),
(),
) )
} }
module ArrayNumberDist = { module ArrayNumberDist = {
let make = (name, fn) => { let make = (name, fn) => {
FnDefinition.make(~name, ~inputs=[FRTypeArray(FRTypeNumber)], ~run=(inputs, _) => FnDefinition.make(
~name,
~inputs=[FRTypeArray(FRTypeNumber)],
~run=(inputs, _) =>
Prepare.ToTypedArray.numbers(inputs) Prepare.ToTypedArray.numbers(inputs)
->E.R.bind(r => E.A.length(r) === 0 ? Error("List is empty") : Ok(r)) ->E.R.bind(r => E.A.length(r) === 0 ? Error("List is empty") : Ok(r))
->E.R.bind(fn) ->E.R.bind(fn),
(),
) )
} }
let make2 = (name, fn) => { let make2 = (name, fn) => {
FnDefinition.make(~name, ~inputs=[FRTypeArray(FRTypeAny)], ~run=(inputs, _) => FnDefinition.make(
~name,
~inputs=[FRTypeArray(FRTypeAny)],
~run=(inputs, _) =>
Prepare.ToTypedArray.numbers(inputs) Prepare.ToTypedArray.numbers(inputs)
->E.R.bind(r => E.A.length(r) === 0 ? Error("List is empty") : Ok(r)) ->E.R.bind(r => E.A.length(r) === 0 ? Error("List is empty") : Ok(r))
->E.R.bind(fn) ->E.R.bind(fn),
(),
) )
} }
} }
module NumberToNumber = { module NumberToNumber = {
let make = (name, fn) => let make = (name, fn) =>
FnDefinition.make(~name, ~inputs=[FRTypeNumber], ~run=(inputs, _) => { FnDefinition.make(
~name,
~inputs=[FRTypeNumber],
~run=(inputs, _) => {
inputs inputs
->getOrError(0) ->getOrError(0)
->E.R.bind(Prepare.oneNumber) ->E.R.bind(Prepare.oneNumber)
->E.R2.fmap(fn) ->E.R2.fmap(fn)
->E.R2.fmap(Wrappers.evNumber) ->E.R2.fmap(Wrappers.evNumber)
}) },
(),
)
} }

View File

@ -54,9 +54,12 @@ let registry = [
~name="toContinuousPointSet", ~name="toContinuousPointSet",
~definitions=[ ~definitions=[
FnDefinition.make( FnDefinition.make(
~name="toContinuousPointSet", ~nameSpace=Some("PointSet"),
~requiresNamespace=true,
~name="makeContinuous",
~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))], ~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))],
~run=(inputs, _) => inputsTodist(inputs, r => Continuous(Continuous.make(r))), ~run=(inputs, _) => inputsTodist(inputs, r => Continuous(Continuous.make(r))),
(),
), ),
], ],
(), (),
@ -65,9 +68,12 @@ let registry = [
~name="toDiscretePointSet", ~name="toDiscretePointSet",
~definitions=[ ~definitions=[
FnDefinition.make( FnDefinition.make(
~name="toDiscretePointSet", ~nameSpace=Some("PointSet"),
~requiresNamespace=true,
~name="makeDiscrete",
~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))], ~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))],
~run=(inputs, _) => inputsTodist(inputs, r => Discrete(Discrete.make(r))), ~run=(inputs, _) => inputsTodist(inputs, r => Continuous(Continuous.make(r))),
(),
), ),
], ],
(), (),
@ -75,9 +81,14 @@ let registry = [
Function.make( Function.make(
~name="Declaration", ~name="Declaration",
~definitions=[ ~definitions=[
FnDefinition.make(~name="declareFn", ~inputs=[Declaration.frType], ~run=(inputs, _) => { FnDefinition.make(
~name="declareFn",
~inputs=[Declaration.frType],
~run=(inputs, _) => {
inputs->getOrError(0)->E.R.bind(Declaration.fromExpressionValue) inputs->getOrError(0)->E.R.bind(Declaration.fromExpressionValue)
}), },
(),
),
], ],
(), (),
), ),
@ -189,6 +200,7 @@ to(5,10)
~name="toContinuousPointSet", ~name="toContinuousPointSet",
~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))], ~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))],
~run=(inputs, _) => inputsTodist(inputs, r => Continuous(Continuous.make(r))), ~run=(inputs, _) => inputsTodist(inputs, r => Continuous(Continuous.make(r))),
(),
), ),
], ],
(), (),
@ -207,6 +219,7 @@ to(5,10)
~name="toDiscretePointSet", ~name="toDiscretePointSet",
~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))], ~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))],
~run=(inputs, _) => inputsTodist(inputs, r => Discrete(Discrete.make(r))), ~run=(inputs, _) => inputsTodist(inputs, r => Discrete(Discrete.make(r))),
(),
), ),
], ],
(), (),
@ -222,9 +235,14 @@ to(5,10)
] ]
})`, })`,
~definitions=[ ~definitions=[
FnDefinition.make(~name="declareFn", ~inputs=[Declaration.frType], ~run=(inputs, _) => { FnDefinition.make(
~name="declareFn",
~inputs=[Declaration.frType],
~run=(inputs, _) => {
inputs->E.A.unsafe_get(0)->Declaration.fromExpressionValue inputs->E.A.unsafe_get(0)->Declaration.fromExpressionValue
}), },
(),
),
], ],
~isExperimental=true, ~isExperimental=true,
(), (),
@ -366,6 +384,8 @@ to(5,10)
~name="Dict.merge", ~name="Dict.merge",
~definitions=[ ~definitions=[
FnDefinition.make( FnDefinition.make(
~nameSpace=Some("Dict"),
~requiresNamespace=true,
~name="merge", ~name="merge",
~inputs=[FRTypeDict(FRTypeAny), FRTypeDict(FRTypeAny)], ~inputs=[FRTypeDict(FRTypeAny), FRTypeDict(FRTypeAny)],
~run=(inputs, _) => { ~run=(inputs, _) => {
@ -380,6 +400,7 @@ to(5,10)
| _ => Error(impossibleError) | _ => Error(impossibleError)
} }
}, },
(),
), ),
], ],
(), (),
@ -388,16 +409,19 @@ to(5,10)
Function.make( Function.make(
~name="Dict.mergeMany", ~name="Dict.mergeMany",
~definitions=[ ~definitions=[
FnDefinition.make(~name="mergeMany", ~inputs=[FRTypeArray(FRTypeDict(FRTypeAny))], ~run=( FnDefinition.make(
inputs, ~nameSpace=Some("Dict"),
_, ~requiresNamespace=true,
) => ~name="mergeMany",
~inputs=[FRTypeArray(FRTypeDict(FRTypeAny))],
~run=(inputs, _) =>
inputs inputs
->Prepare.ToTypedArray.dicts ->Prepare.ToTypedArray.dicts
->E.R2.fmap(E.Dict.concatMany) ->E.R2.fmap(E.Dict.concatMany)
->E.R2.fmap(Js.Dict.map((. r) => FunctionRegistry_Core.FRType.matchReverse(r))) ->E.R2.fmap(Js.Dict.map((. r) => FunctionRegistry_Core.FRType.matchReverse(r)))
->E.R2.fmap(r => r->Js.Dict.entries->Belt.Map.String.fromArray) ->E.R2.fmap(r => r->Js.Dict.entries->Belt.Map.String.fromArray)
->E.R2.fmap(Wrappers.evRecord) ->E.R2.fmap(Wrappers.evRecord),
(),
), ),
], ],
(), (),
@ -405,11 +429,18 @@ to(5,10)
Function.make( Function.make(
~name="Dict.keys", ~name="Dict.keys",
~definitions=[ ~definitions=[
FnDefinition.make(~name="keys", ~inputs=[FRTypeDict(FRTypeAny)], ~run=(inputs, _) => FnDefinition.make(
~nameSpace=Some("Dict"),
~requiresNamespace=true,
~name="keys",
~inputs=[FRTypeDict(FRTypeAny)],
~run=(inputs, _) =>
switch inputs { switch inputs {
| [FRValueDict(d1)] => Js.Dict.keys(d1)->E.A2.fmap(Wrappers.evString)->Wrappers.evArray->Ok | [FRValueDict(d1)] =>
Js.Dict.keys(d1)->E.A2.fmap(Wrappers.evString)->Wrappers.evArray->Ok
| _ => Error(impossibleError) | _ => Error(impossibleError)
} },
(),
), ),
], ],
(), (),
@ -417,7 +448,12 @@ to(5,10)
Function.make( Function.make(
~name="Dict.values", ~name="Dict.values",
~definitions=[ ~definitions=[
FnDefinition.make(~name="values", ~inputs=[FRTypeDict(FRTypeAny)], ~run=(inputs, _) => FnDefinition.make(
~nameSpace=Some("Dict"),
~requiresNamespace=true,
~name="values",
~inputs=[FRTypeDict(FRTypeAny)],
~run=(inputs, _) =>
switch inputs { switch inputs {
| [FRValueDict(d1)] => | [FRValueDict(d1)] =>
Js.Dict.values(d1) Js.Dict.values(d1)
@ -425,7 +461,8 @@ to(5,10)
->Wrappers.evArray ->Wrappers.evArray
->Ok ->Ok
| _ => Error(impossibleError) | _ => Error(impossibleError)
} },
(),
), ),
], ],
(), (),
@ -433,7 +470,12 @@ to(5,10)
Function.make( Function.make(
~name="Dict.toList", ~name="Dict.toList",
~definitions=[ ~definitions=[
FnDefinition.make(~name="dictToList", ~inputs=[FRTypeDict(FRTypeAny)], ~run=(inputs, _) => FnDefinition.make(
~nameSpace=Some("Dict"),
~requiresNamespace=true,
~name="toList",
~inputs=[FRTypeDict(FRTypeAny)],
~run=(inputs, _) =>
switch inputs { switch inputs {
| [FRValueDict(dict)] => | [FRValueDict(dict)] =>
dict dict
@ -447,7 +489,8 @@ to(5,10)
->Wrappers.evArray ->Wrappers.evArray
->Ok ->Ok
| _ => Error(impossibleError) | _ => Error(impossibleError)
} },
(),
), ),
], ],
(), (),
@ -455,10 +498,12 @@ to(5,10)
Function.make( Function.make(
~name="Dict.fromList", ~name="Dict.fromList",
~definitions=[ ~definitions=[
FnDefinition.make(~name="dictFromList", ~inputs=[FRTypeArray(FRTypeArray(FRTypeAny))], ~run=( FnDefinition.make(
inputs, ~nameSpace=Some("Dict"),
_, ~requiresNamespace=true,
) => { ~name="fromList",
~inputs=[FRTypeArray(FRTypeArray(FRTypeAny))],
~run=(inputs, _) => {
let convertInternalItems = items => let convertInternalItems = items =>
items items
->E.A2.fmap(item => { ->E.A2.fmap(item => {
@ -473,7 +518,9 @@ to(5,10)
->E.R2.fmap(Wrappers.evRecord) ->E.R2.fmap(Wrappers.evRecord)
inputs->getOrError(0)->E.R.bind(Prepare.ToValueArray.Array.arrayOfArrays) inputs->getOrError(0)->E.R.bind(Prepare.ToValueArray.Array.arrayOfArrays)
|> E.R2.bind(convertInternalItems) |> E.R2.bind(convertInternalItems)
}), },
(),
),
], ],
(), (),
), ),
@ -481,7 +528,12 @@ to(5,10)
~name="List.make", ~name="List.make",
~definitions=[ ~definitions=[
//Todo: If the second item is a function with no args, it could be nice to run this function and return the result. //Todo: If the second item is a function with no args, it could be nice to run this function and return the result.
FnDefinition.make(~name="listMake", ~inputs=[FRTypeNumber, FRTypeAny], ~run=(inputs, _) => { FnDefinition.make(
~nameSpace=Some("List"),
~requiresNamespace=true,
~name="make",
~inputs=[FRTypeNumber, FRTypeAny],
~run=(inputs, _) => {
switch inputs { switch inputs {
| [FRValueNumber(number), value] => | [FRValueNumber(number), value] =>
Belt.Array.make(E.Float.toInt(number), value) Belt.Array.make(E.Float.toInt(number), value)
@ -490,21 +542,29 @@ to(5,10)
->Ok ->Ok
| _ => Error(impossibleError) | _ => Error(impossibleError)
} }
}), },
(),
),
], ],
(), (),
), ),
Function.make( Function.make(
~name="upTo", ~name="upTo",
~definitions=[ ~definitions=[
FnDefinition.make(~name="upTo", ~inputs=[FRTypeNumber, FRTypeNumber], ~run=(inputs, _) => FnDefinition.make(
~nameSpace=Some("List"),
~requiresNamespace=true,
~name="upTo",
~inputs=[FRTypeNumber, FRTypeNumber],
~run=(inputs, _) =>
inputs inputs
->Prepare.ToValueTuple.twoNumbers ->Prepare.ToValueTuple.twoNumbers
->E.R2.fmap(((low, high)) => ->E.R2.fmap(((low, high)) =>
E.A.Floats.range(low, high, (high -. low +. 1.0)->E.Float.toInt) E.A.Floats.range(low, high, (high -. low +. 1.0)->E.Float.toInt)
->E.A2.fmap(Wrappers.evNumber) ->E.A2.fmap(Wrappers.evNumber)
->Wrappers.evArray ->Wrappers.evArray
) ),
(),
), ),
], ],
(), (),