# The writing rules for expression x in loop functions

Description:

Introduce the common rules on expressions in loop functions.

Note:

Expression x in loop functions such as r.(x), r.run(x), A.(x), A.run(x) and so on, should follow the rules below:

Ø  Members: ~ and A.~. ~ means the record being processed currently.

 A 1 =demo.query("select * from EMPLOYEE") 2 =A1.(age(~.HIREDATE)) [9,6,3,7,9,8,…] 3 =A1.new(~.NAME:Name,age(~.HIREDATE):Workage) Ø  Sequence numbers: # and A.#. # indicates the sequence number of the members.

 A [1,2,3,4,5,6,7,8…], the two results are the same 1 =demo.query("select * from EMPLOYEE") 2  =A1.(#) 3 =A1.(A1.#)

Ø  Fields: F, ~.F, r.F and A. F are equal to A.~. F , and A.F is equal to A(1).F if there is not a loop.

 A 1 =demo.query("select * from EMPLOYEE") 2 =A1.(age(HIREDATE)) [9,6,3,7,9,8,…], the results of the three expressions are the same. 3  =A1.(age(~.HIREDATE)) 4 =A1.(age(A1.HIREDATE)) 5 =A1.HIREDATE 2005-03-11, it is equal to A1(1).HIREDATE if there is not a loop.

Ø  A[i] and ~[i] are equal to A(#+i) in the loop, which counts the members forward from the current member in A and return the ith member. If the boundary is exceeded, errors are not reported and null is returned. F[] is equal to A[].F

 A 1 =demo.query("select ORDERID,AMOUNT,'' as ACCUMULATION from SALES") 2 =A1.run(ACCUMULATION=AMOUNT+ACCUMULATION[-1]) Obtain the current ACCUMULATION by adding the ACCUMULATION of the last record to the current AMOUNT.

Ø  A[a,b] and ~[a,b] in the loop indicate a sequence composed of members between A(#+a) and A(#+b). a is 1-# by default and b is A.len()-# by default.

 A 1 =demo.query("select STOCKID,DATE,CLOSING,'' as FirstThree from STOCKRECORDS") 2 =A1.run(FIRSTTHREE=~[--3,-1].(CLOSING).avg()) ~[-3,-1] indicates a sequence composed of three records counted backward from the current row.

Ø  Iteration: A1.f1(A2.f2(x))

Symbols in expression x will be first identified to serve A2; if expression x serves A1, the iteration will be . A1.f1(A2.f2(A1.x)).

 A 1 =demo.query("select * from EMPLOYEE") 2 =demo.query("select * from DEPARTMENT") 3 =A2.run(MANAGER=A1.select@1(EID==A2.~.MANAGER).NAME) Reference the nested loop

Ø  Aggregate: A.f(x)

A.f(x) is equal to A.(x).f(), in which f is an aggregate function and x can be null.

Note:

The x in A.count(x) is a boolean expression, which is different from the rule for other aggregate functions.