the pain of strings


(David Baraff) #1

I know, I’ve read tons about about this. I sympathize. Unicode, it’s all very complex.

But.

  BUT.

Python:
  shortID = longerDeviceID[-2:] # give me the last two characters

Swift:
  let shortID = String(longerDeviceID.characters.dropFirst(longerDeviceID.characters.count - 2))

I can’t even read the above without my eyes glazing over. As has been pointed out, an API which demands this much verbosity is crippling for many developers, to say the least.

With Swift 4, am I correct that it will be at least:

  let shortID = String(longerDeviceID.dropFirst(longerDeviceID.count - 2))


(Brent Royal-Gordon) #2

This actually perfectly illustrates why Swift is designed the way it is.

The first thing to notice is that the Python version is terse, but it's actually impossible to know what it actually *does*. Is it operating on bytes, code units, Unicode scalars, or grapheme clusters? Well, that depends: is this Python 2 or Python 3? If it's 2, is this a `string` or a `unicode`? If it's 3, is it a `string` or a `bytes`? And if it is one of the Unicode-aware types, how are those indexed? (I honestly don't know—I can't find anything about that in the documentation.) And forget about understanding its performance characteristics—that's an implementation detail.

The second thing to notice is that your Swift solution is very inefficient. It counts all the characters in the string, subtracts two, then counts all but the last two characters in the string before returning the last two. That is, it unnecessarily walks over the entire string twice. If you read your expression closely, all of this behavior is plainly stated in the code.

What you actually want to do is count back two characters from the *end*, like so:

  let shortID = String(longerDeviceID.characters[longerDeviceID.characters.index(longerDeviceID.characters.endIndex, offsetBy: -2) ..< longerDeviceID.characters.endIndex])

Or, in Swift 4:

  let shortID = String(longerDeviceID[longerDeviceID.index(longerDeviceID.endIndex, offsetBy: -2)...])

Or, as Adrian Zubarev pointed out, you can use the very convenient `suffix(_:)` method, available on any `Sequence`:

  let shortID = String(longerDeviceID.characters.suffix(2)) // Swift 3
  let shortID = String(longerDeviceID.suffix(2)) // Swift 4

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.

···

On Jun 30, 2017, at 2:44 PM, David Baraff via swift-users <swift-users@swift.org> wrote:

Python:
shortID = longerDeviceID[-2:] # give me the last two characters

Swift:
let shortID = String(longerDeviceID.characters.dropFirst(longerDeviceID.characters.count - 2))

--
Brent Royal-Gordon
Architechies


(Charles Srstka) #3

Hey, look on the bright side. They could have done what they’ve done with Data; make it *look* like simple integer subscripts will work, when actually they will blow up in your face at runtime.

func parseSome(data: Data) {
  let magic = data[0..<4]
}

That’ll read the first four bytes of ‘data’ into ‘magic’, right? Well, not if someone does this when calling the function:

func parseSome(data: inputData[someStart..<someEnd])

Now the ‘data’ your function has received is actually a slice, and when it tries to access index 0, it’ll actually access the first byte of the original data the slice was made from, *not* the first byte of the slice! On the currently available version of Xcode, that’ll read the wrong data; with the current sources from the trunk, it’ll crash. Either way, you won’t know until runtime.

Charles

···

On Jun 30, 2017, at 4:44 PM, David Baraff via swift-users <swift-users@swift.org> wrote:

I know, I’ve read tons about about this. I sympathize. Unicode, it’s all very complex.

But.

BUT.

Python:
shortID = longerDeviceID[-2:] # give me the last two characters

Swift:
let shortID = String(longerDeviceID.characters.dropFirst(longerDeviceID.characters.count - 2))

I can’t even read the above without my eyes glazing over. As has been pointed out, an API which demands this much verbosity is crippling for many developers, to say the least.

With Swift 4, am I correct that it will be at least:

let shortID = String(longerDeviceID.dropFirst(longerDeviceID.count - 2))


(Adrian Zubarev) #4

let longString = "1234567890"
print(longString.suffix(2)) // prints "90"

···

--
Adrian Zubarev
Sent with Airmail

Am 30. Juni 2017 um 23:45:01, David Baraff via swift-users (swift-users@swift.org) schrieb:

I know, I’ve read tons about about this. I sympathize. Unicode, it’s all very complex.

But.

BUT.

Python:
shortID = longerDeviceID[-2:] # give me the last two characters

Swift:
let shortID = String(longerDeviceID.characters.dropFirst(longerDeviceID.characters.count - 2))

I can’t even read the above without my eyes glazing over. As has been pointed out, an API which demands this much verbosity is crippling for many developers, to say the least.

With Swift 4, am I correct that it will be at least:

let shortID = String(longerDeviceID.dropFirst(longerDeviceID.count - 2))

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Adrian Zubarev) #5

This looks way better than the subscript in Python and 1000 times better than your example. It might be a good idea to look up possible API first before writing such ugly long lines. I mean they get the job done, but just why so complicated? :frowning:

···

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 00:08:47, Adrian Zubarev (adrian.zubarev@devandartist.com) schrieb:

let longString = "1234567890"
print(longString.suffix(2)) // prints "90"

--
Adrian Zubarev
Sent with Airmail

Am 30. Juni 2017 um 23:45:01, David Baraff via swift-users (swift-users@swift.org) schrieb:

I know, I’ve read tons about about this. I sympathize. Unicode, it’s all very complex.

But.

BUT.

Python:
shortID = longerDeviceID[-2:] # give me the last two characters

Swift:
let shortID = String(longerDeviceID.characters.dropFirst(longerDeviceID.characters.count - 2))

I can’t even read the above without my eyes glazing over. As has been pointed out, an API which demands this much verbosity is crippling for many developers, to say the least.

With Swift 4, am I correct that it will be at least:

let shortID = String(longerDeviceID.dropFirst(longerDeviceID.count - 2))

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(^) #6

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.


(David Baraff) #7

I’m sorry, but I don’t see suffix() as a member function in any documentation, nor does it complete in Xcode.
Is this perhaps only in Swift 4?

If so, that’s a definite improvement!

···

Begin forwarded message:

From: Adrian Zubarev <adrian.zubarev@devandartist.com>
Subject: Re: [swift-users] the pain of strings
Date: June 30, 2017 at 3:13:42 PM PDT
To: David Baraff <davidbaraff@gmail.com>
Cc: swift-users@swift.org

This looks way better than the subscript in Python and 1000 times better than your example. It might be a good idea to look up possible API first before writing such ugly long lines. I mean they get the job done, but just why so complicated? :frowning:

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 00:08:47, Adrian Zubarev (adrian.zubarev@devandartist.com <mailto:adrian.zubarev@devandartist.com>) schrieb:

let longString = "1234567890"
print(longString.suffix(2)) // prints "90"

--
Adrian Zubarev
Sent with Airmail

Am 30. Juni 2017 um 23:45:01, David Baraff via swift-users (swift-users@swift.org <mailto:swift-users@swift.org>) schrieb:

I know, I’ve read tons about about this. I sympathize. Unicode, it’s all very complex.

But.

BUT.

Python:
shortID = longerDeviceID[-2:] # give me the last two characters

Swift:
let shortID = String(longerDeviceID.characters.dropFirst(longerDeviceID.characters.count - 2))

I can’t even read the above without my eyes glazing over. As has been pointed out, an API which demands this much verbosity is crippling for many developers, to say the least.

With Swift 4, am I correct that it will be at least:

let shortID = String(longerDeviceID.dropFirst(longerDeviceID.count - 2))

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(David Baraff) #8

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.)

···

On Jun 30, 2017, at 9:48 PM, Taylor Swift via swift-users <swift-users@swift.org> wrote:

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(^) #9

I agree, and the new format of the swift 4 API docs are much harder to find
things in than the swift 3 ones. Perusing all the types and free functions
in alphabetical order is so much easier than trying to guess what “topic”
something is sorted under.

···

On Sat, Jul 1, 2017 at 12:54 AM, David Baraff <davidbaraff@gmail.com> wrote:

On Jun 30, 2017, at 9:48 PM, Taylor Swift via swift-users < > swift-users@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Adrian Zubarev) #10

Well you’ve mentioned Swift 4 in your original post, therefore I provided a solution using Swift 4. It’s returning a view called `Substring`.

···

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 00:38:42, David Baraff (davidbaraff@gmail.com) schrieb:

I’m sorry, but I don’t see suffix() as a member function in any documentation, nor does it complete in Xcode.
Is this perhaps only in Swift 4?

If so, that’s a definite improvement!

Begin forwarded message:

From: Adrian Zubarev <adrian.zubarev@devandartist.com>
Subject: Re: [swift-users] the pain of strings
Date: June 30, 2017 at 3:13:42 PM PDT
To: David Baraff <davidbaraff@gmail.com>
Cc: swift-users@swift.org

This looks way better than the subscript in Python and 1000 times better than your example. It might be a good idea to look up possible API first before writing such ugly long lines. I mean they get the job done, but just why so complicated? :frowning:

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 00:08:47, Adrian Zubarev (adrian.zubarev@devandartist.com) schrieb:

let longString = "1234567890"
print(longString.suffix(2)) // prints "90"

--
Adrian Zubarev
Sent with Airmail

Am 30. Juni 2017 um 23:45:01, David Baraff via swift-users (swift-users@swift.org) schrieb:

I know, I’ve read tons about about this. I sympathize. Unicode, it’s all very complex.

But.

BUT.

Python:
shortID = longerDeviceID[-2:] # give me the last two characters

Swift:
let shortID = String(longerDeviceID.characters.dropFirst(longerDeviceID.characters.count - 2))

I can’t even read the above without my eyes glazing over. As has been pointed out, an API which demands this much verbosity is crippling for many developers, to say the least.

With Swift 4, am I correct that it will be at least:

let shortID = String(longerDeviceID.dropFirst(longerDeviceID.count - 2))

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(David Baraff) #11

I only know a little bit of what I’ve read online in blog posts and such. I don’t have access to the Swift 4 API documentation, since i’m not running the xcode beta yet.

Is there someplace I can see the actual new API’s for String, in swift 4? i googled but haven’t found it yet.

···

On Jun 30, 2017, at 5:44 PM, Adrian Zubarev <adrian.zubarev@devandartist.com> wrote:

Well you’ve mentioned Swift 4 in your original post, therefore I provided a solution using Swift 4. It’s returning a view called `Substring`.

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 00:38:42, David Baraff (davidbaraff@gmail.com <mailto:davidbaraff@gmail.com>) schrieb:

I’m sorry, but I don’t see suffix() as a member function in any documentation, nor does it complete in Xcode.
Is this perhaps only in Swift 4?

If so, that’s a definite improvement!

Begin forwarded message:

From: Adrian Zubarev <adrian.zubarev@devandartist.com <mailto:adrian.zubarev@devandartist.com>>
Subject: Re: [swift-users] the pain of strings
Date: June 30, 2017 at 3:13:42 PM PDT
To: David Baraff <davidbaraff@gmail.com <mailto:davidbaraff@gmail.com>>
Cc: swift-users@swift.org <mailto:swift-users@swift.org>

This looks way better than the subscript in Python and 1000 times better than your example. It might be a good idea to look up possible API first before writing such ugly long lines. I mean they get the job done, but just why so complicated? :frowning:

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 00:08:47, Adrian Zubarev (adrian.zubarev@devandartist.com <mailto:adrian.zubarev@devandartist.com>) schrieb:

let longString = "1234567890"
print(longString.suffix(2)) // prints "90"

--
Adrian Zubarev
Sent with Airmail

Am 30. Juni 2017 um 23:45:01, David Baraff via swift-users (swift-users@swift.org <mailto:swift-users@swift.org>) schrieb:

I know, I’ve read tons about about this. I sympathize. Unicode, it’s all very complex.

But.

BUT.

Python:
shortID = longerDeviceID[-2:] # give me the last two characters

Swift:
let shortID = String(longerDeviceID.characters.dropFirst(longerDeviceID.characters.count - 2))

I can’t even read the above without my eyes glazing over. As has been pointed out, an API which demands this much verbosity is crippling for many developers, to say the least.

With Swift 4, am I correct that it will be at least:

let shortID = String(longerDeviceID.dropFirst(longerDeviceID.count - 2))

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users


(Kyle Murray) #12

Hi David,

You can see the new APIs for Swift 4's String here: https://developer.apple.com/documentation/swift/string?changes=latest_minor <https://developer.apple.com/documentation/swift/string?changes=latest_minor>

The page indicates changes and additions since Xcode 8.3's Swift 3.1, corresponding with the version of Swift 4 that's in Xcode 9 beta 2.

Hope this helps,
Kyle

···

On Jun 30, 2017, at 5:57 PM, David Baraff via swift-users <swift-users@swift.org> wrote:

I only know a little bit of what I’ve read online in blog posts and such. I don’t have access to the Swift 4 API documentation, since i’m not running the xcode beta yet.

Is there someplace I can see the actual new API’s for String, in swift 4? i googled but haven’t found it yet.

On Jun 30, 2017, at 5:44 PM, Adrian Zubarev <adrian.zubarev@devandartist.com <mailto:adrian.zubarev@devandartist.com>> wrote:

Well you’ve mentioned Swift 4 in your original post, therefore I provided a solution using Swift 4. It’s returning a view called `Substring`.

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 00:38:42, David Baraff (davidbaraff@gmail.com <mailto:davidbaraff@gmail.com>) schrieb:

I’m sorry, but I don’t see suffix() as a member function in any documentation, nor does it complete in Xcode.
Is this perhaps only in Swift 4?

If so, that’s a definite improvement!

Begin forwarded message:

From: Adrian Zubarev <adrian.zubarev@devandartist.com <mailto:adrian.zubarev@devandartist.com>>
Subject: Re: [swift-users] the pain of strings
Date: June 30, 2017 at 3:13:42 PM PDT
To: David Baraff <davidbaraff@gmail.com <mailto:davidbaraff@gmail.com>>
Cc: swift-users@swift.org <mailto:swift-users@swift.org>

This looks way better than the subscript in Python and 1000 times better than your example. It might be a good idea to look up possible API first before writing such ugly long lines. I mean they get the job done, but just why so complicated? :frowning:

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 00:08:47, Adrian Zubarev (adrian.zubarev@devandartist.com <mailto:adrian.zubarev@devandartist.com>) schrieb:

let longString = "1234567890"
print(longString.suffix(2)) // prints "90"

--
Adrian Zubarev
Sent with Airmail

Am 30. Juni 2017 um 23:45:01, David Baraff via swift-users (swift-users@swift.org <mailto:swift-users@swift.org>) schrieb:

I know, I’ve read tons about about this. I sympathize. Unicode, it’s all very complex.

But.

BUT.

Python:
shortID = longerDeviceID[-2:] # give me the last two characters

Swift:
let shortID = String(longerDeviceID.characters.dropFirst(longerDeviceID.characters.count - 2))

I can’t even read the above without my eyes glazing over. As has been pointed out, an API which demands this much verbosity is crippling for many developers, to say the least.

With Swift 4, am I correct that it will be at least:

let shortID = String(longerDeviceID.dropFirst(longerDeviceID.count - 2))

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(David Baraff) #13

I’ve written the more common new swift 4 functions i need as extensions in my project so i can write swift 4 syntax while waiting for xcode 9 to become stable/safe enough for use.

Thanks, this was all super-helpful for me. Looking forward to the Substring API, quite a bit.

···

On Jun 30, 2017, at 6:06 PM, Kyle Murray <kyle_murray@apple.com> wrote:

Hi David,

You can see the new APIs for Swift 4's String here: https://developer.apple.com/documentation/swift/string?changes=latest_minor <https://developer.apple.com/documentation/swift/string?changes=latest_minor>wow, that’s awesome — i never knew you could get a diff view. good stuff!!


(Adrian Zubarev) #14

The best docs you can get without Xcode I know about is this one here:

https://developer.apple.com/documentation/swift/string

In Swift 4 a String will yet again become a collection type.

···

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 02:57:05, David Baraff (davidbaraff@gmail.com) schrieb:

I only know a little bit of what I’ve read online in blog posts and such. I don’t have access to the Swift 4 API documentation, since i’m not running the xcode beta yet.

Is there someplace I can see the actual new API’s for String, in swift 4? i googled but haven’t found it yet.

On Jun 30, 2017, at 5:44 PM, Adrian Zubarev <adrian.zubarev@devandartist.com> wrote:

Well you’ve mentioned Swift 4 in your original post, therefore I provided a solution using Swift 4. It’s returning a view called `Substring`.

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 00:38:42, David Baraff (davidbaraff@gmail.com) schrieb:

I’m sorry, but I don’t see suffix() as a member function in any documentation, nor does it complete in Xcode.
Is this perhaps only in Swift 4?

If so, that’s a definite improvement!

Begin forwarded message:

From: Adrian Zubarev <adrian.zubarev@devandartist.com>
Subject: Re: [swift-users] the pain of strings
Date: June 30, 2017 at 3:13:42 PM PDT
To: David Baraff <davidbaraff@gmail.com>
Cc: swift-users@swift.org

This looks way better than the subscript in Python and 1000 times better than your example. It might be a good idea to look up possible API first before writing such ugly long lines. I mean they get the job done, but just why so complicated? :frowning:

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 00:08:47, Adrian Zubarev (adrian.zubarev@devandartist.com) schrieb:

let longString = "1234567890"
print(longString.suffix(2)) // prints "90"

--
Adrian Zubarev
Sent with Airmail

Am 30. Juni 2017 um 23:45:01, David Baraff via swift-users (swift-users@swift.org) schrieb:

I know, I’ve read tons about about this. I sympathize. Unicode, it’s all very complex.

But.

BUT.

Python:
shortID = longerDeviceID[-2:] # give me the last two characters

Swift:
let shortID = String(longerDeviceID.characters.dropFirst(longerDeviceID.characters.count - 2))

I can’t even read the above without my eyes glazing over. As has been pointed out, an API which demands this much verbosity is crippling for many developers, to say the least.

With Swift 4, am I correct that it will be at least:

let shortID = String(longerDeviceID.dropFirst(longerDeviceID.count - 2))

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(David Baraff) #15

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 Jun 30, 2017, at 10:51 PM, Taylor Swift <kelvin13ma@gmail.com> wrote:

On Sat, Jul 1, 2017 at 12:54 AM, David Baraff <davidbaraff@gmail.com <mailto:davidbaraff@gmail.com>> wrote:

On Jun 30, 2017, at 9:48 PM, Taylor Swift via swift-users <swift-users@swift.org <mailto:swift-users@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@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users


(Adrian Zubarev) #16

Plus if you want to play with Swift 4 without running a toolchain or the new Xcode you can do it in your browser:

https://swift.sandbox.bluemix.net/#/repl

Just change that repl to Swift 4.

···

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 03:02:38, Adrian Zubarev (adrian.zubarev@devandartist.com) schrieb:

The best docs you can get without Xcode I know about is this one here:

https://developer.apple.com/documentation/swift/string

In Swift 4 a String will yet again become a collection type.

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 02:57:05, David Baraff (davidbaraff@gmail.com) schrieb:

I only know a little bit of what I’ve read online in blog posts and such. I don’t have access to the Swift 4 API documentation, since i’m not running the xcode beta yet.

Is there someplace I can see the actual new API’s for String, in swift 4? i googled but haven’t found it yet.

On Jun 30, 2017, at 5:44 PM, Adrian Zubarev <adrian.zubarev@devandartist.com> wrote:

Well you’ve mentioned Swift 4 in your original post, therefore I provided a solution using Swift 4. It’s returning a view called `Substring`.

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 00:38:42, David Baraff (davidbaraff@gmail.com) schrieb:

I’m sorry, but I don’t see suffix() as a member function in any documentation, nor does it complete in Xcode.
Is this perhaps only in Swift 4?

If so, that’s a definite improvement!

Begin forwarded message:

From: Adrian Zubarev <adrian.zubarev@devandartist.com>
Subject: Re: [swift-users] the pain of strings
Date: June 30, 2017 at 3:13:42 PM PDT
To: David Baraff <davidbaraff@gmail.com>
Cc: swift-users@swift.org

This looks way better than the subscript in Python and 1000 times better than your example. It might be a good idea to look up possible API first before writing such ugly long lines. I mean they get the job done, but just why so complicated? :frowning:

--
Adrian Zubarev
Sent with Airmail

Am 1. Juli 2017 um 00:08:47, Adrian Zubarev (adrian.zubarev@devandartist.com) schrieb:

let longString = "1234567890"
print(longString.suffix(2)) // prints "90"

--
Adrian Zubarev
Sent with Airmail

Am 30. Juni 2017 um 23:45:01, David Baraff via swift-users (swift-users@swift.org) schrieb:

I know, I’ve read tons about about this. I sympathize. Unicode, it’s all very complex.

But.

BUT.

Python:
shortID = longerDeviceID[-2:] # give me the last two characters

Swift:
let shortID = String(longerDeviceID.characters.dropFirst(longerDeviceID.characters.count - 2))

I can’t even read the above without my eyes glazing over. As has been pointed out, an API which demands this much verbosity is crippling for many developers, to say the least.

With Swift 4, am I correct that it will be at least:

let shortID = String(longerDeviceID.dropFirst(longerDeviceID.count - 2))

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(David Baraff) #17

I’ll answer my own question (fired up xcode beta to try some stuff in swift 4):

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()?

and in swift 4, the answer is “yes.” If I simply overload the string subscript operator to operate on CountablePartialRange<Int>, and I assume that positive means offset from the beginning, and negative means relative to the end, then I believe I can write something that is as efficient as one can get, using this syntax:

  let shortDeviceId = getLongerDeviceID[(-2)...]

   let someOtherSlice = getLongerDeviceID[3...] // start at 3 from beginning and go to end

So *I’ll* be happy. I just need to overload to handle all the different range types. I’m sure there’s a reason for not including this in the API (i guess there’s a need to be precise about what “units” one is talking about) but for my purposes it gives an efficient and easy to read API.

I could imagine, just to avoid amiguity, perhaps

   let someOtherSlice = getLongerDeviceID[UTF8.Index(3)...] // start at 3 from beginning and go to end

which wouldn’t be the end of the world (compared with what we have now).

···

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@gmail.com <mailto:davidbaraff@gmail.com>> wrote:

On Jun 30, 2017, at 9:48 PM, Taylor Swift via swift-users <swift-users@swift.org <mailto:swift-users@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@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users