有没有更简洁的方式在Clojure中实现以下操作?
请注意,我可能想在
这类似于此问题,但输出是向量而不是映射,因此我不能使用
(defn this [x] (* 2 x))
(defn that [x] (inc x))
(defn the-other [x] (-> x this that))
(defn make-vector [thing]
(let [base (vector (this (:a thing))
(that (:b thing)))]
(if-let [optional (:c thing)]
(conj base (the-other optional))
base)))
(make-vector {:a 1, :b 2}) ;=> [2 3]
(make-vector {:a 1, :b 2, :c 3}) ;=> [2 3 7]
By "cleaner" I mean something closer to this:
(defn non-working-make-vector [thing]
(vector (this (:a thing))
(that (:b thing))
(if (:c thing) (the-other (:c thing)))))
(non-working-make-vector {:a 1, :b 2} ;=> [2 3 nil] no nil, please!
(non-working-make-vector {:a 1, :b 2, :c 3} ;=> [2 3 7]
请注意,我可能想在
thing
中的任何键上调用一些任意函数(例如this
、that
、the-other
),并将结果放入返回的向量中。重要的是,如果该键不存在于映射中,则不应在向量中放置nil
。这类似于此问题,但输出是向量而不是映射,因此我不能使用
merge
。