​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?
- Nick
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution