1

In Scala, this is how I sum the Int values of a multidimensional array:

val data = Array.ofDim[Int](200, 200, 200)
// fill data with Int values

val sum = data.map(i => i.map(j => j.sum).sum).sum

Is there a shorter, perhaps easier to read, way of getting the sum of the array?

1
  • 2
    Consider asking on Code Review. That aside, consider a function for flattening the array. Commented Mar 6, 2019 at 17:07

1 Answer 1

3

A good solution would be:

val sum = data.flatten.flatten.sum
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.