Clojure - 合併Map



將兩個Map的條目合併到一個Map條目中。

語法

以下是語法。

(merge hmap1 hmap2)

引數 − ‘hmap1’ 是雜湊鍵和值的Map。‘hmap2’ 是雜湊鍵和值的Map,需要與第一個HashMap對映。

返回值 − 返回hasmap1和hasmap2的組合HashMap。

示例

以下是Clojure中merge的示例。

(ns clojure.examples.example
   (:gen-class))
(defn example []
   (def demokeys (hash-map "z" 1 "b" 2 "a" 3))
   (def demokeys1 (hash-map "a" 2 "h" 5 "i" 7))
   (println (merge-with + demokeys demokeys1)))
(example)

輸出

以上程式碼產生以下輸出。

{z 1, x 4, a 3, i 7, b 2, h 5}
clojure_maps.htm
廣告

© . All rights reserved.