You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My current solution is to copy Scala's mkString but I wonder if there is a better (more general and performant) way:
extensionCollection{func makeString(_ separator:String)->String{makeString("", separator,"")}func makeString(_ prefix:String, _ separator:String, _ suffix:String)->String{
if isEmpty {return prefix + suffix
}else{returnaddString("", prefix, separator, suffix)}}privatefunc addString(_ string:String, _ prefix:String, _ separator:String, _ suffix:String)->String{varstring= string
if prefix.count !=0{ string.append(contentsOf: prefix)}varit=makeIterator()
if let start = it.next(){
string.append(contentsOf:"\(start)")
while let rest = it.next(){
string.append(contentsOf: separator)
string.append(contentsOf:"\(rest)")}}
if suffix.count !=0{ string.append(contentsOf: suffix)}return string
}}
//This allows for something close to what is possible in Scala
func prettyString(_ sudoku:[[Int]])->String{
sudoku.chunks(ofCount:3).map{ bigChunck in
bigChunck.map{ row in
row.chunks(ofCount:3).map{ smallChunk in
smallChunk.makeString("","","")}.makeString("|","|","|")}.makeString("\n")}.makeString("+-------+-------+-------+\n","\n+-------+-------+-------+\n","\n+-------+-------+-------+")}
The text was updated successfully, but these errors were encountered:
Joining with a prefix and suffix is a relatively common operation that
isn't terribly simple to implement concisely. This adds that operation
for joining strings.
Related to #195.
Motivation:
I wanted to pretty print a 2D array (a Sudoku board) and found mapping to string and using
joined(by:)
fell short of what I needed.My current solution is to copy Scala's
mkString
but I wonder if there is a better (more general and performant) way:The text was updated successfully, but these errors were encountered: