Frequently Used Symbols

This chapter introduces some commonly used symbols in DolphinDB.

1. Equal operator = : assignment or equal operator in SQL

$ x=1;
$ select * from t1 where id=100;

2. Square bracket [] : define a vector or indices

$ x=[1,2,3,4];

$ x[0 2];
[1,3]

3. Parenthesis () : function definition or function call, constant tuple, or expression grouping

def f(x):x+10;
$ f(2);
12

$ x=(1, 2, 3, 4);

$ x=1:(6+1);

4. Curly bracket {} : statement block or partial application

$ timer {x = rand(100.0, 10000000); y=(x pow 2)+2*x;};
Time elapsed: 1351.24 ms

$ a=100;
$ g=add{a*a};
$ g(8);
10008

5. Comma “,” : vector element separator, function argument separator, or multiple dimension indices separator

$ x=[1,2,3,4];
def f(x,y):x+y;

f(1,2);
3

6. Colon “:” : pair operator, lambda expression, or higher order function ( accumulate (:A), groupby (:G) etc)

$ x=1..6$2:3;
$ x;

col1

col2

col3

1

3

5

2

4

6

$ x[0:1,0:2];

col1

col2

1

3

def f(x):x*x+3*x+4.0;
f(2);
14
$ x=1..10;
$ +:A x;
[1,3,6,10,15,21,28,36,45,55]

7. Backtick “`” : also called back quote. It is used before letters, numbers or _ to indicate a string. It is equivalent to double quotes around one word, but is more convenient to use.

$ x=`AAPL`IBM`KO;
$ x;
["AAPL","IBM","KO"]

$ select * from table1 where id=`1001;

8. Dot “.” : object-oriented function call or dictionary lookup.

$ x=1 2.5 6;
$ x.sum();
9.5

$ x=3 6 1 5 9;
$ y=4.2 3.7 8.8 6.4 5.3;
$ z=dict(x, y);
$ z.3;
4.2

9. Function definition: def

def f(a){return a+1};

f(2);
3

10. Column alias: as

$ t=table(1 2 3 as a, 4 5 6 as b);
t;

a

b

1

4

2

5

3

6

11. Conditional statement: if … else …

$ def temp(const a) {
$       if (a>10)
$          return a\10
$       else if (a<=10 && a>1)
$          {return a}
$       else {
$       b=abs(a)*10
$          return b
$       }
$ };

$ temp 10;
10
$ temp 11;
1.1
$ temp 0.5
5

12. “;” with ENTER: parse and execute the script in console.

13. Comment: // or /* */

// This is a comment.

/* this is for calculating stock returns
for a trading strategy development. */