4

我想要一个可以填充此地图内容的函数:

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

所以我可以做类似的事情

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

是否存在执行此操作的现有 Clojure 库?

我的问题是:序数指标是否有 Clojure 函数?

(如果有更好的方式来描述这种功能 - 请告诉我)

4

1 回答 1

6
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")

注意:1不是1:也不是映射键的语法

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
于 2015-01-24T13:22:23.397 回答