用自然语言(Scala)枚举列表的最佳方法是什么?

发布于 2024-12-11 17:57:07 字数 417 浏览 0 评论 0原文

List 对象具有 mkString 方法,可以将其转换为带有分隔符的字符串。然而,大多数人类语言在枚举列表时对最后一个元素的处理方式有所不同。例如 A、B、C 和 D。就

代码大小和合理效率而言,完成此任务的最佳方案是什么?准确地说,我正在寻找一个满足以下条件的函数:

assertEquals("",foo(List()))
assertEquals("A",foo(List("A")))
assertEquals("A and B",foo("List("A","B")))
assertEquals("A, B and C", foo(List("A","B","C")))
assertEquals("A, B, C and D", foo(List("A","B","C","D")))

The List object has the mkString method that will can convert to a string with a seperator. However most human languages treats the last element different when enumerating a list. For example A, B, C and D.

What is the best in terms of code size and reasonable efficiency to accomplish this? To be precise, I am searching for a function that satisfies:

assertEquals("",foo(List()))
assertEquals("A",foo(List("A")))
assertEquals("A and B",foo("List("A","B")))
assertEquals("A, B and C", foo(List("A","B","C")))
assertEquals("A, B, C and D", foo(List("A","B","C","D")))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追星践月 2024-12-18 17:57:07
def foo(xs: List[String]) = 
  (xs.dropRight(2) :\ xs.takeRight(2).mkString(" and "))(_+", "+_)

编辑:这可能更清楚一点:

def foo(xs: List[String]) = 
  (xs.dropRight(2) :+ xs.takeRight(2).mkString(" and ")).mkString(", ")

@axaluss 速度取决于列表长度。由于平均列表长度超过 4 个元素,第二个版本比 Tomasz 的版本更快。否则,速度会稍微慢一些。

def foo(xs: List[String]) = 
  (xs.dropRight(2) :\ xs.takeRight(2).mkString(" and "))(_+", "+_)

edit: This might be a bit clearer:

def foo(xs: List[String]) = 
  (xs.dropRight(2) :+ xs.takeRight(2).mkString(" and ")).mkString(", ")

@axaluss The speed depends on the list length. With an average list length above about 4 elements, this second version is faster than Tomasz's. Otherwise, it's slightly slower.

山人契 2024-12-18 17:57:07

我的看法:

def foo[T](list: List[T]): String = list match {
    case Nil => ""
    case x :: Nil => x.toString
    case x :: y :: Nil => x + " and " + y
    case x :: rs => x + ", " + foo(rs)
}

还要利用尾递归:

@tailrec def str[T](cur: String,  list: List[T]): String = list match {
    case Nil => cur
    case x :: Nil => cur + x
    case x :: y :: Nil => cur + x + " and " + y
    case x :: rs => str(cur + x + ", ", rs)
}

def foo[T](list: List[T]) = str("", list)

My take:

def foo[T](list: List[T]): String = list match {
    case Nil => ""
    case x :: Nil => x.toString
    case x :: y :: Nil => x + " and " + y
    case x :: rs => x + ", " + foo(rs)
}

Also to utilize tail recursion:

@tailrec def str[T](cur: String,  list: List[T]): String = list match {
    case Nil => cur
    case x :: Nil => cur + x
    case x :: y :: Nil => cur + x + " and " + y
    case x :: rs => str(cur + x + ", ", rs)
}

def foo[T](list: List[T]) = str("", list)
画离情绘悲伤 2024-12-18 17:57:07
def foo(list: List[String]) = list match{
  case Nil => ""
  case _ if list.length == 1 => list.first
  case _ => list.init.mkString(", ") + " and " + list.last
}
def foo(list: List[String]) = list match{
  case Nil => ""
  case _ if list.length == 1 => list.first
  case _ => list.init.mkString(", ") + " and " + list.last
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文