tags:

views:

94

answers:

4

It seems like the support for printing arrays is somewhat lacking in Scala. If you print one, you get the default garbage you'd get in Java:

scala> val array = Array.fill(2,2)(0)             
array: Array[Array[Int]] = Array(Array(0, 0), Array(0, 0))

scala> println(array)
[[I@d2f01d

Furthermore, you cannot use the Java toString/deepToString methods from the java.util.Arrays class: (or at least I cannot figure it out)

scala> println(java.util.Arrays.deepToString(array))
<console>:7: error: type mismatch;
 found   : Array[Array[Int]]
 required: Array[java.lang.Object]
       println(java.util.Arrays.deepToString(array))

The best solution I could find for printing a 2D array is to do the following:

scala> println(array.map(_.mkString(" ")).mkString("\n"))
0 0
0 0

Is there a more idiomatic way of doing this?

+1  A: 

So your issue is that you can't come up with a clean way of doing something as complex as 2D printing in less than 60 characters? Is that what you're saying? Because if that's the case then all of us who went into this gig thirty years ago because we worried about the "software crisis" can just declare victory. Because, well, damn.

Dave Griffith
No, it's not the end of the world by any means. I just wanted to make sure I wasn't missing a better way of doing things. I agree that the one liner is quite powerful and I don't mean to say that I'm taking that power for granted.
I82Much
I have also noted the trend of "is there a cleaner way of"-type Scala questions at SOF. I have no problem with that, but I am a bit confused. Does that mean that everything in Scala actually works, so all we have to consider from this point on is style?
olle kullberg
I think the SO Scala questions reflect (gasp!) what the programmers are dealing with at the moment, not what the Scala developers or the programming language research and development community at large are confronting. If you want the "inside" story, subscribing to the Scala mailing lists is advisable.
Randall Schulz
@olle - in my case I am learning Scala and it's very easy to pick up bad habits when learning a new skill. As such I'm turning to the community to ensure that I'm not missing out on more idiomatic (read: better) ways of expressing myself. If I should be tagging these questions a different way, I'd love to know.
I82Much
@I82Much No, no. Please go on as usual.
olle kullberg
+4  A: 

In Scala 2.8, you can use the deep method defined on Array, that returns an IndexedSeq cointaining all of the (possibly nested) elements of this array, and call mkString on that:


scala> val array = Array.fill(2,2)(0)
array: Array[Array[Int]] = Array(Array(0, 0), Array(0, 0))

scala> println(array.deep.mkString("\n"))
Array(0, 0)
Array(0, 0)

The IndexedSeq returned does have a stringprefix 'Array' by default, so I'm not sure whether this gives precisely what you wanted.

Arjan Blokzijl
+2  A: 

How about this:

scala> val array = Array.fill(2,2)(0)
array: Array[Array[Int]] = Array(Array(0, 0), Array(0, 0))

scala> import scala.runtime.ScalaRunTime._
import scala.runtime.ScalaRunTime._

scala> val str = stringOf(array)
str: String =
Array(Array(0, 0), Array(0, 0))
Eastsun
A: 

The "functional programming" way to do this (as far as I concern) is:

scala> array foreach{case a => a foreach {b => print(b.toString + " ")}; print('\n')}
0 0 
0 0 

Or if you don't really care about the spacing:

scala> array foreach{a => a foreach println}
0
0
0
0

IMHO, functional programming can get a little messy, if it takes too long to make this, I'd say just go with the imperative way.

the_great_monkey