How to apply a function of all elements from n array?

I am new to Scala and would like to apply a (let math.log

) function to all elements of the [Double] array. Th foreach

doesn't work. What's the best way to do this?

Here is my code:

def func(arr: Array[Double]): Double = {

arr.map(a => log(a)).sum
}

      

+3


source to share


1 answer


scala> Array(1.1, 4.4, 9.9).map(math.log(_))
res21: Array[Double] = Array(0.09531017980432493, 1.4816045409242156, 2.2925347571405443)

      



+6


source







All Articles