Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
1
Task/Reverse-a-string/Scala/reverse-a-string-1.scala
Normal file
1
Task/Reverse-a-string/Scala/reverse-a-string-1.scala
Normal file
|
|
@ -0,0 +1 @@
|
|||
"asdf".reverse
|
||||
1
Task/Reverse-a-string/Scala/reverse-a-string-2.scala
Normal file
1
Task/Reverse-a-string/Scala/reverse-a-string-2.scala
Normal file
|
|
@ -0,0 +1 @@
|
|||
"asdf".foldRight("")((a,b) => b+a)
|
||||
11
Task/Reverse-a-string/Scala/reverse-a-string-3.scala
Normal file
11
Task/Reverse-a-string/Scala/reverse-a-string-3.scala
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def reverse(s: String) = {
|
||||
import java.text.{Normalizer,BreakIterator}
|
||||
val norm = Normalizer.normalize(s, Normalizer.Form.NFKC) // waffle -> waffle (optional)
|
||||
val it = BreakIterator.getCharacterInstance
|
||||
it setText norm
|
||||
def break(it: BreakIterator, prev: Int, result: List[String] = Nil): List[String] = it.next match {
|
||||
case BreakIterator.DONE => result
|
||||
case cur => break(it, cur, norm.substring(prev, cur) :: result)
|
||||
}
|
||||
break(it, it.first).mkString
|
||||
}
|
||||
30
Task/Reverse-a-string/Scala/reverse-a-string-4.scala
Normal file
30
Task/Reverse-a-string/Scala/reverse-a-string-4.scala
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
def reverseString(s: String) = {
|
||||
import java.lang.Character._
|
||||
|
||||
val combiningTypes = List(NON_SPACING_MARK, ENCLOSING_MARK, COMBINING_SPACING_MARK)
|
||||
def isCombiningCharacter(c: Char) = combiningTypes contains c.getType
|
||||
def isCombiningSurrogate(high: Char, low: Char) = combiningTypes contains getType(toCodePoint(high, low))
|
||||
def isCombining(l: List[Char]) = l match {
|
||||
case List(a, b) => isCombiningSurrogate(a, b)
|
||||
case List(a) => isCombiningCharacter(a)
|
||||
case Nil => true
|
||||
case _ => throw new IllegalArgumentException("isCombining expects a list of up to two characters")
|
||||
}
|
||||
|
||||
def cleanSurrogate(l: List[Char]) = l match {
|
||||
case List(a, b) if a.isHighSurrogate && b.isLowSurrogate => l
|
||||
case List(a, b) if a.isLowSurrogate => Nil
|
||||
case List(a, b) => List(a)
|
||||
case _ => throw new IllegalArgumentException("cleanSurrogate expects lists of two characters, exactly")
|
||||
}
|
||||
|
||||
def splitString(string: String) = (string+" ").iterator sliding 2 map (_.toList) map cleanSurrogate toList
|
||||
|
||||
def recurse(fwd: List[List[Char]], rev: List[Char]): String = fwd match {
|
||||
case Nil => rev.mkString
|
||||
case c :: rest =>
|
||||
val (combining, remaining) = rest span isCombining
|
||||
recurse(remaining, c ::: combining.foldLeft(List[Char]())(_ ::: _) ::: rev)
|
||||
}
|
||||
recurse(splitString(s), Nil)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue