broken_atoi.scala

#235
Raw
Author
Anonymous
Created
Oct. 27, 2020, 4:54 a.m.
Expires
Never
Size
458 bytes
Hits
177
Syntax
Scala
object Solution {
    def myAtoi(s: String): Int = {
        // well, ignores invalid text, instead of returing 0.
        s.foldRight((0, 1))((v, acc) =>
                           v match {
                               case '-' => (acc._1* -1,acc._2)
                               case v if '0' to '9' contains v => (acc._1+(v-'0')*acc._2, acc._2*10)
                               case _ => acc
                           })._1
    }
}