目录

merge-with

返回一个映射,该映射由第一个映射的其余映射组成。 如果一个键出现在多个映射中,则后者(从左到右)的映射将与结果中的映射组合。

语法 (Syntax)

以下是语法。

(merge-with f hmap1 hmap2)

Parameters - 'f'是需要应用于哈希映射的运算符。 'hmap1'是散列键和值的映射。 'hmap2'是散列键和值的映射,需要与第一个HashMap映射。

Return Value - 返回一个映射,该映射由第一个映射的其余映射组成。

例子 (Example)

以下是在Clojure中合并的示例。

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

输出 (Output)

上面的代码产生以下输出。

{z 1, a 5, i 7, b 2, h 5}

请注意,在输出中,因为键'a'出现两次,所以根据operator +从两个HashMaps添加该值。

↑回到顶部↑
WIKI教程 @2018