Re: Weird error messages & more
- Posted by "Boehme, Gabriel" <gboehme at MUSICLAND.COM> Apr 15, 1999
- 337 views
Ralf Nieuwenhuijsen wrote: >While we at fixes/suggestions ... here's again a suggestion I made >some time ago... > >The 'each' keyword, executes the current statement, for every element >of the sequence that follows. > >Example: > > printf (1, "The next number is %2d", {each s}) > >Is equilivent to the current: > > for index = 1 to length(s) do > printf (1, "The next number is %2d", {s[index]}) > end for > >But for readability, maintainability, speed, etc. the first is much >cleaner. I disagree. There's nothing wrong with the readability, maintainability, etc. of the second example. In fact, the first example could be *horribly* confusing, since the loop operation -- instead of being obvious -- is "hidden" in that little "each" keyword, lurking way out there on the end. [Not to mention the serious logistical problems the interpreter would have to face, trying to figure out where the programmer wants the "each" loop to apply to within multiple nested operations. Are you trying to give Rob nightmares? :) ] >And in complex loops, this can make it much more readable. > > puts (each fhandle_list, each text_image) > >Will output to each filehandle in fhandle_list, every line in >text_image. >It is equilivent (some one tell me how to spell this word, my spell >checker claims it doesn't exist) to.. > > for index1 = 1 to length(fhandle_list) do > for index2 = 1 to length(text_image) do > puts (fhandle_list[index1], text_image[index2]) > end for > end for Again, I find example 2 far more "readable" than example 1, since you can clearly see what's going on. Yes, you have to type a little more for example 2, but the benefits of the obvious loops far outweigh any possible disadvantages resulting from the extra typing, IMO. [BTW, the word you're looking for is "equivalent." :) ] >Or look at this: > > s = repeat (0, 10) > each s = getc (fhandle) > >Equilivent to: > > s = repeat (0,10) > for index = 1 to length(s) do > s[index] = getc (fhandle) > end for In Euphoria 2.1, you can include get.e and code this: s = get_bytes(fhandle, 10) Which is much cleaner than either of the above examples. >But the real advantage comes when used in conjunction with >functions.... >Introduce the new keyword 'reply' ... >It means the same as return, except unlike return after processing >the value, the function might continue at that point, if >another value if desired. > >Look at the new count function () > > function count () > integer index > index = 1 > while 1 do > reply index > index += 1 > end while > end function > > s = repeat (0, 10) > each s = count () > -- s now contains: { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } > -- If the above could also have been written as: > s = each count () > -- it would end up in an infinite loop. Why not just code it like this: function count(integer n) sequence s s = repeat(0, n) for i = 1 to n do s[i] = i end for return s end function s = count(10) This is far more readable and maintainable than the "each"/"reply" stuff would be. >For example, what if the built-in find () and match () were written >this way (they would still be backwards compatible), however for >those programs that need it they could use a list of all places >found, rather that just the first place it is found. > > Example: > > sequence s > s = "Hello world, what's up ?" > > printf (1, "A space is found at position: %d", {each find(' ', s)}) You'll get an infinite loop, since "find(' ', s)" will always return 6, the position of the first space in s. Again, why not just make use of how Euphoria actually works, and code it like this: function find_all(object value, sequence s) object not_value sequence list integer f not_value = not value list = {} f = find(value, s) while f do list = append(list, f) -- append to position list s[f] = not_value -- so we don't find this one again f = find(value, s) -- find the next one end while return list end function sequence s, pos s = "Hello world, what's up ?" pos = find_all(' ', s) for i = 1 to length(pos) do printf(1, "A space is found at position: %d\n", pos[i]) end for It's far more obvious what's going on this way. >And like many others, I'm still awaiting the concatonated >assignment... > > { succes_value, return_value } = get (fhandle) I sort of agree with you here, but what happens if the function returns an atom value instead of a sequence? Or a length-3 sequence instead of length-2? Type-check failure, I suppose. I agree that this would greatly help to simplify some code, and actually might *not* require the Euphoria interpreter to be completely rewritten. :) Be seeing you, Gabriel Boehme P.S. Thanks, by the way, for forcing me to come up with the find_all() function! :) ------- Freedom's just another word for not caring about the quality of your work. from a "Dilbert" cartoon by Scott Adams -------