从向量的向量中获取字符串值

问题描述 投票:0回答:1

给出此数据结构

(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"

clojure clojurescript
1个回答
0
投票

轻松自如!只需将字符串对的序列转换为映射即可快速查找:

(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 CheatSheetthis list

© www.soinside.com 2019 - 2024. All rights reserved.