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(
Prepare.ToTypedArray.numbers(inputs) ~name,
->E.R.bind(r => E.A.length(r) === 0 ? Error("List is empty") : Ok(r)) ~inputs=[FRTypeArray(FRTypeNumber)],
->E.R.bind(fn) ~run=(inputs, _) =>
Prepare.ToTypedArray.numbers(inputs)
->E.R.bind(r => E.A.length(r) === 0 ? Error("List is empty") : Ok(r))
->E.R.bind(fn),
(),
) )
} }
let make2 = (name, fn) => { let make2 = (name, fn) => {
FnDefinition.make(~name, ~inputs=[FRTypeArray(FRTypeAny)], ~run=(inputs, _) => FnDefinition.make(
Prepare.ToTypedArray.numbers(inputs) ~name,
->E.R.bind(r => E.A.length(r) === 0 ? Error("List is empty") : Ok(r)) ~inputs=[FRTypeArray(FRTypeAny)],
->E.R.bind(fn) ~run=(inputs, _) =>
Prepare.ToTypedArray.numbers(inputs)
->E.R.bind(r => E.A.length(r) === 0 ? Error("List is empty") : Ok(r))
->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(
inputs ~name,
->getOrError(0) ~inputs=[FRTypeNumber],
->E.R.bind(Prepare.oneNumber) ~run=(inputs, _) => {
->E.R2.fmap(fn) inputs
->E.R2.fmap(Wrappers.evNumber) ->getOrError(0)
}) ->E.R.bind(Prepare.oneNumber)
->E.R2.fmap(fn)
->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(
inputs->getOrError(0)->E.R.bind(Declaration.fromExpressionValue) ~name="declareFn",
}), ~inputs=[Declaration.frType],
~run=(inputs, _) => {
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(
inputs->E.A.unsafe_get(0)->Declaration.fromExpressionValue ~name="declareFn",
}), ~inputs=[Declaration.frType],
~run=(inputs, _) => {
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 ~inputs=[FRTypeArray(FRTypeDict(FRTypeAny))],
->Prepare.ToTypedArray.dicts ~run=(inputs, _) =>
->E.R2.fmap(E.Dict.concatMany) inputs
->E.R2.fmap(Js.Dict.map((. r) => FunctionRegistry_Core.FRType.matchReverse(r))) ->Prepare.ToTypedArray.dicts
->E.R2.fmap(r => r->Js.Dict.entries->Belt.Map.String.fromArray) ->E.R2.fmap(E.Dict.concatMany)
->E.R2.fmap(Wrappers.evRecord) ->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(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(
switch inputs { ~nameSpace=Some("Dict"),
| [FRValueDict(d1)] => Js.Dict.keys(d1)->E.A2.fmap(Wrappers.evString)->Wrappers.evArray->Ok ~requiresNamespace=true,
| _ => Error(impossibleError) ~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)
},
(),
), ),
], ],
(), (),
@ -417,15 +448,21 @@ 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(
switch inputs { ~nameSpace=Some("Dict"),
| [FRValueDict(d1)] => ~requiresNamespace=true,
Js.Dict.values(d1) ~name="values",
->E.A2.fmap(FunctionRegistry_Core.FRType.matchReverse) ~inputs=[FRTypeDict(FRTypeAny)],
->Wrappers.evArray ~run=(inputs, _) =>
->Ok switch inputs {
| _ => Error(impossibleError) | [FRValueDict(d1)] =>
} Js.Dict.values(d1)
->E.A2.fmap(FunctionRegistry_Core.FRType.matchReverse)
->Wrappers.evArray
->Ok
| _ => Error(impossibleError)
},
(),
), ),
], ],
(), (),
@ -433,21 +470,27 @@ 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(
switch inputs { ~nameSpace=Some("Dict"),
| [FRValueDict(dict)] => ~requiresNamespace=true,
dict ~name="toList",
->Js.Dict.entries ~inputs=[FRTypeDict(FRTypeAny)],
->E.A2.fmap(((key, value)) => ~run=(inputs, _) =>
Wrappers.evArray([ switch inputs {
Wrappers.evString(key), | [FRValueDict(dict)] =>
FunctionRegistry_Core.FRType.matchReverse(value), dict
]) ->Js.Dict.entries
) ->E.A2.fmap(((key, value)) =>
->Wrappers.evArray Wrappers.evArray([
->Ok Wrappers.evString(key),
| _ => Error(impossibleError) FunctionRegistry_Core.FRType.matchReverse(value),
} ])
)
->Wrappers.evArray
->Ok
| _ => Error(impossibleError)
},
(),
), ),
], ],
(), (),
@ -455,25 +498,29 @@ 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",
let convertInternalItems = items => ~inputs=[FRTypeArray(FRTypeArray(FRTypeAny))],
items ~run=(inputs, _) => {
->E.A2.fmap(item => { let convertInternalItems = items =>
switch item { items
| [FRValueString(string), value] => ->E.A2.fmap(item => {
(string, FunctionRegistry_Core.FRType.matchReverse(value))->Ok switch item {
| _ => Error(impossibleError) | [FRValueString(string), value] =>
} (string, FunctionRegistry_Core.FRType.matchReverse(value))->Ok
}) | _ => Error(impossibleError)
->E.A.R.firstErrorOrOpen }
->E.R2.fmap(Belt.Map.String.fromArray) })
->E.R2.fmap(Wrappers.evRecord) ->E.A.R.firstErrorOrOpen
inputs->getOrError(0)->E.R.bind(Prepare.ToValueArray.Array.arrayOfArrays) ->E.R2.fmap(Belt.Map.String.fromArray)
|> E.R2.bind(convertInternalItems) ->E.R2.fmap(Wrappers.evRecord)
}), inputs->getOrError(0)->E.R.bind(Prepare.ToValueArray.Array.arrayOfArrays)
|> E.R2.bind(convertInternalItems)
},
(),
),
], ],
(), (),
), ),
@ -481,30 +528,43 @@ 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(
switch inputs { ~nameSpace=Some("List"),
| [FRValueNumber(number), value] => ~requiresNamespace=true,
Belt.Array.make(E.Float.toInt(number), value) ~name="make",
->E.A2.fmap(FunctionRegistry_Core.FRType.matchReverse) ~inputs=[FRTypeNumber, FRTypeAny],
->Wrappers.evArray ~run=(inputs, _) => {
->Ok switch inputs {
| _ => Error(impossibleError) | [FRValueNumber(number), value] =>
} Belt.Array.make(E.Float.toInt(number), value)
}), ->E.A2.fmap(FunctionRegistry_Core.FRType.matchReverse)
->Wrappers.evArray
->Ok
| _ => Error(impossibleError)
}
},
(),
),
], ],
(), (),
), ),
Function.make( Function.make(
~name="upTo", ~name="upTo",
~definitions=[ ~definitions=[
FnDefinition.make(~name="upTo", ~inputs=[FRTypeNumber, FRTypeNumber], ~run=(inputs, _) => FnDefinition.make(
inputs ~nameSpace=Some("List"),
->Prepare.ToValueTuple.twoNumbers ~requiresNamespace=true,
->E.R2.fmap(((low, high)) => ~name="upTo",
E.A.Floats.range(low, high, (high -. low +. 1.0)->E.Float.toInt) ~inputs=[FRTypeNumber, FRTypeNumber],
->E.A2.fmap(Wrappers.evNumber) ~run=(inputs, _) =>
->Wrappers.evArray inputs
) ->Prepare.ToValueTuple.twoNumbers
->E.R2.fmap(((low, high)) =>
E.A.Floats.range(low, high, (high -. low +. 1.0)->E.Float.toInt)
->E.A2.fmap(Wrappers.evNumber)
->Wrappers.evArray
),
(),
), ),
], ],
(), (),