Re: problems with the scoping in eu4

new topic     » goto parent     » topic index » view thread      » older message » newer message
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).

I also feel that I might be tempted to code:

public function find_all(object needle, sequence haystack, integer start=1) 
    integer kx = 0 
    if find(needle,ignore_set) then 
        start = 0 
    end if 
    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 

It just seems illogical to me that the test occurs first in the source code but is not executed until much later.

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

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

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

Search



Quick Links

User menu

Not signed in.

Misc Menu