RE: Eu 2.5: new sequence functions
- Posted by Tommy Carlier <tommy.carlier at pandora.be> Jan 13, 2004
- 398 views
Andy Serpa wrote: > Tommy Carlier wrote: >> Wouldn't we all like some new (fast as hell) sequence functions? >> >> (length) >> (append) >> (prepend) >> >> s = insert(s, i, o) -- insert object o in sequence s at position i >> s = remove(s, i) -- remove element at position i from sequence s >> s = replace(s, i, o) -- replace element at position i in sequence s with >> >> >> object o >> >> Wouldn't these 3 little (not so complex) functions add a lot of power to >> >> >> Euphoria and its sequence system? Sure, you can already do them right >> now like this: >> >> insert(s, i, o) = s[1..i-1] & o & s[i..length(s)] >> remove(s, i) = s[1..i-1] & s[i+1..length(s)] >> replace(s, i, o) = s[1..i-1] & o & s[i+1..length(s)] >> >> But it might be too slow for applications that need pure power. And the >> above only works if 1 < i < length(s), so you'd have to check for >> boundaries. >> > > Actually, they work in all cases. If the second index of a slice in > exactly 1 less than the first, you get the empty sequence. Perfectly > legal (and documented) Euphoria. > > See, you aren't really indexing the elements, but the spaces between > them. The first index refers to the SPACE BEFORE that element, and the > second index refers to the SPACE AFTER that element. > > So, in s[1..0], both indexes are actually referencing the same spot, so > you get the empty sequence. > Thanks for telling me, I didn't know. But still, these functions are very common, don't you think? And readability of code is very important: I think that 's = replace(s, i, o)' is more readable than 's = s[1..i-1] & o & s[i+1..length(s)]'. And even without the boundary check: these functions as builtin system-functions would be a lot faster than the Euphoria-equivalent. And more optimizations could be done: I think I read that A=append(A,B) is optimized to be faster than A=append(B,C), so A=insert(A,I,O) could also be optimized to be faster than A=insert(B,I,O). And I don't think that they are very complex to implement as builtin functions, right Robert? ;-D ______________ tommy online: http://users.pandora.be/tommycarlier