Note that there are some explanatory texts on larger screens.

plurals
  1. POArrays in recursive print method
    primarykey
    data
    text
    <p>I'm trying to make a <code>println</code> replacement that outputs nested collections in a more readable format. This is best illustrated with an example: I'd like <code>List(Set(Vector(1.0,1.1), Vector(0d)), Set(Vector("a", "b", "c"), Vector("x", "y")))</code> to be printed as</p> <pre><code>List Set Vector(1.0, 1.1) Vector(0.0) Set Vector(a, b, c) Vector(x, y) </code></pre> <p>This would be a lot easier without type erasure, but I've come up with</p> <pre><code>def rprint(a: Any, indent: Int = 0): Unit = a match { case x: Traversable[_] =&gt; if (x.isEmpty) rprint(x.toString, indent) else x.head match { case y: Traversable[_] =&gt; { rprint(x.toString.takeWhile(_ != '('), indent) x foreach {i =&gt; rprint(i, indent + 2)} } case y =&gt; rprint(x.toString, indent) } case x =&gt; println(" " * indent + x) } </code></pre> <p>I'm struggling with getting this to work nicely with Arrays, without substantial code duplication. I'd like them to work the same as for other collections. Specifically:</p> <ul> <li><p>Arrays are not <code>Traversable</code></p></li> <li><p>could convert Arrays using <code>genericArrayOps</code> to ArrayOps which is <code>TraversableOnce</code>, but <code>TraversableOnce</code> doesn't have a <code>head</code> method, so I can't see how to get an element to check its type</p></li> <li><p><code>toString</code> doesn't work quite like other collections (use <code>.deep</code>)</p></li> </ul> <p>What's the best way to incorporate Arrays into this method, or is there a different approach that would work better?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload