If x and y are from a small domain (e.g. characters),
then rpl3a is faster and leaner:
rpl3=: 4 : 0
t{~&> rpl2&.>/ (t=.x<@,&,y) i.&.>x;y
)
rpl3a=: 4 : 0
'x0 x1'=. x
((x1,a.) {~ (x0,a.) i. ]) y
)
x=: 'abcd',:'ABCD'
y=: a. {~ 1e6 [EMAIL PROTECTED] 256
x (rpl3 -: rpl3a) y
1
ts 'x rpl3 y'
0.124357 1.78308e7
ts 'x rpl3a y'
0.0304843 9.4407e6
ts 'x rpl3a y' NB. J6.02
0.00624549 1.05235e6
----- Original Message -----
From: "R.E. Boss"
Date: Tuesday, July 3, 2007 12:55
Subject: RE: [Jprogramming] Replacement
To: 'Programming forum'
> Thanks for the elegant and faster solution.
>
> Non-numeric x and y can be handled by
> t{~&> rpl12&.>/ (t=.x<@,&,y) i.&.>x;y
>
> R.E. Boss
>
>
>
> > -----Oorspronkelijk bericht-----
> > Van: [EMAIL PROTECTED] [mailto:programming-
> > [EMAIL PROTECTED] Namens Raul Miller
> > Verzonden: dinsdag 3 juli 2007 17:27
> > Aan: Programming forum
> > Onderwerp: Re: [Jprogramming] Replacement
> >
> > On 7/3/07, R.E. Boss wrote:
> > > What is the fastest way to replace the atoms in y which
> occur in {.x by
> > the
> > > corresponding values in {:x, where 2={.$x and 2=#$x?
> >
> > For numeric y, this seems faster:
> > rpl2=: ] - (0 ,~ -/)@[ {~ ] i.~ [EMAIL PROTECTED]
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm