implementation of atoi in scala I've implemented atoi function as described in leetcode problem string-to-integer-atoi.

I am looking for some feedback on writing this in a more scala native way and being able to include the requirement that:


  If the numerical value is out of the range of representable values,
  INT_MAX (231 − 1) or INT_MIN (−231) is returned.


  object Solution {
    def myAtoi(str: String): Int = {

      val charMap = Map('1'-&gt;1, '2'-&gt;2, '3'-&gt;3, '4'-&gt;4, '5'-&gt;5, '6'-&gt;6, '7'-&gt;7, '8'-&gt;8, '9'-&gt;9, '0'-&gt; 0)

      val keySet = charMap.keySet
      var num = 0
      var firstChar = -1
      var negFlag = false
      var zeroFlag = false

      var cleanStr = str.reverse.dropWhile(c =&gt; !charMap.contains(c) || c == '-' || c == ' ').trim

      for((c, i) &lt;- cleanStr.view.zipWithIndex) {
        var summand = c match {
          case c if keySet.contains(c) =&gt; {
            if(firstChar == -1) firstChar = i
            charMap(c) * math.pow(10, i - firstChar).toInt
          }
          case '-' if(firstChar != -1) =&gt;  {
            negFlag = true
            0
          }
          case _ =&gt; {
            zeroFlag = true
            0
          }
        }
        num += summand
      }
      if(zeroFlag) 0 else if(negFlag) num * -1 else num
    }
  }
A few observations about your code.

incomplete - As you've noted, it doesn't handle Int overflow. It also doesn't handle the optional leading + character.

digit characters - The variables charMap and keyMap are unnecessary. The .isDigit and .asDigit methods in the standard library provide the same functionality.

Boolean logic - Consider this Boolean test: !charMap.contains(c) || c == '-' || c == ' ' It translates to "if c is not a digit OR if c is a dash OR if c is a space." But if c is a dash or space then it is already not a digit. The dash and space tests serve no purpose because no dash or space character would get beyond the digit test.

unneeded vars - There's no reason to make cleanStr or summand a var. Their initial values are never changed. In fact, you could just drop summand altogether and increment num directly.

num += (c match { ...//rest of code block


excess braces - In a match{...} every case statement ends the previous code block and starts a new one. Therefore wrapping the code block in { braces } is redundant and just adds visual clutter. In other words, this...

case x =&gt;
  //code
  //here


...is cleaner than this...

case x =&gt; {
  //code
  //here
}


style - The use of so much mutation (i.e. vars instead of vals) makes it pretty clear that you are using the Scala language to write C code. This will change as you become more familiar with the Scala standard library, the tenants of Functional Programming, and the study of other Scala code.

Here, for example, is a Scala solution for the atoi challenge.

val numRE = "\\s*([+-]?)0*(\\d+).*".r

def myAtoi(str :String) :Int = str match {
  case numRE(signStr, numStr) =&gt;
    val sign    = if (Option(signStr).contains("-")) -1 else 1
    val bailOut = if (sign &lt; 0) Int.MinValue else Int.MaxValue
    if (numStr.length &gt; 10) bailOut                      //too many digits
    else if (numStr.length &lt; 10 || numStr &lt; "2147483648")
      sign * numStr.foldLeft(0)(_*10 + _.asDigit)        //calculate return Int
    else bailOut
  case _ =&gt; 0  //input doesn't match the pattern
}