3 ms·
Let'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 slightl
by mikeash 11y ago
Let'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 :)
- mikeash 11y agoWhere are you getting that it's slow?
- fauigerzigerk 11y agoIn my own little ad-hoc micro benchmark it is. That's why I suspected that there was copying going on, but there could be so many other reasons for it that it was probably a pointless exercise in the first place. [Edit] And I mean relatively slow (4x the C++ code), not absysmally slow. Not a big deal.
- mikeash 11y agoGotcha. Do you have optimizations on? If so, I'd guess it's just the overhead of the extra indirection, plus dropFirst does have to examine that first little bit of data to determine the grapheme cluster boundaries.
- fauigerzigerk 11y agoYes I've done a release build.
- 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.