Impressum/Kontaktparent nodes: implemented functions

Reduce

testReduce
| sales data table |
"http://www.microapl.co.uk/apl_help/ch_020_020_800.htm"

"data aplReduce:function"

"aplReduce: can be applied to any dyadic function, including user defined functions. The N-wise reduction is aplReduce:subsetOf:"

"The operand of aplReduce: is inserted between all elements of the array right argument. In the absence of an axis specification, the operand is inserted between items along the last axis"

"This is the same as 2+4+6"
self assert: 12 asApl = (#(2 4 6) asApl aplReduce:#+).

"The sum of the numbers in sales"
sales := #(25 5.7 8 50 101 74 19) asApl.
self assert: 282.7 asApl = (sales aplReduce:#+).

"The same as (82 aplGreaterOf:(66 aplGreaterOf:(93 aplGreaterOf:13))). The result of 93 aplGreaterOf:13 is compared with 66; the result of this comparison is compared with 82; the result of the last comparison is the largest"
self assert: 93asApl = (#(82 66 93 13) asApl aplReduce:#aplGreaterOf:).

"The same as 0 ? 1 ? 1 ? 0 ? 0. Used to test if there are any 1s"
self assert: true asApl = (#(0 1 1 0 0) asApl aplReduce:#aplOr:).

"Are all elements 1:"
self assert: false asApl = (#(0 1 1 0 0 ) asApl aplReduce:#aplAnd:).

data := (Array with:'ABC' with:'DEF' with:'HIJ') asApl.
self assert: 'ABCDEFHIJ' asApl aplEnclose = (data aplReduce:#,).

"Result is a scalar"
self assert: (0 aplReshape:#()) aplShape = (data aplReduce:#,) aplShape.


"Multiply is applied to the elements of a matrix. Since no dimension is specified, it works on the last dimension, the columns. 6 is the result of multiplying the columns in row 1. 120 is the product of those in row 2"
table :=#(1 2 3 4 5 6) aplReshape:#(2 3).
self assert:#(6 120) asApl = (table aplReduce:#*).


"The onAxis:1 specifies that the operation is to apply across the 1st dimension, the rows. Each element in row 1 is multiplied by the corresponding element in row 2."

self assert:#(4 10 18) asApl = (table aplReduce:#* onAxis:1).