我想要一个将填充此地图内容的函数:
{: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库可以做到这一点?
我的问题是: ordinal indicators是否有Clojure功能?
(如果有更好的方式来描述这种功能 - 请告诉我)
答案 0 :(得分: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