Impressum/Kontaktparent nodes: implemented functions

↑ Take

testAplTake
| list table result expected mat |
"http://www.microapl.co.uk/apl_help/ch_020_020_560.htm"

"data aplTake:selection"

"The selection argument of aplTake: specifies how many elements are to be selected from the data argument in each of its dimensions. If the selection argument is positive, the elements are selected from the start of the appropriate dimension, if negative, from the end. The result is the data selected."

self assert: 'A.S.F' asApl = ('A.S.FREEMAN' asApl aplTake: 5).
self assert: 'FREEMAN' asApl = ('A.S.FREEMAN' asApl aplTake: -7).
self assert: #(22 2 19) asApl = (#(22 2 19 12) asApl aplTake:3).
self assert: #(12) asApl = (#(22 2 19 12) asApl aplTake: -1).

list := {4 aplInterval aplReshape:#(2 2). 10 aplInterval.} asApl.
"Note that first removes depth"
self assert: list aplFirst aplShape = #(2 2) asApl.

"Take does not affect the depth"
self assert: (list aplTake: 1) aplShape = #(1) asApl.

"If the selection specifies more elements than the data argument contains, all elements are selected and the prototype of the array self assert: #(40 92 11 0 0) asApl = (#(40 92 11) asApl aplTake:5).
self assert: #(0 0 40 92 11) asApl = (#(40 92 11) asApl aplTake: -5).

"If the data argument is a matrix, the first number in the selection argument specifies the number of rows to be selected, and the second, the number of columns:"

table := 12 aplInterval aplReshape:#(4 3).

"Selects all three columns of the first two rows"
result := table aplTake:#(2 3).
expected := #(1 2 3 4 5 6) aplReshape:#(2 3).
self assert: result = expected.

"Selects all three columns of the last row"
result := table aplTake:#(-1 3).
expected := #(10 11 12) aplReshape:#(1 3).
self assert: result = expected.

"Selects row 1, columns 1 and 2"
result := table aplTake:#(1 2).
expected := #(1 2) aplReshape:#(1 2).
self assert: result = expected.

"The overtake operation on matrices or higher dimensional arrays uses the prototype of the first element of each row already in existence to extend rows. New rows use the array prototype."

mat := #(1 $A $B 2) aplReshape:#(2 2).
self assert: #(2 2) asApl = mat aplShape.

"Extension of row 1 uses row 1 prototype"
"Row 2 prototype is a blank character"
"Row 3 is new and uses the array prototype"
result := mat aplTake:#(3 3).
expected := #(1 $A 0 $B 2 $ 0 0 0) aplReshape:#(3 3).
self assert: result = expected.

"Similar considerations apply to higher dimension arrays."