Re: problems with the scoping in eu4

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

Re:

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 

Two criticisms:

1 The function by default has start=1. Calling the function with start=0 is an error. Calling the function with a value out of range is possible. Calling with a negative index is also possible (think Python). This could easily be avoided by either:

a. Asserting start has to be in range 1..length(haystack) or at least > 0 # certainly a good idea.
b. silently ignore the error. If it is filter code you may not be allowed to bail out. But this is not such a good idea as negative numbers will throw errors.

The function looks pretty strange. I really do not see what it is trying to do. Perhaps the remove escaped the loop.

2 Dealing correctly with 1 means start has a value. But there is still a problem about using a while loop. start is pointing at an object, it may not be the object we want to remove..

OK: this is not Euphoria code - it's rather a mish-mash and it's a somewhat different task:

function delchar(char c, string s, int start) return string 
  assert(start in 1..length(s),"start not in range fn: find") 
  -- note: at this point start is in range but probably isn't  
  -- pointing to a delchar 
  loop  
    start = index(s, c, start)  -- find a delchar 
    exit when start = 0 
    s = substr(s,1,start-1) || substr(s,start+1)  
  end loop  
  return s  
end function 

That doesn't falsify anything.

Final note: while with entry do is apart from anything else defensive programming.

If you are going to go into a while loop then it is a very bad idea not to have a valid loop variable.

Dropping into the last line of the while in this particular instance hides the fact that the algorithm was incorrect.

On that point: types are a very good idea. as one can declare a type of (say :) ) index as:

type index(integer i) i > 0 end type

Re my bad creole - I started using it yesterday. I like to have things turn out the way I want not how some translator decides.

Re my original post: The issue about not being able to use:

for i = ... end for
for i = ... end for

seems to be some kind of problem with the interpreter and (perhaps) the OS. The problem wasn't there today when I ran equivalent code, but today I had a strange thing happen:

The code it apparently objected to was printf("a: %s\n",a) OK it was wrong and it told me so - but then I found my PATH variable was mutilated.

It wasn't dreadful. I didn't lose my session or anything - but when I started a new xterm the history didn't have anything from that session. Strange.

I use Slackware 13. I can't give you a full report as the session was pretty screwed up and I wasn't really thinking of you guys - but rather of - how badly is this screwed?

Bill (bye)

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

Search



Quick Links

User menu

Not signed in.

Misc Menu