Proposal: Add .times method to Integer type


(Howard Lovatt) #1

+1 to 5.times{stuff} from me. I much prefer library functions to language
extensions.

  -- Howard.


(TJ Usiyan) #2

-1 from me. It is simple enough for an extension and provides little
benefit as a standard library inclusion.

···

On Sun, Dec 20, 2015 at 9:10 PM, Howard Lovatt via swift-evolution < swift-evolution@swift.org> wrote:

+1 to 5.times{stuff} from me. I much prefer library functions to language
extensions.

  -- Howard.

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


#3

Just another -1. While I appreciated it in Ruby, I don't think it belongs in the Swift standard library at this time. If closures could be annotated as break/continue-able, and if the standard library started grew a lot—as Ruby's did—then maybe (I'd prefer the former and hold off on the latter, though).

In the meantime, "repeat 5 { /* do something */ }" seems better to me.

Stephen

···

On Dec 20, 2015, at 9:32 PM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org> wrote:

-1 from me. It is simple enough for an extension and provides little benefit as a standard library inclusion.

On Sun, Dec 20, 2015 at 9:10 PM, Howard Lovatt via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
+1 to 5.times{stuff} from me. I much prefer library functions to language extensions.

  -- Howard.

_______________________________________________
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


(Cihat Gündüz) #4

Just an update to my thoughts about the times method in the meantime:

Currently I’m using my HandySwift <https://github.com/Flinesoft/HandySwift> framework to include a `.times` method for my own usage and I’m happy that way for now – although that solution doesn’t allow for control flow statements. I still think it is a useful feature to be added to the language but I also agree with Chris that `repeat` should be in the name somewhere, so speaking about the syntax the following would be my new proposal:

`repeat 5 times { /* code */ }`

Also somebody here asked for use cases and here’s one from the HandySwift framework where I implemented it itself (source <https://github.com/Flinesoft/HandySwift/blob/develop/Sources/Extensions/ArrayExtension.swift>):

public extension Array {
    public var sample: Element? {
        get {
            if self.count > 0 {
                let randomIndex = self.startIndex.advancedBy(Int(randomBelow: self.count))
                return self[randomIndex]
            }
            return nil
        }
    }
    
    public func sample(size size: Int) -> [Element]? {
        if self.count > 0 {
            var sampleElements: [Element] = []
            size.times { // <== <== <== <== <== times method used here <== <== <== <== <==
                sampleElements.append(self.sample!)
            }
            return sampleElements
        }
        return nil
    }
}

···

Am 21.12.2015 um 17:00 schrieb Stephen Celis via swift-evolution <swift-evolution@swift.org>:

Just another -1. While I appreciated it in Ruby, I don't think it belongs in the Swift standard library at this time. If closures could be annotated as break/continue-able, and if the standard library started grew a lot—as Ruby's did—then maybe (I'd prefer the former and hold off on the latter, though).

In the meantime, "repeat 5 { /* do something */ }" seems better to me.

Stephen

On Dec 20, 2015, at 9:32 PM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

-1 from me. It is simple enough for an extension and provides little benefit as a standard library inclusion.

On Sun, Dec 20, 2015 at 9:10 PM, Howard Lovatt via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
+1 to 5.times{stuff} from me. I much prefer library functions to language extensions.

  -- Howard.

_______________________________________________
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


(Angelo Villegas) #5

-1 for me. I don't think it's in the direction of Swift, it also gives little benefit to the standard library, I don't think I would have used it. In terms of verbosity, "repeat 5" is more readable to me than "someArray.times"

···

_____________________________
From: Cihat Gündüz <swift-evolution@swift.org>
Sent: Saturday, January 9, 2016 8:53 PM
Subject: Re: [swift-evolution] Proposal: Add .times method to Integer type
To: Stephen Celis <stephen.celis@gmail.com>
Cc: Davide De Franceschi via swift-evolution <swift-evolution@swift.org>

       Just an update to my thoughts about the times method in the meantime:
       Currently I’m using my HandySwift framework to include a `.times` method for my own usage and I’m happy that way for now – although that solution doesn’t allow for control flow statements. I still think it is a useful feature to be added to the language but I also agree with Chris that `repeat` should be in the name somewhere, so speaking about the syntax the following would be my new proposal:
       `repeat 5 times { /* code */ }`
       Also somebody here asked for use cases and here’s one from the HandySwift framework where I implemented it itself ( source):
           public extension Array { public var sample: Element? { get { if self. count > 0 { let randomIndex = self.startIndex.advancedBy( Int(randomBelow: self.count)) return self[randomIndex] } return nil } }

        public func sample(size size: Int) -> [ Element]? { if self. count > 0 { var sampleElements: [ Element] = [] size.times { // <== <== <== <== <== times method used here <== <== <== <== <== sampleElements.append( self.sample!) } return sampleElements } return nil } }
                  Am 21.12.2015 um 17:00 schrieb Stephen Celis via swift-evolution < swift-evolution@swift.org>:
                                Just another -1. While I appreciated it in Ruby, I don't think it belongs in the Swift standard library at this time. If closures could be annotated as break/continue-able, and if the standard library started grew a lot—as Ruby's did—then maybe (I'd prefer the former and hold off on the latter, though).
                      In the meantime, "repeat 5 { /* do something */ }" seems better to me.
                      Stephen
                                  On Dec 20, 2015, at 9:32 PM, T.J. Usiyan via swift-evolution < swift-evolution@swift.org> wrote:
                              -1 from me. It is simple enough for an extension and provides little benefit as a standard library inclusion.
                       On Sun, Dec 20, 2015 at 9:10 PM, Howard Lovatt via swift-evolution <swift-evolution@swift.org> wrote:
                                       +1 to 5.times{stuff} from me. I much prefer library functions to language extensions.
                             
                                            -- Howard.
                                                        
_______________________________________________
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


(Zhao Xin) #6

I think this is easy to implement yourself if you want that.

extension Int {
    func times(@autoclosure closure: ()->()) {
        for _ in 1 ... self {
            closure()
        }
    }
}

let a = 5
5.times(print("good luck!"))

zhaoxin

···

On Sat, Jan 9, 2016 at 8:52 PM, Cihat Gündüz <swift-evolution@swift.org> wrote:

Just an update to my thoughts about the times method in the meantime:

Currently I’m using my HandySwift
<https://github.com/Flinesoft/HandySwift> framework to include a `.times`
method for my own usage and I’m happy that way for now – although that
solution doesn’t allow for control flow statements. I still think it is a
useful feature to be added to the language but I also agree with Chris that
`repeat` should be in the name somewhere, so speaking about the syntax the
following would be *my new proposal*:

`repeat 5 times { /* code */ }`

Also somebody here asked for *use cases* and here’s one from the
HandySwift framework where I implemented it itself (source
<https://github.com/Flinesoft/HandySwift/blob/develop/Sources/Extensions/ArrayExtension.swift>
):

public extension Array {
    public var sample: Element? {
        get {
            if self.count > 0 {
                let randomIndex = self.startIndex.advancedBy(Int(randomBelow:
self.count))
                return self[randomIndex]
            }
            return nil
        }
    }

    public func sample(size size: Int) -> [Element]? {
        if self.count > 0 {
            var sampleElements: [Element] = []
            size.times { // <== <== <== <== <== times method used here <==
<== <== <== <==
                sampleElements.append(self.sample!)
            }
            return sampleElements
        }
        return nil
    }
}

Am 21.12.2015 um 17:00 schrieb Stephen Celis via swift-evolution < > swift-evolution@swift.org>:

Just another -1. While I appreciated it in Ruby, I don't think it belongs
in the Swift standard library at this time. If closures could be annotated
as break/continue-able, and if the standard library started grew a lot—as
Ruby's did—then maybe (I'd prefer the former and hold off on the latter,
though).

In the meantime, "repeat 5 { /* do something */ }" seems better to me.

Stephen

On Dec 20, 2015, at 9:32 PM, T.J. Usiyan via swift-evolution < > swift-evolution@swift.org> wrote:

-1 from me. It is simple enough for an extension and provides little
benefit as a standard library inclusion.

On Sun, Dec 20, 2015 at 9:10 PM, Howard Lovatt via swift-evolution < > swift-evolution@swift.org> wrote:

+1 to 5.times{stuff} from me. I much prefer library functions to language
extensions.

  -- Howard.

_______________________________________________
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

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

--

Owen Zhao