I'm a newbie in Scala and would like to apply a function (let's say math.log
) on all elements of an Array[Double]. Th foreach
doesn't work. What is the best way to do that?
Here is my code:
def func(arr: Array[Double]): Double = {
arr.map(a => log(a)).sum
}
scala> Array(1.1, 4.4, 9.9).map(math.log(_))
res21: Array[Double] = Array(0.09531017980432493, 1.4816045409242156, 2.2925347571405443)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With