递归打印方法中的数组
我正在尝试进行 println
替换,以更易读的格式输出嵌套集合。这最好用一个例子来说明:我想要 List(Set(Vector(1.0,1.1), Vector(0d)), Set(Vector("a", "b", "c"), Vector ("x", "y")))
打印为
List
Set
Vector(1.0, 1.1)
Vector(0.0)
Set
Vector(a, b, c)
Vector(x, y)
如果没有类型擦除,这会容易得多,但我想出了
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)
}
我正在努力让它与数组很好地配合工作,而不需要大量代码重复。我希望它们能像其他集合一样工作。具体来说:
数组不是
可遍历
可以使用
genericArrayOps
将数组转换为ArrayOps,即TraversableOnce
,但是TraversableOnce
没有head
方法,所以我看不到如何获取元素检查其类型toString
不像其他集合那样工作(使用.deep
)
将数组合并到其中的最佳方法是什么这种方法,还是有其他更好的方法?
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 isTraversableOnce
, butTraversableOnce
doesn't have ahead
method, so I can't see how to get an element to check its typetoString
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了使其更加灵活,我定义了一个特征,其中包括实用方法和一个抽象方法,以便可以像
Array(1,2).print
一样使用它:然后使用隐式方法来实现它看起来
str
和print
是Any
的方法:这可以处理任意深度嵌套,但不会对不支持的集合使用多行包含集合 - 例如将打印:
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
:Then an implicit to make it look like
str
andprint
are methods ofAny
:This handles arbitrary deep nesting, but won't use multiple lines for collections that don't contain collections - for instance that would print:
不要转换为 ArrayOps,而是尝试 WrappedArray,它有一个 head 方法并且是可遍历的。
Instead of converting to an ArrayOps, try a WrappedArray, which does have a head method and is traversable.
尝试在匹配短语中使用更通用的类型,例如
GenTraversable
来实现隐式转换。您可以为特定缺失类型(例如元组)添加更多案例
我无法弄清楚打印某些类型名称(例如
List
和Array
)有什么问题 - 请参阅示例在函数定义之后)。a.getClass.getSimpleName
和a.ToString
都返回有线字符串我还尝试连接类型
Array
和GenTraversable - 因为存在从 Array 到 WrapperArray <: GenTraversable 的隐式转换。
下面的解决方案不满足,因为它打印“WrappedArray”而不是“Array”,
当我们使用
Array
实例调用函数时编辑:
我已将 GenTraversable 的名称提取器更改为
a.stringPrefix
Try tu use more general types in match phrase, like
GenTraversable
to witch exists implicit conversion.You can add more cases for specific missing types (like tuples)
I can't figure what is the problem with printing some type names (like
List
andArray
- see the example after the function definition). Botha.getClass.getSimpleName
anda.ToString
returns wired stringI've also try to connect type
Array
andGenTraversable
- because there is implicit conversion fromArray
toWrapperArray <: GenTraversable
.The solution below doesn't satisfy as it prints "WrappedArray" instead "Array",
when we call the function with
Array
instanceEdit:
I've changed name extractor for GenTraversable to
a.stringPrefix
我无意回答自己的问题,但我有一些有用的东西。我认为它可以改进很多,所以感谢建议。那里仍然有重复和相当丑陋的演员阵容。
测试:
It wasn't my intention to answer my own question, but I have something that works. I think it can be improved a lot, so suggestions are appreciated. There is still repetition and a rather ugly cast in there.
test: