So I'm working through a few of the exercises in "Scala for the Impatient" and one of them is:
Write a for
loop for computing the product of the Unicode codes of all letters in a string. For example, the product of the characters in "Hello" is 9415087488 L.
The next problem is to do the same, but without a for
loop - it hints that we should check StringOps
in Scaladoc.
I checked the RichChar
and StringOps
section in Scaladoc, and perhaps I'm misreading or looking in the wrong places, but I can't find anything that gets me to match their output. I've thus far tried:
scala> x.foldLeft(1)(_ * _.toInt)
res0: Int = 825152896
scala> x.foldLeft(1)(_ * _.getNumericValue)
res5: Int = 2518992
scala> x.foldLeft(1)(_ * _.intValue())
res6: Int = 825152896
scala> var x = 1
x: Int = 1
scala> for (c <- "Hello") x *= c.toInt
scala> x
res12: Int = 825152896
Which does not match their output.
How do I do this, in both the for
and non-for
way?
Thanks!