RE: Eu 2.5: new sequence functions
- Posted by Andy Serpa <ac at onehorseshy.com> Jan 13, 2004
- 399 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.