Help / Phrases / 3. B. Merge & Amend
>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Phrases
3B. Merge & Amend
Two arguments x and y can be merged by interleaving their items (necessarily of a common shape) as determined by a Boolean list of shape x +&# y. For example:
x=: >;:'That they hunted from hill' y=: >;:'second time me to plain' b=: 0 1 1 0 0 1 0 0 1 1 mrg=: 1 : '/:@/:@(x"_) { ,' x([ ; ] ; (,.b)"_ ; b mrg)y +----------------------+ |That |second|0|That | |they |time |1|second| |hunted|me |1|time | |from |to |0|they | |hill |plain |0|hunted| | | |1|me | | | |0|from | | | |0|hill | | | |1|to | | | |1|plain | +----------------------+
The form of the function b mrg obtained by applying the adverb mrg suggests the form of a function MRG to be applied to a Boolean left argument and a right argument formed as the catenation of the original arguments. Thus:
b mrg /:@/:@(0 1 1 0 0 1 0 0 1 1"_) { , MRG=: /:@/:@[ { ] b MRG x,y That second time they hunted me from hill to plain
The argument b need not be Boolean, but may be anything of the requisite number of items that is in the domain of /:. For example:
b=: 0 2 2 1 0 2 2 2 0 0 1 1 2 1 2 1 1 1 1 1 y0=: 'abcd' [ y1=: '123456789' [ y2=: 'zzzzzzz' b MRG y0,y1,y2 azz1bzzzcd23z4z56789
a0=: mrg=: 1 : '/:@/:@(x"_) { ,' | x b mrg y merges x and y |
m1=: MRG=: /:@/:@[ { ] | b MRG x,y is equivalent to above |
d2=: alt=: ,@,. | Merge items from x and y alternately |
For example:
x=: 'temr rtes' y=: 'h axbohr ' x alt y the marx brothers
An argument can be amended by replacing those cells selected by an index, by the cells of another argument. For example:
x=: 'ABCD' [ y=: 'abcdefghij' i=: 4 2 8 6 i{y ecig ]z=: x i} y abBdAfDhCj m=: a.{~(a. i. 'A')+i.5 5 ]i=: 2 # &.> i. # m +-------------------+ |0 0|1 1|2 2|3 3|4 4| +-------------------+ x=: '+-*%^' m ; (i{m) ; x ; x i} m +-----------------------+ |ABCDE|AGMSY|+-*%^|+BCDE| |FGHIJ| | |F-HIJ| |KLMNO| | |KL*NO| |PQRST| | |PQR%T| |UVWXY| | |UVWX^| +-----------------------+
Amendment can also be made by using a function that selects a portion of its argument. For example:
IR=: @(i.@$@]) NB. Adverb to select indices of (ravelled) table right argument A=: IR } NB. Adverb to amend selected portion of right argument d=: (<0 1)&|: NB. Function to select diagonal of a table '+-*%^' (] ; d@] ; ]IR ; d IR ; d IR } ; d A) m +---------------------------------------------------+ |ABCDE|AGMSY| 0 1 2 3 4|0 6 12 18 24|+BCDE|+BCDE| |FGHIJ| | 5 6 7 8 9| |F-HIJ|F-HIJ| |KLMNO| |10 11 12 13 14| |KL*NO|KL*NO| |PQRST| |15 16 17 18 19| |PQR%T|PQR%T| |UVWXY| |20 21 22 23 24| |UVWX^|UVWX^| +---------------------------------------------------+ NB. m; diag m; Indices of ravelled m; Indices of diagonal; Amendments ur=: 2 _3&{. NB. Select upper right corner (2 3$'+-*%^!') (] ; ur@] ; ]IR ; ur IR ; ur IR } ; ur A) m +------------------------------------------+ |ABCDE|CDE| 0 1 2 3 4|2 3 4|AB+-*|AB+-*| |FGHIJ|HIJ| 5 6 7 8 9|7 8 9|FG%^!|FG%^!| |KLMNO| |10 11 12 13 14| |KLMNO|KLMNO| |PQRST| |15 16 17 18 19| |PQRST|PQRST| |UVWXY| |20 21 22 23 24| |UVWXY|UVWXY| +------------------------------------------+
a3=: IR=: @(i.@$@]) | f IR selects indices of ravelled rgt arg |
m4=: d=: (<0 1)&|: | Function to select diagonal of table |
m5=: ur=: 2 _3&{. | Function to select upper right corner |