Here is what seems to me a much easier/saner version of amend to use,
especially for chained amends. 2 versions, one called for L:0 y, then other
without. The dyad version doesn't seem totally necessary.
amend_z_ =: 2 : 0 NB. v is n or n{"num
if. 0 = 4!:0 <'v' do. s =. v else. s =. v y end.
(u (s{y)) (s}) y
:
if. 0 = 4!:0 <'v' do. s =. v else. s =. v y end.
(x u (s{y)) (s}) y
)
amendL0_z_ =: 2 : 0 NB. v is n or n{"num
if. 0 = 4!:0 <'v' do. s =. v else. s =. v y end.
(u (s{ L:0 y)) (s}) L:0 y
:
if. 0 = 4!:0 <'v' do. s =. v else. s =. v y end.
(x u (s{ L:0)y) (s} L:0) y
)
([: 2: amend 0 3: amend 1) amend 2 ( i. 5 3) NB. replace in row 2, col 0
and 1
0 1 2
3 4 5
2 3 8
9 10 11
12 13 14
([: 2: amend 0 3: amend 1) amend 2 &.|: ( i. 5 3) NB. replace in col 2, row
0 and 1
0 1 2
3 4 3
6 7 8
9 10 11
12 13 14
((0;'f')"_ amend 1 3) amendL0 3 (<"1 &. |: i. 5 3), <'ABCDE' NB. for row
3, replace col 1 and 3
┌──────────┬──────────┬───────────┬─────┐
│0 3 6 9 12│1 4 7 0 13│2 5 8 11 14│ABCfE│
└──────────┴──────────┴───────────┴─────┘
( 2&+ L:0 amend 1 2) amendL0 3 4 (<"1 &. |: i. 5 3), <'ABCDE' NB. use
function y, to replace col 1 and 2 of row 3 and 4
┌──────────┬───────────┬───────────┬─────┐
│0 3 6 9 12│1 4 7 12 15│2 5 8 13 16│ABCDE│
└──────────┴───────────┴───────────┴─────┘
or dyad:
2( + L:0 amend 1 2) amendL0 3 4 (<"1 &. |: i. 5 3), <'ABCDE'
┌──────────┬───────────┬───────────┬─────┐
│0 3 6 9 12│1 4 7 12 15│2 5 8 13 16│ABCDE│
└──────────┴───────────┴───────────┴─────┘
more complex function: change row 3 from 9;10;11;'D' to 11;10;9;'f'
((<'f') ,~ |.@:}:) amendL0 3 (<"1 &. |: i. 5 3), <'ABCDE'
┌───────────┬───────────┬──────────┬─────┐
│0 3 6 11 12│1 4 7 10 13│2 5 8 9 14│ABCfE│
└───────────┴───────────┴──────────┴─────┘
for improvements, the v side could be a 2 sided gerund for the {`} sides which
could be for example {"1`}"1 or {L:0`}L:0
Thank you Raul and Aai for your help.
----- Original Message -----
From: Raul Miller <[email protected]>
To: Programming forum <[email protected]>
Cc:
Sent: Saturday, September 21, 2013 12:50:48 AM
Subject: Re: [Jprogramming] Amend trickiness part 2
On Fri, Sep 20, 2013 at 6:21 PM, Pascal Jasmin <[email protected]> wrote:
>> I wanted to use the dyadic definition, but did not have
>> a meaningful value for x, so I used ~ (giving the effect of y m} y).
>
> There seems to be a lot more going on there. There is already an x value of
> 1.
Here?
1 3:`2:`]}~&.>@{`[`]} <"1 |: i. 5 3
In this case, the ~ in the phrase 3:`2:`]}~&.>@... appears in the verb
which is on the left side of an @
And we always use the monadic definition of the verb on the left of the @
So we use the monadic definition of 3:`2:`]}~&.> and so we use the
monadic definition of 3:`2:`]}~
(And the tilde means we use the dyadic definition of 3:`2:`]})
That said, I should point out that there's something subtle going on
here, with word boundaries:
;:'1 2 3: 4 5'
+---+--+---+
|1 2|3:|4 5|
+---+--+---+
> for instance with the value x of _3 _2 0 or 2, an answer is provided,
> but item 1 of the middle cell is also modified. Other values of x
> give various domain or index errors. If the '~' is removed, then x
> values of 0 to 2, produce a middle cell of 6 to 8. I can't follow
> what causes those results at all.
The x that I think you are speaking of here is relevant for the verb
on the right hand side of the @ so lets simplify the left hand side
and play with the expression a bit:
1 <@0:@{`[`]} <"1 |: i. 5 3
+----------+-+-----------+
|0 3 6 9 12|0|2 5 8 11 14|
+----------+-+-----------+
_3 _2 0 <@0:@{`[`]} <"1 |: i. 5 3
+-+-+-----------+
|0|0|2 5 8 11 14|
+-+-+-----------+
2 <@0:@{`[`]} <"1 |: i. 5 3
+----------+-----------+-+
|0 3 6 9 12|1 4 7 10 13|0|
+----------+-----------+-+
Note also that index _3 is (for this argument) the same as index 0.
> Next I'm trying to change columns 1 and 4 row 2 to 3 and 'F', but I expect
> that to be harder.
Well... I'm not quite sure what you want there, but let us assume that
you want to change
(<"1 &. |: i. 5 3), <'ABCDE'
+----------+-----------+-----------+-----+
|0 3 6 9 12|1 4 7 10 13|2 5 8 11 14|ABCDE|
+----------+-----------+-----------+-----+
to
+----------+-----------+-----------+-----+
|0 3 3 9 12|1 4 7 10 13|2 5 8 11 14|ABFDE|
+----------+-----------+-----------+-----+
if so, a phrase that would accomplish that would be:
(3;7;8;'F') [`2:`]}&.> data
Here, I've presumed that you are calling the contents of a box a
"column" and that you are calling the location within a box a "row".
I've also presumed that you have added 1 to the "column index" when
you are speaking of "columns 1 and 4". I could easily be wrong,
though, about your intentions. If so, let us know and we can try
again?
Thanks,
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm