Impressum/Kontaktparent nodes: implemented functions

Scan

testAplScan
| result data table |
"http://www.microapl.co.uk/apl_help/ch_020_020_820.htm"

"data aplScan:dyadicFunction "

"Used with a dyadic function operand. The type of operation being carried out will be apparent from the context in which the symbol is used. Scan can be applied to any dyadic function, including user- defined functions."

"The function is any dyadic function (i.e. a smalltalk method with one argument). The effect is as if the function had been entered between all the elements of the data. In the absence of an axis specification, the function is applied to the last dimension. (This is similar to aplReduce). A given element of the result consists of the result of applying the function repeatedly over all the positions up to it. In each case the general rule for the order of execution is obeyed."

"Compare with 20 + 10 + -5 + 7. The result shows the running totals and the final sum"
self assert: #(20 30 25 32) asApl = (#(20 10 -5 7) asApl aplScan:#+).


result := #('AB' 'ABCD' 'ABCDEF') asApl.
data := #('AB' 'CD' 'EF') asApl.

"Repeated applications of ,"
self assert: result = (data aplScan:#,).


table := #(5 2 3 4 7 6) aplReshape:#(2 3).
result := #(5 10 30 4 28 168) aplReshape:#(2 3).

"Puts * between all elements of TABLE and shows the result of each multiplication in row 1 and in row 2. Note that since no dimension was specified, the operation takes place on the last dimension, the columns. See axis operator"

self assert: result = (table aplScan:#*).

"First axis scan. Applies across each row, i.e. Down the columns. Note: ScanOnFirstAxis is not implemented, we use the alternate form"
result := #(5 2 3 20 14 18) aplReshape:#(2 3).
self assert: result = (table aplScan:#* onAxis:1).

result:=#(1 1 1 0 0 0) asApl.
self assert: result = (#(1 1 1 0 1 1) asApl aplScan:#aplAnd:).

"Applies logical 'and' over all elements. A series of 1's is produced up to the first 0. Shows where a test first failed"

"result:= #(1 -1 2 -2) asApl. ATTENTION:DIFFERENT Result than apl code, see below!"
result:= #(1 -1 -4 -8) asApl.
"The intermediate results are (apl evaluates from right to left, smalltalk (and maths) from left to right"
"                 APL                       Smalltalk"
"1                 1                         1       "
"1 - 2            -1                        -1       "
"1 - 2 - 3        1 - (2 - 3) = 2           -4       "
"1 - 2 - 3 - 4    1 - (2 - (3 - 4))  = -2   -8       "
self assert: result = (#(1 2 3 4) asApl aplScan:#-).


"Useful examples of Scan include:"
"aplScan:#aplAnd:      All 0 after the first 0"
"aplScan:#aplOr:       All 1 after the first 1"
"aplScan:#<            1 at the first 1"
"aplScan:#<=           0 at the first 0"
"aplScan:#~=           0 or 1, reversing at each 1"