给定此数据结构
(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"
发布于 2020-04-04 07:24:10
简单多了!只需将字符串对序列转换为映射以进行快速查找:
(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。
https://stackoverflow.com/questions/61021569
复制相似问题