You are viewing a single comment's thread. Return to all comments →
In Scala w/ side-effecting foldLeft and if-elses. If there's anything offensive from an FP perspective, please comment.
foldLeft
if
else
object Solution extends App { private val str = readLine() private val firstChar = str.head print(firstChar) private val lastCnt = (1 until str.size).foldLeft((firstChar, 1)) { case ((lastChar, cnt), i) => val c = str(i) if (c == lastChar) (c, cnt + 1) else { if (cnt != 1) print(cnt) print(c) (c, 1) } }._2 if (lastCnt != 1) print(lastCnt) }
Seems like cookies are disabled on this browser, please enable them to open this website
String Compression
You are viewing a single comment's thread. Return to all comments →
In Scala w/ side-effecting
foldLeft
andif
-else
s. If there's anything offensive from an FP perspective, please comment.