[swift-evolution] `once` keyword for use with loops

Joe Groff jgroff at apple.com
Tue May 17 10:44:37 CDT 2016


> On May 16, 2016, at 11:03 PM, Nicholas Maccharoli via swift-evolution <swift-evolution at swift.org> wrote:
> 
> ​Hello Swift Evolution,
> 
> Its not uncommon to have to do a piece of work only once or on the first iteration of 
> a loop.
> Take for example producing a comma separated string from an Array:
> 
>     var result = ""
> 
>     let values = [1, 2, 3, 4, 5]
> 
>     var gen = values.generate()
> 
> 
> 
>     if let first = gen.next() {
> 
>         result += "\(first)"
> 
>         while let value = gen.next() {
> 
> 
>             result += ", "
> 
>             result += "\(value)"
> 
>         }
> 
>     }
> 
> 
> 
> Since on the first iteration we want to skip putting a comma in front we use an `if let` to grab the first element and then embed a `while let` inside the `if let` to handle the rest.
> 
> 
> 
> Another way to do this could be using a bool to keep track of the first iteration:
> 
> 
> 
>     var first = true
> 
>     while let value = gen.next() {
> 
>         if first {
> 
>             result += "\(value)"
> 
>             first = false
> 
>             continue
> 
>         } else {
> 
>             result += ", "
> 
>             result += "\(value)"
> 
>         }
> 
> 
>     }
> 
> 
> 
> These approaches work, but I think there may be a way to do this with less noise.
> 
> If there was a keyword to execute a block of code only on the first iteration of a loop I think that would make code like this more concise.
> 
> If there was a keyword like `once` then the same thing could be achieved with something like:
> 
> 
> 
>     while let value = gen.next() {
> 
>         once {
> 
>             result += "\(value)"
> 
>             continue
> 
>         }
> 
>         result += ", "
> 
>         result += "\(value)"
> 
> 
>     }
> 
> 
> 
> How does it sound?

IMO, a better approach would be to write an 'interleave' combinator, like this:

func interleave<S: Sequence>(_ x: S, between: () -> (), each: S.GeneratorType.Element -> ()) {
  var generator = x.generate()
  if let first = generator.next() {
    each(first)
    while let rest = generator.next() {
      between()
      each(rest)
    }
  }
}

-Joe

> 
> 
> 
> - Nick
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list