Skip to content

Instantly share code, notes, and snippets.

@acobster
Created December 29, 2020 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acobster/76d656c3e266e012dfccf43946f8589d to your computer and use it in GitHub Desktop.
Save acobster/76d656c3e266e012dfccf43946f8589d to your computer and use it in GitHub Desktop.
Oxford Comma
(defn oxford-comma [phrases]
(if (> (count phrases) 2)
(join ", and " [(join ", " (butlast phrases)) (last phrases)])
(join " and " phrases)))
(oxford-comma []) ;; => ""
(oxford-comma ["one"]) ;; => "one"
(oxford-comma ["one" "two"]) ;; => "one and two"
(oxford-comma ["one" "two" "three"]) ;; => "one, two, and three"
(oxford-comma ["one" "two" "three" "four"]) ;; => "one, two, three, and four"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment