[swift-evolution] Proposal: Add .times method to Integer type
肇鑫
owenzx at gmail.com
Sat Jan 9 09:57:30 CST 2016
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 at 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 at 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 at 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 at 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 at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
--
Owen Zhao
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160109/e7a9615b/attachment.html>
More information about the swift-evolution
mailing list