Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6836393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:21:01+00:00 2026-05-26T23:21:01+00:00

I’m trying to make a println replacement that outputs nested collections in a more

  • 0

I’m trying to make a println replacement that outputs nested collections in a more readable format. This is best illustrated with an example: I’d like List(Set(Vector(1.0,1.1), Vector(0d)), Set(Vector("a", "b", "c"), Vector("x", "y"))) to be printed as

List
  Set
    Vector(1.0, 1.1)
    Vector(0.0)
  Set
    Vector(a, b, c)
    Vector(x, y)

This would be a lot easier without type erasure, but I’ve come up with

def rprint(a: Any, indent: Int = 0): Unit = a match {
  case x: Traversable[_] =>
    if (x.isEmpty)
      rprint(x.toString, indent)
    else x.head match {
      case y: Traversable[_] => {
        rprint(x.toString.takeWhile(_ != '('), indent)
        x foreach {i => rprint(i, indent + 2)}
      }
      case y => rprint(x.toString, indent)
    }
  case x => println(" " * indent + x)
}

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:

  • Arrays are not Traversable

  • could convert Arrays using genericArrayOps to ArrayOps which is TraversableOnce, but TraversableOnce doesn’t have a head method, so I can’t see how to get an element to check its type

  • toString doesn’t work quite like other collections (use .deep)

What’s the best way to incorporate Arrays into this method, or is there a different approach that would work better?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:21:01+00:00Added an answer on May 26, 2026 at 11:21 pm

    To make it more flexible I’m defining a trait that includes utility methods and one abstract method, so that it can be used like that Array(1,2).print:

    trait Printable[T] {
      def str(indent: Int = 0): String
      def str: String = str(0)
      def print(indent: Int = 0): Unit = println(str(indent))
      def print: Unit = print(0)
    }
    

    Then an implicit to make it look like str and print are methods of Any:

    implicit def any2Printable(a: Any): Printable[Any] = new Printable[Any] {
      import collection._
      def name = a match {
        case a: Array[_] => "Array"
        case g: GenTraversableLike[_, _] => g.stringPrefix 
        case i: Iterator[_] => "Iterator"
        case _ => ""
      }
      object Iter {
        def unapply(a: Any): Option[GenIterable[_]] = a match {
          case a: Array[_] => Some(a.toIterable)
          case t: GenTraversableOnce[_] => Some(t.toIterable)
          case _ => None
        }
      }
      object Nested {
        def unapply(i: GenIterable[_]): Option[GenIterable[_]] = i match {
          case nested if i.exists{case Iter(j) =>true case _ =>false} => Some(nested)
          case _ => None
        }
      }
      def str(indent: Int = 0) = " " * indent + (a match {
        case Iter(i) if i.isEmpty => name + " <empty>"
        case Iter(Nested(i)) => name + "\n" + i.map(_.str(indent+2)).mkString("\n")
        case Iter(i) => name + i.map(_.toString).mkString("(", ", ", ")")
        case _ => a.toString
      })
    }
    

    This handles arbitrary deep nesting, but won’t use multiple lines for collections that don’t contain collections – for instance that would print:

    Array
      Set
        Array(1.0, 1.1)
        Vector <empty>
        Array <empty>
        Set
          Vector(a, b, c)
          Vector(x, y)
          List
            Set
              Array(1.0, 1.1)
              Vector(0.0)
            Set
              Vector(a, b, c)
              Vector(x, y)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.