Clojure-maps-mergewith

提供:Dev Guides
移動先:案内検索

Clojure-マップのマージ

最初に結合された残りのマップで構成されるマップを返します。 キーが複数のマップで発生する場合、後者のマッピング(左から右)が結果のマッピングと結合されます。

構文

構文は次のとおりです。

(merge-with f hmap1 hmap2)

パラメータ-「f」は、ハッシュマップに適用する必要がある演算子です。 「hmap1」は、ハッシュキーと値のマップです。 「hmap2」はハッシュキーと値のマップであり、最初のHashMapでマップする必要があります。

戻り値-最初に結合された残りのマップで構成されるマップを返します。

以下は、Clojureでのmerge-withの例です。

(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, a 5, i 7, b 2, h 5}

出力では、キー「a」が2回出現するため、演算子+に従って両方のHashMapから値が追加されることに注意してください。