Last active
August 29, 2015 13:55
-
-
Save fortruce/8725445 to your computer and use it in GitHub Desktop.
Naive pluralization helper function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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