Re: problems with the scoping in eu4

new topic     » goto parent     » topic index » view thread      » older message » newer message
petelomax said...
jeremy said...

with entry is not for every loop, but many loops it makes the code cleaner, no duplication and thus easier to understand, less potential for initial bugs and for bugs due to later maintenance. For example:

public function find_all(object needle, sequence haystack, integer start=1) 
    integer kx = 0 
    while start with entry do 
	kx += 1 
	haystack[kx] = start 
	start += 1 
    entry 
	start = find(needle, haystack, start) 
    end while 
 
    haystack = remove( haystack, kx+1, length( haystack ) ) 
 
    return haystack 
end function 

Jeremy

I have to say I'm in the "don't like with entry" crowd. For me the above looks like the while loop will not run for a find_all(,,0) call (I believe bill has the same impression).

petelomax said...

also did you mean haystack[kx] = haystack[start]?

No, find_all() returns a sequence of indexes so you know where each needle is located in the haystack.

This change would mean that haystack returns either an empty sequence or a sequence of needles, repeated over. length(ret) would tell you how many instances of needles were found, but beyond that it wouldn't be very useful....

petelomax said...

Also, why remove() when haystack=haystack[1..kx] would do the same job, or am I missing something?]

remove() is a builtin that is especially optimized to do work in-place when assigning the result back to the original sequence. Same trick as append(), really.

petelomax said...

The other point that strikes me is that you cannot code:

    while start with entry do 
	integer start 
        ... 
    entry 
	start = find(needle, haystack, start) 
    end while 

Regards, Pete

Maybe that should be allowed to work. Hmm...

But, to make it work right now, one just have to put "integer start" outside of the loop. It may not be ideal, but at least there's no need to duplicate code for this case.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu