4 ms·
This article reads like the Stockholm syndrome for programmers - come on - any modern language needs to handle this stuff in a sane manner. Recently I wanted t
by chvid 11y ago
This article reads like the Stockholm syndrome for programmers - come on - any modern language needs to handle this stuff in a sane manner.
Recently I wanted to skip the first character of a string and then convert it to a float.
Here is the beauty:
(text.substringFromIndex(text.startIndex.advancedBy(1)) as NSString).floatValue
(Notice the start index "concept" and then the "cast" into the other string implementation (NSString) to get the to float conversion method.) In any normal language it would be something like:
Float.parse(text.substring(1))
String handling is in Swift is half-finished at best.
- agumonkey 11y agoI get the idea behind very explicit method names, but seriously, python-like (or whoever invented it) slices are the most straightforward and succint I've seen so far, even without the [concrete:syntax].
- Gankro 11y agoDo you want to proceed forward one byte, "character", grapheme, or codepoint?
- mikeash 11y agoLet's at least write your example in a sane manner: Float(String(s.characters.dropFirst(1))) Is that so bad? The need to convert back to String is slightly annoying, but overall this is not bad. And of course there is the inevitable question: when you say "skip the first character," do you mean the first grapheme cluster, the first code point, the first UTF-16 code unit, or the first UTF-8 code unit? You say that "any modern language" needs to handle this stuff in a sane language? Which modern languages handle all of those possibilities sanely? I agree that the String API has some holes in it. Which, you know, is why I say things like "The String API does have some holes in it...." But in terms of how it's designed, it's the only one I've seen that gets it right.
- fauigerzigerk 11y agoThat's pretty clean. It is also rather inefficient. I think dropFirst copies the sequence and then the result is copied again to create a String and then finally it gets parsed into a Float. No problem for one Float, but parsing a largish CSV file like that may be a different matter. At least I think that's what's happening. I'm not 100% sure.
- mikeash 11y agoNope, it's all just pointer games. I just tested it here and inspected the guts in the debugger. My initial string containing "123456789" contains a pointer internally to 0x000000010006d326 which contains that data. The result of .characters contains that same pointer. The result of .dropFirst(1) contains 0x000000010006d327 and the result of turning it back into a string still contains 0x000000010006d327. That's the great thing about abstract APIs and immutable data types, you rarely need to actually copy anything.
- fauigerzigerk 11y agoMaybe it's a short string optimization in this case, because the docs say that dropFirst is O(n) https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_CollectionType_Protocol/index.html https://developer.apple.com/library/prerelease/ios/documenta... Or they have optimized it after writing the docs. In any event, there's a lot more going on than what is necessary to parse a float. But I guess that's OK if the goal is to have a clean and consistent high level API.
- mikeash 11y agoIn that context, n is the number of items being dropped, not the length of the collection.
- fauigerzigerk 11y agoI wonder what makes it so slow then. I'm looking forward to the day when we can look at the source code :)
- chvid 11y agoThank you for that. Who says you can't learn anything by ranting on HN ... :-) As for the inevitable question - if I write text.substring(1) in JavaScript - why are people not asking the same question?
- mikeash 11y agoI don't know why people aren't asking the same question for JavaScript. Certainly, that call is going to cause trouble sometimes. Just try it on the string "<smiley emoji> I love it." (HN is unhappy with emoji, apparently. Surprise! Stuff that doesn't fit into UTF-16 often breaks all sorts of things.) The results are rather unexpected. But it works often enough that I assume people just don't really think about it much. That's the trouble with string manipulation: it's usually easy to make a 99% solution and hard to make a 100% solution. Swift is attempting to prod you into the 100% solution by making the 99% solution more difficult, and the 100% solution maybe a bit easier.