1. RE: Looking for the best "Parse"...

Euphoria allows certain reverse slices (see refman) which lets you 
simlify your code -

    k = n - m
    if v = 1 then
        p = r & u[m+1..n]
    elsif v = 1 + k then
        p = u[1..k] & r 
    else
        p = u[1..v-1] & r & u[v+m..n]
    end if

to this -

    p = u[1..v-1] & r & u[v+m..n]

Try it.  - Colin


Igor Kachan wrote:
> Hello Cassidy Napoli,
> 
> Just once more iteration with your program 
> and your very interesting *recursive* idea,
> which you can make with the 
> *delete doubl* method to get a simple 
> and understandable code.
> 
> Try please:
> 
> --[begin code]
> include wildcard.e
> 
> global function parseUserText
> (sequence u, --unParsedText -- u
>  sequence e, --escape       -- e
>  sequence r) --replace      -- r
> -- 
>  sequence p  --ParsedText   -- p
>   integer v  --varPos       -- v
>   integer m -- le
>   integer n -- lu
>   integer k
> --  
> 	  v = match(lower(e), lower(u)) -- add explanations in your
>    if not v then return u end if        -- native language and in English
> --
> 	  m = length(e) -- place for explanation
> 	  n = length(u) -- place for explanation
> 	  k = n - m -- place for explanation
>        if v = 1 -- place for explanation
>      then p = r & u[m+1..n] -- place for explanation
>     elsif v = 1 + k -- place for explanation
>      then p = u[1..k] & r -- place for explanation
>      else p = u[1..v-1] & r & u[v+m..n] -- place for explanation
>    end if -- place for explanation
>   return parseUserText(p,e,r) -- place for explanation
> end function
> 
> sequence text
> 
> text = parseUserText("The $A jumped over the lazy dog.","$A","quick 
> brown
> fox")
> 
> puts (1, text)
> 
> --[end code]
> 
> The speed is another question, it requires
> the bench-testings on monotasking OS,
> in plain Dos-32 mode, for example.
> 
> Regards,
> Igor Kachan
> kinz at peterlink.ru
> 
>

new topic     » topic index » view message » categorize

2. RE: Looking for the best "Parse"...

Thanks everyone.  I'm still studying the different methods Jiri, Igor, 
and Colin posted.  But I'll probably uses Igor's slightly improved 
version of my method because I understand it the most clearly.

Jiri - Could you explain more about what is happening using your method? 
 I'm not exactly sure I follow it, what makes it better than recursion?  
I'm willing to take it as a matter of faith that it's faster; even 
though I'm not concerned with marginal speed differences for this 
particular program, I'll store the knowledge away for when I AM 
concerned with speed.

Igor, What's "double delete"?

Thanks Again,
Cassidy

new topic     » goto parent     » topic index » view message » categorize

3. RE: Looking for the best "Parse"...

Igor and Cassidy,

To summarise reverse slices, if you have a sequence s and a position p 
in that sequence, then a slice s[p..p-1] produces an empty sequence {}.  
This is handy when doing operations such as inserting a string into 
another string, since it works regardless of the insertion point.  You 
don't have to make a special case for p = 1 or p = length(s)+1.

For example, you can insert t into s at p by the general formula:

    s = s[1..p-1] & t & s[p..length(s)]

this will work for any value of p from 1 to length(s)+1.  If p = 1 -- 
ie. prepend t to s -- the formula resolves to:

    s = s[1..0] & t & s[1..length(s)]  --> s = t & s

if p = length(s)+1 -- ie. append t to s -- then the formula resolves to:

    s = s[1..length(s)] & t & s[length(s)+1..length(s)]  --> s = s & t

So, Igor's code can be reduced to:

--[begin code]
include wildcard.e

global function parseUserText(sequence u, sequence e, sequence r)
    integer v
    v = match(lower(e), lower(u))
    if not v then
        return u
    end if
    u = u[1..v-1] & r & u[v+length(e)..length(u)]
    return parseUserText(u,e,r)
end function
--[end code]

This routine is short, but will be very slow.  Jiri's approach should be 
used if speed is an issue (which it usually is).

- Colin

Cassidy Napoli wrote:
> Thanks everyone.  I'm still studying the different methods Jiri, Igor, 
> and Colin posted.  But I'll probably uses Igor's slightly improved 
> version of my method because I understand it the most clearly.
> 
> Jiri - Could you explain more about what is happening using your method? 
> 
>  I'm not exactly sure I follow it, what makes it better than recursion?  
> I'm willing to take it as a matter of faith that it's faster; even 
> though I'm not concerned with marginal speed differences for this 
> particular program, I'll store the knowledge away for when I AM 
> concerned with speed.
> 
> Igor, What's "double delete"?
> 
> Thanks Again,
> Cassidy
> 
>

new topic     » goto parent     » topic index » view message » categorize

4. RE: Looking for the best "Parse"...

How about just "do/end do" or "loop"
To me "repeat" insinuates I can loop a specified number of times, 
similar to a for loop w/o the explicit counter variable(I think that 
would be more useful than a simple loop).
For the sake of a single boolean comparison though, I don't think it's 
worthwhile to introduce a new structure to euphoria.

How about...

loop x do
   --code goes here
end loop

Where x is the number of iterations, or 0 for an infinite loop.
A loop with a value of 0 would be optimized to a simple loop when bound.

Chris

Igor Kachan wrote:
> Yes, Derek, *just* is not good and your
> variant is more similar to  Euphoria's
> syntax.
> 
> > Yes, a simple loop construct would be handy. The standard method of 
> > doing
> > this...
> > 
> >   while 1 do
> >   . . .
> >   end while
> > 
> > introduces an extra comparison for each iteration. An alternative syntax
> of
> > ...
> > 
> >   repeat do
> >   . . .
> >   end repeat
> > 
> > might not be such a bad idea.
> > -------
> > Derek
> 
> 
> Regards,
> Igor Kachan
> kinz at peterlink.ru
> 
>

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu