A function is a parameterized sequence of statements. Functions are first-class values. Functions can be stored in variables, records, and arrays; functions can be passed as arguments to other functions; functions can be returned as the results of functions. Functions can not be sent in messages to other processes. Function objects are immutable values. A function introduces a new scope: variables declared in a function are not visible outside of the function, but will be visible to inner functions.
Function objects are made with the function operator which takes a parameter list and a body and produces a function object.
function_literal
"function" space optional_function_name parameter_list space body
optional_function_name
""
name
A function may be given a name for recursion and documentation.
Function objects can also be made with the def statement and record literals.
The parameter list can be open (each parameter name on its own line) or closed
(all parameter names on the same line, separated with a space).
The parameter names are bound to the arguments when the function is called. A parameter is a def that is initialized by the caller with an argument.
parameter_list
'(' parameter_list_filler ')'
parameter_list_filler
""
parameter more_closed_parameters
indent parameter more_open_parameters outdent
parameter name optional_default
optional_default
""
space '|' space expression
more_closed_parameters
""
space parameter more_closed_parameters
more_open_parameters
""
linebreak parameter more_open_parameters
When the function is invoked, the arguments are assigned to the parameter names and made available to the body. The body can not modify the parameters, but can modify the values of the parameters if they are not stone.
The number of names in the parameter list determines the function's arity.
If the function is invoked with too many arguments, the actor halts.
If the function is invoked with too few arguments, the remaining parameters are set to null.
The |default operator is allowed in parameter lists, replacing a null argument with a value from an expression.
The body is a string of statements wrapped in {}braces.
body
'{' indent statements outdent'}'
The return statement or the jump statement gives the return value. Implicit return (falling through the bottom) returns null.
The context of a function is the function in which it is made.
An outer function provides the context for an inner function. The inner
function has access to all of the outer function's variables, definitions,
and parameters. The context (or lexical closure)
remains available to the inner function even after the outer function
has finished its invocation and returned. If an inner function redeclares names that are already declared in the context (parameters, def, or var), it is not able to access the outer names.
Functions are invoked with the ()paren suffix operator.
function_value
(arguments)
The parens contain zero or more expressions whose values are passed to the function as arguments.
Each value is assigned to a named parameter.
Parameters that do not have values are initialized to null.
If there are too many arguments, then the actor halts.
call my_function(do re mi) # function invocation
intrinsic
module
outer function
inner function
A function creates a scope. A scope is a set of names belonging to the particular invocation of a function. The names are declared by
A name in a function can only be declared once, and it must be declared before it is used.
Names declared with the function form of the def statement are visible to all inner functions, even before the point of declaration. This makes mutual recursion possible.
If a name is declared in an if statement, it must be declared in every branch. That name is visible to the rest of the function.
In addition to the declared names, a function also has access to the names in the scopes of all of the functions it is nested in, including the misty program or module and the intrinsics. A function may use those outer names as if they were its own without further declaration. A function may continue to enjoy access to the scope of an outer function even after the outer function has returned.
A function is not required to use any of the outer names. A function is free to reuse any of those outer names by declaration, but in doing so, it loses its right to use the outer version.
An inner function can see the scope of an outer function, but an outer function may not see into the scope of an inner function. A function can be both an inner function (to the function it is nested inside) and an outer function (of the functions nested inside of itself).
function_operator
"'[]'"
''' infix_operator '''
The operators can be used as functions by wrapping the operators with 'single quote. These operator functions, or functinos, can be used like any other functions. They can be passed as arguments or stored in records and arrays.
So '+' is the binary add function that can be passed to the reduce function to make sums.
reduce([1 2 3 4 5] '+') # 15 '/\'(3 4) # null and '\/'(3 4) # null or '|'(3 4) # 3 default '='(3 4) # false equal '<>'(3 4) # true not equal '<'(3 4) # true less '<='(3 4) # true less or equal '>'(3 4) # false greater '>='(3 4) # false greater or equal '&'(3 4) # "34" concat '&&'(3 4) # "3 4" concat with space '+'(3 4) # 7 add '-'(3 4) # -1 subtract '*'(3 4) # 12 multiply '/'(3 4) # 0.75 divide '//'(3 4) # 0 integer divide '[]'(3 4) # null get
Three are short-circuiting in their operator form, but not in their functino form:
'/\'and '\/'or '|'default.
The equal function can take two or three arguments. The first two arguments are values to be compared.
If all of the parameters are numbers, an optional tolerance argument may be included which must be a non-negative number.
The result is true if the absolute value of the difference between the
two numbers is less than or equal to the tolerance.
If the arguments are texts, and if the tolerance is true, then the comparison is case-insensitive.
def first: 12.3775 def second: 12.38 assign exactly: '='(first second) # exactly is false assign sloppy: '='(first second 0.01) # sloppy is true def first: "vorpal" def second: "VORPAL" assign exactly: '='(first second) # exactly is false assign sloppy: '='(first second true) # sloppy is true
The not equal function can take two or three arguments. The first two arguments are values to be compared. The optional third argument is a tolerance. When comparing numbers, the tolerance is a number that indicates the allowable difference. When comparing texts, the tolerance is a logical that if true ignores case.
The get function takes two arguments. The first argument is an array, record, or text. The second argument is an element number or key. It returns an element, a property, or a character, or null.
There are several function types that are essential to Misty. They are
A callback function is a function that is used to deliver functional results from the future. A callback function has this signature:
function (value reason)
The value is the value of the operation if it was successful. The value is null if it is unsuccessful.
If the value is null, then the optional reason may include an error message or indication of the cause of the lack of success.
Requestor functions take a callback.
The send statement can take a callback to receive replies.
A factory function is any function that returns a function. A requestor factory is a function that returns a requestor function. These standard functions are requestor factories:
A requestor function encapsulates a unit of work, communicating the result thru a callback, allowing work to progress over many turns. A requestor function has this signature:
function requestor(callback value)
When the requestor function is finished, it calls the callback function with the result.
The optional value is some value that is used to determine the result.
A common usage is to send a message to some actor. When the reply eventually arrives, extract a result from the reply and pass it to the callback.
A requestor function can optionally return a cancel function.
A requestor function may optionally return a cancel function. When the cancel function is called, it attempts to stop the work of the requestor. A cancel function tries to send a message to some actor informing it that the result is no longer needed. The purpose of cancel is to stop work that is no longer required. It is advisory. It is not an undo. It is not guaranteed, particularly in the case where cancel is called after some actor has completed its work.
Cancel is most effective with the requestor factories that are organizing the work.
A cancel function has this signature:
function cancel(reason abandon | true)
The reason is optional, but if included might be logged or propagated. If abandon is true, then simply stop the requested work. If abandon is false, then if possible, complete the assignment, perhaps by calling the callback with the reason.
Programs and modules are special cases of functions. A program is a function that is called when an actor is started. A module is an independent function that is called with the module construct.