Function

Built-in Functions of DolphinDB

In Python Parser sessions, most of DolphinDB's built-in functions can be called directly without importing modules. However, functions sharing the same name in DolphinDB and Python are parsed as Python's built-in functions, such as dict, set, and type.

Python Parser encapsulates these functions and DolphinDB's constant objects in the dolphindb package. It is necessary to import the dolphindb module before calling these functions and objects.

import dolphindb as ddb
x = [1,2,3,4].toddb()
type(x)

Output: dolphindb.VECTOR.INT

ddb.type(x)

Output: 4

Some of DolphinDB's built-in functions support operators as the input parameters of functions, such as expr and nullCompare. In Python Parser, however, operators or keywords, such as "not" and "and", cannot be directly passed into functions as arguments. Otherwise, an exception occurs due to parsing conflicts. To solve this issue, replace the operator with the corresponding built-in function of DolphinDB as follows:

DolphinDB:

expr(6, <, 8)

Python Parser:

expr(6, lt, 8)

User-Defined Functions

Similar to Python, Python Parser defines a function using a colon as the start of the function block, in which all lines are indented.

Note: User-defined functions cannot share the same name with DolphinDB's built-in functions.

def <functionName> ([parameters]): 
    statements

The following example defines a function that returns a Fibonacci sequence of numbers less than n.

def fib(n):
    xs = []
    a, b = 0, 1
    while a < n:
        xs.append(a)
        a, b = b, a + b
	return xs
fib(2000)

Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]

Python Parser's user-defined functions support assigning a default constant value to a parameter.

def func1(a, b=1): 
    return a+b
func1(4)

Output: 5

When calling a function, Python Parser passes parameters to a function based on their order in the parameter list or their names and corresponding values.

def func2(a, b=1, c=2): 
    return a+b-c
func2(4, c=4)

Output: 1

Note that the current version of Python Parser does not support:

  • Positional arguments (/);

  • Asterisk arguments (* and **);

  • Defining a class inside a function;

  • Using the import statement inside a function;

  • Modifying the value of a global variable inside a function.

Aggregate Functions

The defg function is used to define aggregate functions.

defg <functionName> ([parameters]): 
    statements

Lambda Functions

DolphinDB's native syntax supports the following four Lambda expressions:

def <functionName>(parameters): expression

def (parameters): expression

def (parameters) -> expression

parameter -> expression

Since Python's native syntax cannot define anonymous functions with the def function or identify the symbol "->", Python Parser does not support the above four Lambda expressions.

Python Parser's Lambda function syntax is the same as Python's:

lambda [arg1 [,arg2,.....argn]]: expression

Higher-Order Functions

In Python Parser, DolphinDB's built-in higher-order functions can be directly called, but cannot be called by symbols.

Nested Functions

def test_func1(x):
    w = [0.1, 0.3, 0.5, 0.1].toddb()
    def inner():
        s = wsum(x, w)
        return s
    re = inner() / 100 
    return re

test_func1([1,2,3,4].toddb())

Output: 0.026

def test_func2():
    w = [0.1, 0.3, 0.5, 0.1].toddb()
    def inner(x):
        s = wsum(x, w)
        return s
    x = [1,2,3,4].toddb()
    re = inner(x) / 100 
    return re

test_func2()

Output: 0.026

Partial Application (Partial Function)

To be supported in future versions.

Function Decorator

To be supported in future versions.

Function Calling

Functions can be called in the following two ways:

  • Uniform function calling syntax: <func>(parameters)

  • Method calling syntax: x.<func>(parameters), where x is the first parameter.