| In the course of writing my unit testing library, I wanted to pluralize a word. Great! Arc has a method to do that! arc> (pluralize 2 "pass")
"passs"
Hrm, that's not right. Sure, we could write something to detect if we should add an "s" or an "es", but that would definitely be buggy.But we can explicitly set the plural, if it's different. (def pluralize (n str (o plural-form (string str "s")))
(if (or (is n 1) (single n))
str
plural-form))
(def plural (n str (o plural-form (string str "s")))
(string n
#\space
(pluralize n str plural-form)))
And how is it used? Well, here's some unit tests for it. (Note: I'd still love any feedback you've got on the unit test library (https://bitbucket.org/zck/unit-test.arc/)). (suite pluralize
0-pants (assert-same "pants"
(pluralize 0 "pant"))
1-pant (assert-same "pant"
(pluralize 1 "pant"))
2-pants (assert-same "pants"
(pluralize 2 "pant"))
0-oxen (assert-same "oxen"
(pluralize 0 "ox" "oxen"))
1-explosion (assert-same "explosion"
(pluralize 1 "explosion" "EXPLOSIONS ARE AWESOME!"))
7-formulae (assert-same "formulae"
(pluralize 7 "formula" "formulae")))
(suite plural
0-pants (assert-same "0 pants"
(plural 0 "pant"))
1-pant (assert-same "1 pant"
(plural 1 "pant"))
2-pants (assert-same "2 pants"
(plural 2 "pant"))
0-oxen (assert-same "0 oxen"
(plural 0 "ox" "oxen"))
1-explosion (assert-same "1 explosion"
(plural 1 "explosion" "EXPLOSIONS ARE AWESOME!"))
7-formulae (assert-same "7 formulae"
(plural 7 "formula" "formulae")))
Sweet! You have to explicitly write out the plural, but that's not so bad. I considered having the optional argument be suffix instead, but that left out plurals like "party" -> "parties", "foot" -> "feet", and "fish" -> "fish". This way is more general, arguably easier to write, is unit tested, fully backwards compatible, and so I think it's better. Also it doesn't use #\ , preferring #\space. So much easier to read.Of course, after implementing it, I found I didn't actually need to pluralize the word "pass" in the code I was writing. Dang. |