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")))
edit: This might be a bit clearer:
@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.