How to waste computer memory?

Random832 random832 at fastmail.com
Fri Mar 18 11:12:38 EDT 2016


On Fri, Mar 18, 2016, at 10:59, Michael Torrie wrote:
> This seems to me to be a leaky abstraction.  Julia's approach is
> interesting, but it strikes me as somewhat broken as it pretends to do
> O(1) indexing, but in reality it's still O(n) because you still have to
> iterate through the bytes until you find, say, the nth time that doesn't
> raise an exception.  Except for dealing with the ASCII subset of UTF-8,
> I can't really see any time when grabbing whatever resides at the nth
> byte of a UTF-8 string would be useful.

Well, that depends on what "n" is. If it's the value returned by a
search of the same string, then it's useful. So long as, naturally, your
search function returns a byte index.

And, for that matter, you're going to have to explain when grabbing
whatever resides at the nth *character* is useful.

> I guess whether or not you need to find the nth character depends on the
> strength of string functions.  If I searched a string for a particular
> delimiter, I could see it being useful to get whatever is just past the
> delimiter, for example.

Well, you know the length of the delimiter, don't you? Instead of adding
one (who says the delimiter has to be one code point, either?), just add
that.

delim = ","
dlen = len(delim) # length in bytes
result = foo[foo.indexof(delim)+dlen:] # index in bytes, slice by bytes

> Though Python's split() method eliminates the
> need to do that by hand.



More information about the Python-list mailing list