I have been using J for a couple of weeks now and recently started using it for simple problems rather than just toying with concepts.
This problem needs to replace all x characters in a string into y, which works but using a dyad form of the final verb gives me unexpected output.
Let's use the following example input:
input =. 'abcxdefxghi'
First, I need to find the indexes of x characters in the right argument for amend.
findx =. I.@:([:'x'&= ])
findx input NB. 3 7
0 findx input NB. 3 7
1 findx input NB. 3 7
Then, I amend the results of findx with a bonded y on the left.
trxy =. 'y'&(findx })
trxy input NB. abcydefyghi
_1 trxy input NB. domain error
0 trxy input NB. abcxdefxghi <= this is the really unexpected result
1 trxy input NB. abcydefyghi <= somewhat unexpected, works with strictly positive ints
'a' trxy input NB. domain error
There is two things I don't understand:
- why
trxysometimes works as a dyad when I thought I bonded the left side of my amend ? - why a
0left argument stopstrxyfrom working ?
With
1 trxy inputyou are executing1 'y'&(findx }) input– andx u&n yis maybe not what you expect. It is documented (somewhat hidden) on the bottom of this page: https://code.jsoftware.com/wiki/Vocabulary/ampmIt is equivalent to
x (m&n @ ] ^: [) y, thus appliesnony(withmon the left side) forxtimes. That's why with0 trxy yyou aren't executing anything, thusystays the same. With1 trxy yyou are applyingtrxyonce toy. Astrxyhas no trivial inverse,_1 trxy yresults in an error. And because'a'isn't a number, the last one is just a plain error.If you – for whatever reason – just want to be able to write
trxyas a monad and a dyad that ignores the left-hand side, you could usetrxy =. 'y' findx} ]. (As you could also usefindx =. I.@:('x' = ])or justfindx =. [:I. 'x'=].)