【发布时间】:2019-03-19 14:24:40
【问题描述】:
我需要使用纯函数式编程比较两个ArrayBuffer[Array[String]] 类型的集合。
例如
ArrayBuffer(Array("str1"), Array("str2"), Array("str3"))
// compare with
ArrayBuffer(Array("str1"), Array("str2"), Array("str3"))
我有ArrayBuffer[String]的解决方案:
def sameAs[A](c: Traversable[A], d: Traversable[A]): Boolean =
if (c.isEmpty) d.isEmpty
else {
val (e, f) = d span (c.head !=)
if (f.isEmpty) false else sameAs(c.tail, e ++ f.tail)
}
【问题讨论】:
-
用 ==(或 !=)比较数组没有多大意义,因为 Array("a") == Array("a") 是
false。
标签: scala functional-programming arraybuffer