给出此数据结构
(def file-types
[["figure" "Figure"]
["video" "Video"]
["graphic" "Inline Graphic/Custom Artwork"]
["other" "Other"]])
给出此“键”
(def file-type "graphic")
这是从相应的元组中获取第二个值的更好方法吗?
(defn get-file-label [file-types file-type]
(second (peek (filterv #(= (% 0) file-type) file-types))))
预期输出"Inline Graphic/Custom Artwork"
轻松自如!只需将字符串对的序列转换为映射即可快速查找:
(ns tst.demo.core
(:use tupelo.core tupelo.test))
(dotest
(let [file-types [["figure" "Figure"]
["video" "Video"]
["graphic" "Inline Graphic/Custom Artwork"]
["other" "Other"]]
file->type (into {} file-types)]
(is= (file->type "graphic") "Inline Graphic/Custom Artwork")))
请确保查看其他文档的Clojure CheatSheet和this list。