Skip to content

Instantly share code, notes, and snippets.

@munirwanis
Created December 9, 2017 18:50
Show Gist options
  • Save munirwanis/7c578075853d432868f764d0047b879c to your computer and use it in GitHub Desktop.
Save munirwanis/7c578075853d432868f764d0047b879c to your computer and use it in GitHub Desktop.
Reduce String array to a single String separated by new lines.
extension Sequence where Iterator.Element == String {
var reduced: String {
return self.reduce("", { $0 == "" ? $1 : $0 + "\n" + $1 })
}
}
@munirwanis
Copy link
Author

Example:

let sequence = ["one", "two", "three"]

print(sequence.reduced)

// PRINTS:
// one
// two
// three

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment