"replace0" can be improved by
- having a special case where the old and new strings
have the same length.
- use expand/amend rather than cut/raze when the
old string is shorter than the new string.
Thus:
ff=: {: a.
NB. x is text; y is (p;q); replace all occurrences of p by q in x
replace0=: 4 : 0
'p q'=. y
d=. p -&# q
b=. p E. x
if. 0<:d do. NB. replacement string not longer
ff -.~ (q,d$ff) ((I. b) +/ i.#p)}x
else. NB. replacement string longer
((b i. 1){.x) , ; q&,&.>(#p)}.&.>b <;.1 x
end.
)
replace=: 4 : 0
'p q'=. y
j=. p [EMAIL PROTECTED] x
if. ''-:j do. x return. end.
select. *d=. p -&# q
case. 1 do. ff -.~ (q,d$ff) (j +/ i.#p)}x
case. 0 do. q (j +/ i.#p)}x
case. _1 do. q k} (0 (d{."1 k)}1$~(#x)+(#j)*|d) #^:_1 x [ k=.
(j+(|d)*i.#j)+/i.#q
end.
)
x=: 1e6 $ 'of the people, by the people, for the people; '
ts=: 6!:2 , 7!:[EMAIL PROTECTED]
ts 'x replace0 y' [ y=: 'people';'rich'
0.0668616 6.55693e6
ts 'x replace y'
0.063969 5.50886e6
ts 'x replace0 y' [ y=: 'the';'one'
0.0647373 4.46042e6
ts 'x replace y'
0.0338585 3.41133e6
ts 'x replace0 y' [ y=: 'the';'them there'
0.129838 2.79676e7
ts 'x replace y'
0.0677608 1.99288e7
----- Original Message -----
From: Roger Hui <[EMAIL PROTECTED]>
Date: Thursday, November 13, 2008 9:05
Subject: Re: [Jprogramming] tacit stringreplace
To: Programming forum <[email protected]>
> > The code for q longer than p should work for the case
> > where q is not longer than p. The extra code is probably
> > due to its being faster.
>
> rep1=: 4 : 0
> 'p q'=. y
> b=. p E. x
> ((b i. 1){.x) , ; q&,&.>(#p)}.&.>b <;.1 x
> )
>
> x=: 1e6 $ 'by the people, of the people, for the people, '
> y=: 'people';'rich'
> x (replace -: rep1) y
> 1
> ts=: 6!:2 , 7!:[EMAIL PROTECTED] NB. time and space
> ts 'x replace y'
> 0.0380342 6.55693e6
> ts 'x rep1 y'
> 0.132868 2.1696e7
>
>
>
> ----- Original Message -----
> From: Roger Hui <[EMAIL PROTECTED]>
> Date: Thursday, November 13, 2008 8:04
> Subject: Re: [Jprogramming] tacit stringreplace
> To: Programming forum <[email protected]>
>
> > I use the following on occasion. It assumes that the ff
> > character does not occur in the text or in the replacement
> > string. Also, if overlapping substrings need to be handled,
> > you have to replace E. by nos in
> > http://www.jsoftware.com/jwiki/Essays/Non-Overlapping_Substrings
> >
> > The code for q longer than p should work for the case
> > where q is not longer than p. The extra code is probably
> > due to its being faster.
> >
> > ff=: {: a.
> >
> > NB. x is text; y is (p;q); replace all occurrences of p by q
> in x
> > replace=: 4 : 0
> > 'p q'=. y
> > d=. p -&# q
> > b=. p E. x
> > if. 0<:d do. NB. replacement string not longer
> > ff -.~ (q,d$ff) ((I. b) +/ i.#p)}x
> > else. NB. replacement string longer
> > ((b i. 1){.x) , ; q&,&.>(#p)}.&.>b <;.1 x
> > end.
> > )
> >
> >
> >
> > ----- Original Message -----
> > From: "L.Tomei" <[EMAIL PROTECTED]>
> > Date: Thursday, November 13, 2008 7:50
> > Subject: RE: [Jprogramming] tacit stringreplace
> > To: [email protected]
> >
> > >
> > > So, let we start to the target of 100.000 posts on J forums.
> > >
> > > Coming back to stringreplace, I understand your explanation
> > > about "tacit vs
> > > explicit".
> > > But I still think that the stringreplace verb defined in
> > > string.ijs could be
> > > improved, at least for some kinds of replacements.
> > > My feel is that 50 (about) lines of J code, even if
> optimized,
> > > are too much
> > > for fhis function.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm