Search code examples
clojureordinalordinalsordinal-indicator

Is there a Clojure function for ordinal indicators?


I want a function that will populate the contents of this map:

{:1 "first" :2 "second" :3 "third" :4 "fourth" ... :100 "one-hundredth" ...}

So I can do something similar to

(println "This is the " (:3 {... :3 "third" ...}) " item in the sequence")
> This is the third item in the sequence

Is there an existing Clojure library that does this?

My question is: Is there a Clojure function for ordinal indicators?

(If there is a better way to describe this kind function - please let me know)


Solution

  • user=> (require '[clojure.pprint :as pprint])
    nil
    user=> (map #(pprint/cl-format nil "~:R" %) [1 2 3 4 100])
    ("first" "second" "third" "fourth" "one hundredth")
    

    note that :1 is not 1, and : is not a syntax for map keys

    user=> (pprint/pprint (into {} (map (fn [n] [n (pprint/cl-format nil "~:R" n)]) (range 20))))
    {0 "zeroth",
     7 "seventh",
     1 "first",
     4 "fourth",
     15 "fifteenth",
     13 "thirteenth",
     6 "sixth",
     17 "seventeenth",
     3 "third",
     12 "twelfth",
     2 "second",
     19 "nineteenth",
     11 "eleventh",
     9 "ninth",
     5 "fifth",
     14 "fourteenth",
     16 "sixteenth",
     10 "tenth",
     18 "eighteenth",
     8 "eighth"}
    nil