[swift-users] the pain of strings
David Baraff
davidbaraff at gmail.com
Sat Jul 1 10:41:53 CDT 2017
> On Jun 30, 2017, at 10:51 PM, Taylor Swift <kelvin13ma at gmail.com> wrote:
>
> Perusing all the types and free functions in alphabetical order is so much easier than trying to guess what “topic” something is sorted under.
I’ve read that the above sentence three times, and I honestly can’t tell if you’re being sarcastic or not!
I find the long web page version of the docs tedious to read. I use Dash (if you have never tried it, I highly recommend it — seldom do I call any tool indispensable, but dash and launchbar are on my very very short list of tools i cannot live without) and it gives me such a nice compact view of the methods in a sidebar, i can pretty easily find anything.
I thought about what someone posted, and almost replied last night, but didn’t because this is a topic where people can become so entrenched and dogmatic. But after some more thought:
> Or, in Swift 4:
>
> let shortID = String(longerDeviceID[longerDeviceID.index(longerDeviceID.endIndex, offsetBy: -2)…])
My reply being: in Python, i’m expressing exactly the same concept, but in a very short form:
longerDeviceID[-2:] # which reads to me, start 2 before the end, and go to the end.
which is exactly what String(longerDeviceID[longerDeviceID.index(longerDeviceID.endIndex, offsetBy: -2)…]) does.
The problem with this though is that there are THREE mentions of longerDeviceID. So in Python i might write
getLongerDeviceID()[-2:]
but in Swift 4 i cannot easily inline this. Or can I? Is there a way of writing something looks like
getLongergDeviceId()[<-2 from end> ... ]
where whatever goes in the <-2 from the end> does NOT refer to getLongerDeviceID()? I.e. I want to talk about -2 from the end as a concept without needing to refer to the specific string I’m talking about. If i could generate an index concept without referring to the string itself in anyway, i’d be happy. it’d be wonderful if i could express “-2 in whatever you feel like is the natural unit for the string you’re operating on.”
Does the new API permit me to do that? If not, can I add my own subscript functions which take a new type, and i’ll invent the concept of “index in natural unit of the string being operated on.”
thanks.
>
> On Sat, Jul 1, 2017 at 12:54 AM, David Baraff <davidbaraff at gmail.com <mailto:davidbaraff at gmail.com>> wrote:
>
>> On Jun 30, 2017, at 9:48 PM, Taylor Swift via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>>
>>
>> Swift's strings were very deliberately designed this way. It's tougher to get off the ground, sure, but it's better in the long run.
>>
>>
>> It probably is, but the correct idiom is not very well known, and sadly most tutorials and unofficial guides are still teaching dumb ways of subscripting into strings (or worse, falling back into NSString methods without mentioning so) so the end result is people writing less performant code rather than more performant code.
>
> An efficient solution doesn’t help if even experienced programmers can’t easily arrive at it. (I’m highly experienced, but I’ll admit I only put in about 5 minutes before I posted that. on the other hand, it shouldn’t take 5 minutes to figure out something that simple with strings. still, maybe i would have done the simple “suffix()” thing had i been looking at the actual swift 4 api’s, but i only had swift 3 api’s in front of me.)
>
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170701/ddb9504d/attachment.html>
More information about the swift-users
mailing list