模式匹配和递归问题

时间:2016-09-19 02:08:28

标签: scala recursion pattern-matching

以下是我的ADT。需要注意的主要事项是可以嵌套块(查看children属性。

trait Cda {
  def format: String = this match {
    case f: Field => f.value
    case Block(fields, children) => fields.map(f => f.format).mkString("|") + "|" + children.map(b => b.format).mkString("|")
    case Record(keys, blocks) => blocks.map(b => b.format).mkString("|")
  }
}

trait Field extends Cda {
  val name: String
  val value: String
}

case class StringField(name: String, value: String) extends Field
case class DateField(name: String, value: String) extends Field
case class TimeField(name: String, value: String) extends Field
case class MatchKey(keyFields: Seq[Field]) extends Cda
case class Block(fields: Seq[Field], children: Seq[Block] = Seq()) extends Cda
case class Record(key: MatchKey, blocks: Seq[Block]) extends Cda

以下是该ADT的示例实例化

//Block - AI
val aiBlockId = StringField("blockId", "AI")
val addlFieldPos = StringField("AdditionalFieldPosition", "addlFieldPos")
val addlFieldName = StringField("AdditionalFieldName", "addlFieldName")
val AI = Block(Seq(aiBlockId, addlFieldPos, addlFieldName))

//Block - RPS
val rpsBlockId = StringField("blockId", "RPS")
val dateOfStatus = DateField("DateOfStatus", "19240811")
val timeOfStatus = TimeField("TimeOfStatus", "023829")

val rpsBlocks = Seq(rpsBlockId, dateOfStatus, timeOfStatus)

val rpsNestedBlocks = Seq(AI)

val RPS = Block(rpsBlocks, rpsNestedBlocks)

我希望格式化返回RPS|19240811|023829|AI|addlFieldPos|addlFieldName,但我最后会收到一个额外的管道 | RPS|19240811|023829|AI|addlFieldPos|addlFieldName|

如何更改递归函数format(具体为case Block(fields,children))以更正此问题?

2 个答案:

答案 0 :(得分:5)

首先结合seqs。使用迭代器会更便宜,因为迭代器不会创建中间集合。

scala> val as = Seq(1,2,3) ; val bs = Seq.empty[Int]
as: Seq[Int] = List(1, 2, 3)
bs: Seq[Int] = List()

scala> (as ++ bs).mkString("|")
res0: String = 1|2|3

scala> (as.iterator ++ bs).mkString("|")
res1: String = 1|2|3

即,

case Block(fields, children) => (fields.iterator ++ children).map(_.format).mkString("|")

答案 1 :(得分:3)

trait Cda {
  def format: String = this match {
    case f: Field => f.value
    case Block(fields, children) => fields.map(f => f.format).mkString("|") + {if (!children.isEmpty) {"|" + children.map(b => b.format).mkString("|")} else ""}
    case Record(keys, blocks) => blocks.map(b => b.format).mkString("|")
  }
}