Skip to content

Instantly share code, notes, and snippets.

@fortruce
Last active August 29, 2015 13:55
Show Gist options
  • Select an option

  • Save fortruce/8725445 to your computer and use it in GitHub Desktop.

Select an option

Save fortruce/8725445 to your computer and use it in GitHub Desktop.
Naive pluralization helper function.
(defn pluralize
"Takes an integer and a string and returns a string of '%d %s' where %s is pluralized based on %d.
Assumes x >= 1 and s is singular."
([x s] (pluralize x s nil))
([x s ss]
(format "%d %s" x
(if (= 1 x) s
(or ss (condp re-matches s
#".*(x|[^aeiou]o|ch|s)" (str s "es")
#".*[^aeiou]y" (str (apply str (drop-last s)) "ies")
(str s "s")))))))
(pluralize 4 "astronaut" "farmers") ;; "4 farmers"
(pluralize 1 "astronaut" "farmers") ;; "1 astronaut"
(pluralize 2 "day") ;; "2 days"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment