如何在Scala中覆盖def toString以打印List [List [Int]?

时间:2015-11-27 10:45:06

标签: scala functional-programming tail-recursion

我正在尝试编写一个函数来覆盖Scala中的toString函数。我有一个列表(List[List[Int]]),我正在尝试打印。 List[List[Int]]代表数独网格。每个List[Int]代表一个特定的行。覆盖功能应该打印网格,替换' 0' 0用' _'。我希望以递归方式实现所有内容。以下是我的代码:

override def toString() = {
def innerFn(li: List[Int], s:String): String = {
  if (li.tail.isEmpty) s+"\n"
  else {
    if (li.head == 0) innerFn(li.tail,s+"_ ")
    else innerFn(li.tail,s+li.head+" ")
  }
}

def outerFn(li: List[List[Int]], s: String): String = {
  if (li.tail.isEmpty) s
  else {
    if (li.tail.head.isEmpty) s
    else innerFn(li.head,s)
  }
}
outerFn(grid,"")
}

我相信我的" innerFn()"正在工作正常,因为它正在我需要它打印List [Int]。但是,由于某种原因,只打印第一行(或List[List[Int]]的第一个元素)。我看不出我错过了什么。

2 个答案:

答案 0 :(得分:2)

在scala中,我们创建了包装器来覆盖特定类型混合的基本方法:

sed -n "s/['\\\"]index\.html['\\\"]/'\/'/p" index.html

现在您可以验证

object Sudoku {
  implicit class GroupStr[X](xs: Seq[X]) {
    def groupStr(sep: String): String =
      xs.grouped(3).map(
        _.mkString(sep, sep, sep)
      ).mkString
  }
}

case class Sudoku(grid: List[List[Int]]) {
  import Sudoku._
  def lineSep = ("-" * 9).toList.groupStr("+") + "\n"
  override def toString = grid.map(_.groupStr("|") + "\n").groupStr(lineSep)
}

打印

println(Sudoku(List.range(0, 9).map(
    i => List.range(0, 9).map( j => (i + j) % 9 + 1))))

另一种方法是使用类似scalaz.Show的类型类,另见here

答案 1 :(得分:1)

递归表格

  val grid: List[List[Int]] = List(List(1, 2, 3), List(2, 3, 4))

  def toString() = {
    def innerFn(li: List[Int]): String = {
      li match {
        case x :: xs => li.mkString(",")
        case Nil     => ""
      }
    }

    def outerFn(li: List[List[Int]]): String = {
      li match {
        case x :: xs if (!xs.isEmpty) => innerFn(x) + "\n" + outerFn(xs)
        case x :: Nil                 => innerFn(x)
        case Nil                      => ""
      }
    }

    outerFn(grid)
  }