Generating a single string with an Oxford comma from a list What are some clever (brief and idiomatic) approaches to taking a list of strings and returning a single properly punctuated string built from the list, with each element quoted.

This came up for me while experimenting with Groovy, for which my too-literal, but illustrative solution is

def temp = things.collect({"\'${it}\'"})
switch (things.size()) {
    case 1:
        result = temp[0]
        break
    case 2:
        result = temp.join(" and ")
        break
    default:
        result = temp.take(temp.size()-1).join(", ") + ", and " + temp[-1]
        break
}


That is, ['1'] should yield '1', ['1','2'] should yield '1 and 2', [see what I did there?] and ['1','2','3'] should yield '1, 2, and 3'.

I have some good answers for Groovy, but I'd like to see what other languages can do.

What are some compact clever approaches in various languages that take advantage of the features and idioms of those languages?
CSS, 132 116 115 bytes



a:not(:last-child):nth-child(n+2):after,a:nth-last-child(n+3):after{content:","}a+:last-child:before{content:"and "
&lt;p&gt;
  &lt;a&gt;one&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
  &lt;a&gt;one&lt;/a&gt;
  &lt;a&gt;two&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
  &lt;a&gt;one&lt;/a&gt;
  &lt;a&gt;two&lt;/a&gt;
  &lt;a&gt;three&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
  &lt;a&gt;one&lt;/a&gt;
  &lt;a&gt;two&lt;/a&gt;
  &lt;a&gt;three&lt;/a&gt;
  &lt;a&gt;four&lt;/a&gt;
&lt;/p&gt;




a:not(:last-child):nth-child(n+2):after,a:nth-last-child(n+3):after{content:","}a+:last-child:before{content:"and "


CSS is not seen too often in code golf because it can only format text, but it actually works for this challenge and I thought it would be fun to do. See it in action using the snippet above (click "Show code snippet").

List should be in a linked HTML file with each element surrounded by &lt;a&gt; tags and separated by line breaks. The list items should be the only elements in their parent element, e.g.

&lt;a&gt;one&lt;/a&gt;
&lt;a&gt;two&lt;/a&gt;
&lt;a&gt;three&lt;/a&gt;


Explanation

a:not(:last-child):nth-child(n+2)::after,
a:nth-last-child(n+3)::after {
    content: ",";
}

a + :last-child::before {
    content: "and ";
}


Let's consider the ungolfed version above. If you're not familiar with how CSS works, everything outside the curly braces is a selector that determines the set of HTML elements to which the declarations inside the braces apply. Each selector-declaration pair is called a rule. (It's more complicated than that but will suffice for this explanation.) Before any styling is applied, the list appears separated by only spaces.

We want to add commas after every word except the last, except for two-word lists, which get no commas. The first selector, a:not(:last-child):nth-child(n+2):after, selects all elements except the first and the last. :nth-child(n+2) is a shorter way of saying :not(:first-child), and it basically works by selecting elements whose index (starting at 1) is greater than or equal to 2. (Yes, it still confuses me a little. The MDN docs might help.)

Now we just need to select the first element to get a comma if there are three or more elements total. a:nth-last-child(n+3):after works like :nth-child, but counting from the back, so it selects all elements except the last two. The comma takes the union of the two sets, and we use the :after pseudo-element to add content immediately after each selected element.

The second rule is easier. We need to add "and" before the last element in the list, unless it is a single element. In other words, we need to select the last element that is preceded by another element. + is the adjacent sibling selector in CSS.