What is the best way to list a natural language list (Scala)?

The object Listhas a method mkStringthat can convert to a delimited string. However, most human languages ​​consider the last element when listing a list. For example, A, B, C, and D.

What is the best in terms of code size and reasonable efficiency for this? To be precise, I am looking 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")))
+5
source share
3 answers
def foo(xs: List[String]) = 
  (xs.dropRight(2) :\ xs.takeRight(2).mkString(" and "))(_+", "+_)

change . This might be a little clearer:

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

@axaluss Speed ​​depends on the length of the list. With an average list length above 4 items, this second version is faster than Tomas’s. Otherwise, it is a little slower.

+9

:

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)
+8
def foo(list: List[String]) = list match{
  case Nil => ""
  case _ if list.length == 1 => list.first
  case _ => list.init.mkString(", ") + " and " + list.last
}
+4

All Articles