Function - Procedure
Function.ProcedureA Procedure to build modular logic in Elara.
A procedure is a routine (or function) that can be called from within a
FunctionBuilder or ProcessBuilder. It is useful for factoring your code and repeating repetition.Methods on the Procedure enable you to directly transform data from any number of named input parameters
and return a single computed value. The body of the function works similarly to most imperative
languages (like JavaScript/TypeScript) allowing for definition of temporary variables (with let), reassignment
of defined variables (with assign), branching (with if, ifNull and match), loops (with while, forArray, forSet, forDict),
as well as logging and raising errors (with log, warn and error). Inside any of these statements you can use
East Expressions to access and manipulate data in scope.
Example
// create a procedure to multiply two numbers
const my_procedure = new Procedure("my_procedure")
.input("x", FloatType)
.input("y", FloatType)
.output(FloatType)
.body(b => b
.return(vars => Multiply(vars.x, vars.y))
);
// create the predict_amount function
const x = new SourceBuilder("x").value({ value: 3.14 });
const y = new SourceBuilder("y").value({ value: 42.0 });
const f = new FunctionBuilder("f")
.input("x", x.outputStream())
.input("y", y.outputStream())
.procedure(my_procedure)
.body(block => block
.let("output", (vars, procs) => procs.my_procedure(Struct({ x: vars.x, y: vars.y })))
.return({ output: vars => vars.output })
);
Type parameters
| Name | Type |
|---|---|
Name | extends string |
Inputs | extends Record = |
Procedure
constructor
• new Procedure(name):
Procedure
Create a Procedure containing modular logic in Elara.
A procedure is a routine (or function) that can be called from within a
FunctionBuilder or ProcessBuilder. It is useful for factoring your code and repeating repetition.Methods on the Procedure enable you to directly transform data from any number of named input parameters
and return a single computed value. The body of the function works similarly to most imperative
languages (like JavaScript/TypeScript) allowing for definition of temporary variables (with let), reassignment
of defined variables (with assign), branching (with if, ifNull and match), loops (with while, forArray, forSet, forDict),
as well as logging and raising errors (with log, warn and error). Inside any of these statements you can use
East Expressions to access and manipulate data in scope.
Type parameters
| Name | Type |
|---|---|
Name | extends string |
Inputs | extends Record = |
Parameters
| Name | Type |
|---|---|
name | Name |
Returns
Procedure
Example
// create a procedure to multiply two numbers
const my_procedure = new Procedure("my_procedure")
.input("x", FloatType)
.input("y", FloatType)
.output(FloatType)
.body(b => b
.return(vars => Multiply(vars.x, vars.y))
);
// create the predict_amount function
const x = new SourceBuilder("x").value({ value: 3.14 });
const y = new SourceBuilder("y").value({ value: 42.0 });
const f = new FunctionBuilder("f")
.input("x", x.outputStream())
.input("y", y.outputStream())
.procedure(my_procedure)
.body(block => block
.let("output", (vars, procs) => procs.my_procedure(Struct({ x: vars.x, y: vars.y })))
.return({ output: vars => vars.output })
);
input
▸ input(name, type):
Procedure<Name, Inputs & { [K in string]: T }>
Define an input parameter to a
Procedure.Type parameters
| Name | Type |
|---|---|
InputName | extends string |
T | extends EastType |
Parameters
| Name | Type | Description |
|---|---|---|
name | InputName | the name of the input parameter |
type | T | the EastType of the input parameter |
Returns
Procedure<Name, Inputs & { [K in string]: T }>
Example
// create a procedure to multiply two numbers
const my_procedure = new Procedure("my_procedure")
.input("x", FloatType)
.input("y", FloatType)
.output(FloatType)
.body(b => b
.return(vars => Multiply(vars.x, vars.y))
);
output
▸ output(type): ProcedureBodyFinalizer
Define the type of the output of a
Procedure.Type parameters
| Name | Type |
|---|---|
T | extends EastType |
Parameters
| Name | Type | Description |
|---|---|---|
type | T | the EastType of the output value |
Returns
ProcedureBodyFinalizer
Example
// create a procedure to multiply two numbers
const my_procedure = new Procedure("my_procedure")
.input("x", FloatType)
.input("y", FloatType)
.output(FloatType)
.body(b => b
.return(vars => Multiply(vars.x, vars.y))
);