我如何找到这两个向量的相应索引?
How do i find the corresponding index for these two vectors?
在 clojure 中,我试图让我的订单查找器向量在名称列表中查找名称的索引:
(def customer-names ["Adam" "Beth" "Chloe" "Daniel" "Nathan" "Olivia"])
然后使用该索引在此向量中查找相应的披萨订单
(def pizzas ["P" "M" "P" "C" "P" "P" "S" "C" "M" "M" "S" "C"])
列表中的每个人每个订单将有 2 个比萨饼(与客户列表的订单相同)
(defn order-finder [customer-names][pizzas]
customer-names(.indexOf v "Adam")
(.indexOf (pizzas = (.indexOf (customer-names * 2)))))
由于我是这门语言的新手,我想知道是否可以这样做?
我想您的实际需求是按客户名称查找比萨饼。获得 .indexOf
只是一个临时步骤 - 您可以跳过这一步以使事情变得更简单。
(def customer-names ["Adam" "Beth" "Chloe" "Daniel" "Nathan" "Olivia"])
(def pizzas ["P" "M" "P" "C" "P" "P" "S" "C" "M" "M" "S" "C"])
;; each customer has 2 pizzas - so the pizzas array can be re-arranged like:
;;
(partition 2 pizzas)
;; => (("P" "M") ("P" "C") ("P" "P") ("S" "C") ("M" "M") ("S" "C"))
;; now the order finder is just a `Cojure` map with keys as
;; customer names, values as the pizzas ordered
;;
(def order-finder
(zipmap customer-names (partition 2 pizzas)))
;; => {"Adam" ("P" "M"), "Beth" ("P" "C"), "Chloe" ("P" "P"), "Daniel" ("S" "C"), "Nathan" ("M" "M"), "Olivia" ("S" "C")}
;; Now to find pizzas by customer name, just look it up from the order-finder map:
;;
(order-finder "Adam")
;; => ("P" "M")
在 clojure 中,我试图让我的订单查找器向量在名称列表中查找名称的索引:
(def customer-names ["Adam" "Beth" "Chloe" "Daniel" "Nathan" "Olivia"])
然后使用该索引在此向量中查找相应的披萨订单
(def pizzas ["P" "M" "P" "C" "P" "P" "S" "C" "M" "M" "S" "C"])
列表中的每个人每个订单将有 2 个比萨饼(与客户列表的订单相同)
(defn order-finder [customer-names][pizzas]
customer-names(.indexOf v "Adam")
(.indexOf (pizzas = (.indexOf (customer-names * 2)))))
由于我是这门语言的新手,我想知道是否可以这样做?
我想您的实际需求是按客户名称查找比萨饼。获得 .indexOf
只是一个临时步骤 - 您可以跳过这一步以使事情变得更简单。
(def customer-names ["Adam" "Beth" "Chloe" "Daniel" "Nathan" "Olivia"])
(def pizzas ["P" "M" "P" "C" "P" "P" "S" "C" "M" "M" "S" "C"])
;; each customer has 2 pizzas - so the pizzas array can be re-arranged like:
;;
(partition 2 pizzas)
;; => (("P" "M") ("P" "C") ("P" "P") ("S" "C") ("M" "M") ("S" "C"))
;; now the order finder is just a `Cojure` map with keys as
;; customer names, values as the pizzas ordered
;;
(def order-finder
(zipmap customer-names (partition 2 pizzas)))
;; => {"Adam" ("P" "M"), "Beth" ("P" "C"), "Chloe" ("P" "P"), "Daniel" ("S" "C"), "Nathan" ("M" "M"), "Olivia" ("S" "C")}
;; Now to find pizzas by customer name, just look it up from the order-finder map:
;;
(order-finder "Adam")
;; => ("P" "M")