Skip to content

Instantly share code, notes, and snippets.

@leodabus
Created January 24, 2017 03:50
Show Gist options
  • Save leodabus/6ef6dba41c461c69dd12159e2a519d4c to your computer and use it in GitHub Desktop.
Save leodabus/6ef6dba41c461c69dd12159e2a519d4c to your computer and use it in GitHub Desktop.
extension BidirectionalCollection where Iterator.Element == String, SubSequence.Iterator.Element == String {
var sentence: String {
guard let last = last else { return "" }
return count <= 2 ? joined(separator: " and ") :
dropLast().joined(separator: ", ") + " and " + last
}
}
@leodabus
Copy link
Author

let elements = ["a", "b", "c", "d", "e"]

let sentenceFromElements = elements.sentence // "a, b, c, d and e"

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