1. RE: Eu 2.5: new sequence functions
- Posted by Andy Serpa <ac at onehorseshy.com> Jan 13, 2004
- 401 views
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.
2. RE: Eu 2.5: new sequence functions
- Posted by Tommy Carlier <tommy.carlier at pandora.be> Jan 13, 2004
- 400 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
3. RE: Eu 2.5: new sequence functions
- Posted by Andy Serpa <ac at onehorseshy.com> Jan 13, 2004
- 384 views
> 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 Yes, yes, very common functions. It could be that Rob has already optimized statements like: s = s[1..i-1] & s[i+1..length(s)] If not, they'd certainly be excellent candidates for built-ins, yes...
4. RE: Eu 2.5: new sequence functions
- Posted by Matt Lewis <matthewwalkerlewis at yahoo.com> Jan 13, 2004
- 379 views
> From: Tommy Carlier > > Tommy Carlier wrote: > >> Wouldn't we all like some new (fast as hell) sequence functions? > >> <snip> > >> 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)] > >> <snip> > 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)]'. Yes, but I think 's[i] = o' beats them both by a mile. There may be some real gains to be had with a builtin insert and remove, however. Matt Lewis
5. RE: Eu 2.5: new sequence functions
- Posted by Tommy Carlier <tommy.carlier at pandora.be> Jan 13, 2004
- 391 views
Matt Lewis wrote: > > From: Tommy Carlier > > > Tommy Carlier wrote: > > >> Wouldn't we all like some new (fast as hell) sequence functions? > > >> > <snip> > > >> 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)] > <snip> > > 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)]'. > > Yes, but I think 's[i] = o' beats them both by a mile. There may be > some real gains to be had with a builtin insert and remove, however. Of course replace is a silly function! Stupid of me. I was thinking of new functions, so I thought insert and remove would be good. Then I thought of combining remove and insert into a new function repace, not realizing that it already exists. Good one, Matt. ______________ tommy online: http://users.pandora.be/tommycarlier