Shortcut for creating arrays


(Arsen Gasparyan) #1

Hey guys,

Very often we need to create an array strings. It's a routine task and I
really like shortcut in Ruby that shortcut makes everyday coding a little
bit easier and fun:

   words = %w[rats live on no evil star]

What do you think about adding something like this in Swift?

Cheers,
Arsen


(Ross O'Brien) #2

Well, you can do this already:
let words = "rats live on no evil star".componentsSeparatedByString(" ")
so I don't know how much a shortcut is needed.

And given '%w' would currently be impossible in Swift as an operator
containing a letter, is there a Swiftier function name or operator for this?

···

On Fri, Apr 8, 2016 at 2:20 PM, Arsen Gasparyan via swift-evolution < swift-evolution@swift.org> wrote:

Hey guys,

Very often we need to create an array strings. It's a routine task and I
really like shortcut in Ruby that shortcut makes everyday coding a little
bit easier and fun:

   words = %w[rats live on no evil star]

What do you think about adding something like this in Swift?

Cheers,
Arsen

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


(James Campbell) #3

Pehaps we could have a function that splits by whitespace.

"h j c k".split()

···

*___________________________________*

*James⎥Alex's Minder*

*james@supmenow.com <james@supmenow.com>⎥supmenow.com <http://supmenow.com>*

*Sup*

*Runway East *

*10 Finsbury Square*

*London*

* EC2A 1AF *

On 8 April 2016 at 15:05, Ross O'Brien via swift-evolution < swift-evolution@swift.org> wrote:

Well, you can do this already:
let words = "rats live on no evil star".componentsSeparatedByString(" ")
so I don't know how much a shortcut is needed.

And given '%w' would currently be impossible in Swift as an operator
containing a letter, is there a Swiftier function name or operator for this?

On Fri, Apr 8, 2016 at 2:20 PM, Arsen Gasparyan via swift-evolution < > swift-evolution@swift.org> wrote:

Hey guys,

Very often we need to create an array strings. It's a routine task and I
really like shortcut in Ruby that shortcut makes everyday coding a little
bit easier and fun:

   words = %w[rats live on no evil star]

What do you think about adding something like this in Swift?

Cheers,
Arsen

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

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


(Vladimir) #4

Also thought about componentsSeparatedByString, clearly says what we have, what we do and what will have in result.
So I'm -1 for special command for such word array.

···

On 08.04.2016 17:05, Ross O'Brien via swift-evolution wrote:

Well, you can do this already:
let words = "rats live on no evil star".componentsSeparatedByString(" ")
so I don't know how much a shortcut is needed.


(Ross O'Brien) #5

A function that splits by whitespace would be this:

"h j c
k".componentsSeparatedByCharactersInSet(NSCharacterSet.whiteCharacterSet())

'componentsSeparatedByWhitespace' might be a nice addition as a shortcut
for that though?

···

On Fri, Apr 8, 2016 at 3:38 PM, James Campbell <james@supmenow.com> wrote:

Pehaps we could have a function that splits by whitespace.

"h j c k".split()

*___________________________________*

*James⎥Alex's Minder*

*james@supmenow.com <james@supmenow.com>⎥supmenow.com
<http://supmenow.com>*

*Sup*

*Runway East *

*10 Finsbury Square*

*London*

* EC2A 1AF *

On 8 April 2016 at 15:05, Ross O'Brien via swift-evolution < > swift-evolution@swift.org> wrote:

Well, you can do this already:
let words = "rats live on no evil star".componentsSeparatedByString(" ")
so I don't know how much a shortcut is needed.

And given '%w' would currently be impossible in Swift as an operator
containing a letter, is there a Swiftier function name or operator for this?

On Fri, Apr 8, 2016 at 2:20 PM, Arsen Gasparyan via swift-evolution < >> swift-evolution@swift.org> wrote:

Hey guys,

Very often we need to create an array strings. It's a routine task and I
really like shortcut in Ruby that shortcut makes everyday coding a little
bit easier and fun:

   words = %w[rats live on no evil star]

What do you think about adding something like this in Swift?

Cheers,
Arsen

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

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


(Dave Abrahams) #6

I want to find a conciseness and simplicity solution. But
componentsSeparatedByCharactersInSet(NSCharacterSet.whiteCharacterSet()) scares
me :slight_smile:

Yes, we should have words.split() at some point. IMO this is in scope
for a redesign of strings that is expected, but not on the immediate
horizon.

···

on Fri Apr 08 2016, Arsen Gasparyan <swift-evolution@swift.org> wrote:

On Fri, Apr 8, 2016 at 7:39 PM James Campbell > <james@supmenow.com> wrote:

    Exactly

    Sent from Supmenow.com

    On Fri, Apr 8, 2016 at 9:38 AM -0700, "Ross O'Brien" > <narrativium+swift@gmail.com> wrote:

        A function that splits by whitespace would be this:

        "h j c k".componentsSeparatedByCharactersInSet
        (NSCharacterSet.whiteCharacterSet())

        'componentsSeparatedByWhitespace' might be a nice addition as a shortcut
        for that though?

        On Fri, Apr 8, 2016 at 3:38 PM, James Campbell <james@supmenow.com> > wrote:

            Pehaps we could have a function that splits by whitespace.

            "h j c k".split()

                        ___________________________________

            James⎥Alex's Minder

            james@supmenow.com⎥supmenow.com

            Sup

            Runway East

            10 Finsbury Square

            London

             EC2A 1AF

            On 8 April 2016 at 15:05, Ross O'Brien via swift-evolution > <swift-evolution@swift.org> wrote:

                Well, you can do this already:
                let words = "rats live on no evil
                star".componentsSeparatedByString(" ")
                so I don't know how much a shortcut is needed.

                And given '%w' would currently be impossible in Swift as an
                operator containing a letter, is there a Swiftier function name
                or operator for this?

                On Fri, Apr 8, 2016 at 2:20 PM, Arsen Gasparyan via > swift-evolution > <swift-evolution@swift.org> wrote:

                    Hey guys,

                    Very often we need to create an array strings. It's a
                    routine task and I really like shortcut in Ruby that
                    shortcut makes everyday coding a little bit easier and fun:

                    words = %w[rats live on no evil star]

                    What do you think about adding something like this in Swift?

                    Cheers,
                    Arsen

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

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

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

--
Dave


(James Campbell) #7

Exactly

···

Sent from Supmenow.com

On Fri, Apr 8, 2016 at 9:38 AM -0700, "Ross O'Brien" <narrativium+swift@gmail.com> wrote:

A function that splits by whitespace would be this:
"h j c k".componentsSeparatedByCharactersInSet(NSCharacterSet.whiteCharacterSet())
'componentsSeparatedByWhitespace' might be a nice addition as a shortcut for that though?

On Fri, Apr 8, 2016 at 3:38 PM, James Campbell <james@supmenow.com> wrote:
Pehaps we could have a function that splits by whitespace.
"h j c k".split()

___________________________________

James⎥Alex's Minder

james@supmenow.com⎥supmenow.com

Sup

Runway East

10 Finsbury Square

London

EC2A 1AF

On 8 April 2016 at 15:05, Ross O'Brien via swift-evolution <swift-evolution@swift.org> wrote:
Well, you can do this already:let words = "rats live on no evil star".componentsSeparatedByString(" ")so I don't know how much a shortcut is needed.
And given '%w' would currently be impossible in Swift as an operator containing a letter, is there a Swiftier function name or operator for this?
On Fri, Apr 8, 2016 at 2:20 PM, Arsen Gasparyan via swift-evolution <swift-evolution@swift.org> wrote:
Hey guys,
Very often we need to create an array strings. It's a routine task and I really like shortcut in Ruby that shortcut makes everyday coding a little bit easier and fun:
words = %w[rats live on no evil star]
What do you think about adding something like this in Swift?
Cheers,Arsen

_______________________________________________

swift-evolution mailing list

swift-evolution@swift.org

https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________

swift-evolution mailing list

swift-evolution@swift.org

https://lists.swift.org/mailman/listinfo/swift-evolution


(Arsen Gasparyan) #8

I want to find a conciseness and simplicity solution. But
componentsSeparatedByCharactersInSet(NSCharacterSet.whiteCharacterSet()) scares
me :slight_smile:

···

On Fri, Apr 8, 2016 at 7:39 PM James Campbell <james@supmenow.com> wrote:

Exactly

Sent from Supmenow.com <http://supmenow.com>

On Fri, Apr 8, 2016 at 9:38 AM -0700, "Ross O'Brien" < > narrativium+swift@gmail.com> wrote:

A function that splits by whitespace would be this:

"h j c
k".componentsSeparatedByCharactersInSet(NSCharacterSet.whiteCharacterSet())

'componentsSeparatedByWhitespace' might be a nice addition as a shortcut
for that though?

On Fri, Apr 8, 2016 at 3:38 PM, James Campbell <james@supmenow.com> >> wrote:

Pehaps we could have a function that splits by whitespace.

"h j c k".split()

*___________________________________*

*James⎥Alex's Minder*

*james@supmenow.com <james@supmenow.com>⎥supmenow.com
<http://supmenow.com>*

*Sup*

*Runway East *

*10 Finsbury Square*

*London*

* EC2A 1AF *

On 8 April 2016 at 15:05, Ross O'Brien via swift-evolution < >>> swift-evolution@swift.org> wrote:

Well, you can do this already:
let words = "rats live on no evil star".componentsSeparatedByString(" ")
so I don't know how much a shortcut is needed.

And given '%w' would currently be impossible in Swift as an operator
containing a letter, is there a Swiftier function name or operator for this?

On Fri, Apr 8, 2016 at 2:20 PM, Arsen Gasparyan via swift-evolution < >>>> swift-evolution@swift.org> wrote:

Hey guys,

Very often we need to create an array strings. It's a routine task and
I really like shortcut in Ruby that shortcut makes everyday coding a little
bit easier and fun:

   words = %w[rats live on no evil star]

What do you think about adding something like this in Swift?

Cheers,
Arsen

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

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


(Vladimir) #9

As I understand in initial message, string should be splitted by spaces, not all kind of whitespaces(like TAB). whiteCharacterSet() is not just space(at least TAB char there too).
So, if we need split just by space we need some componentsSeparatedBySpaceCharacter(). And IMO this is not a good solution.

Actually "a b c".componentsSeparatedByString(" ") works well, but probaly it will be good to have something like
"a:b-->c/d".componentsSeparatedByStrings(":", "-->", "/"). But I'm not sure about this :slight_smile:

···

On 08.04.2016 19:38, Ross O'Brien via swift-evolution wrote:

A function that splits by whitespace would be this:

"h j c
k".componentsSeparatedByCharactersInSet(NSCharacterSet.whiteCharacterSet())

'componentsSeparatedByWhitespace' might be a nice addition as a shortcut
for that though?


(Radek Pietruszewski) #10

FWIW, in Swift 3:

“h j c k”.componentsSeparated(by: “”)

or

“h j c k”.componentsSeparatedByCharacters(in: .whitespace())

which is slightly better.

Regarding the proposal, I love Ruby’s %w(foo bar) and other nice shortcuts like that, but short-term I think we have bigger problems in Swift.

I would love if we had a macro system that would allow something like this, though.

— Radek

···

On 08 Apr 2016, at 18:38, Ross O'Brien via swift-evolution <swift-evolution@swift.org> wrote:

A function that splits by whitespace would be this:

"h j c k".componentsSeparatedByCharactersInSet(NSCharacterSet.whiteCharacterSet())

'componentsSeparatedByWhitespace' might be a nice addition as a shortcut for that though?

On Fri, Apr 8, 2016 at 3:38 PM, James Campbell <james@supmenow.com <mailto:james@supmenow.com>> wrote:
Pehaps we could have a function that splits by whitespace.

"h j c k".split()

___________________________________

James⎥Alex's Minder

james@supmenow.com <mailto:james@supmenow.com>⎥supmenow.com <http://supmenow.com/>
Sup

Runway East >

10 Finsbury Square

London

> EC2A 1AF

On 8 April 2016 at 15:05, Ross O'Brien via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
Well, you can do this already:
let words = "rats live on no evil star".componentsSeparatedByString(" ")
so I don't know how much a shortcut is needed.

And given '%w' would currently be impossible in Swift as an operator containing a letter, is there a Swiftier function name or operator for this?

On Fri, Apr 8, 2016 at 2:20 PM, Arsen Gasparyan via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
Hey guys,

Very often we need to create an array strings. It's a routine task and I really like shortcut in Ruby that shortcut makes everyday coding a little bit easier and fun:

   words = %w[rats live on no evil star]

What do you think about adding something like this in Swift?

Cheers,
Arsen

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

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

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


(Tino) #11

I would create a computed property on String and call it "words"

Tino