Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Function Calls

Functions compute a result from zero or more input arguments that are passed by value as positional arguments.

A function call is an expression having the form

<entity> ( [ <arg> [ , <arg> ... ]] )

where the <entity> is either an identifier or a lambda expression. When the <entity> is an identifier, it is one of

Each argument <arg> is either

Functions are not first-class values and cannot be assigned to super-structured values as there are no function values in the super-structured data model. Instead, functions may only be called or passed as a reference to another function.

Lambda Expressions

A lambda expression is an anonymous function having the form

lambda [ <param> [ , <param> ... ]] : <expr>

where <expr> is any expression defining the function and each <param> is an identifier defining the positional parameters of the function.

For example,

lambda x:x+1

is an anonymous function that adds one to its argument.

Like named functions, lambda expressions may only be called or passed to another function as an argument.

For example,

lambda x:x+1 (2)

is the value 3 and

f(lambda x:x+1, 2)

calls the function f with the lambda as its first argument and the value 2 as its second argument.

Function References

The syntax for referencing a function by name is

& <name>

where <name> is an identifier corresponding to either a built-in function or a declared function that is in scope.

Note

Many languages form function references simply by referring to their name without the need for a special symbol like&. However, an ambiguity arises here between a field reference, which is not declared, and a function name.

For example,

&upper

is a reference to the built-in function upper.

Examples


Sample calls to various built-in functions

# spq
values pow(2,3), lower("ABC")+upper("def"), typeof(1)
# input

# expected output
8.
"abcDEF"
<int64>

Calling a lambda function

# spq
values lambda x:x+1 (2)
# input

# expected output
3

Passing a lambda function

# spq
fn square(g,val):g(val)*g(val)
values square(lambda x:x+1, 2)
# input

# expected output
9

Passing function references

# spq
fn inc(x):x+1
fn apply(val,sfunc,nfunc):
  case typeof(val)
  when <string> then sfunc(val)
  when <int64> then nfunc(val)
  else val
  end
values apply(this,&upper,&inc)
# input
1
"foo"
true
# expected output
2
"FOO"
true

Function references may not be assigned to super-structured values

# spq
fn f():null
values {x:&f}
# input

# expected output
parse error at line 2, column 11:
values {x:&f}
      === ^ ===