首页 > 其他分享 >无涯教程-Clojure - merge-with函数

无涯教程-Clojure - merge-with函数

时间:2023-10-26 21:03:16浏览次数:41  
标签:clojure 映射 无涯 运算符 merge 哈希 Clojure example

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

merge-with - 语法

以下是语法。

(merge-with f hmap1 hmap2)

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

返回值  -  返回由映射到第一个映射的其余映射组成的映射。

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"出现了两次,因此按照运算符+从两个HashMap中添加了该值。

参考链接

https://www.learnfk.com/clojure/clojure-maps-mergewith.html

标签:clojure,映射,无涯,运算符,merge,哈希,Clojure,example
From: https://blog.51cto.com/u_14033984/8042776

相关文章

  • 无涯教程-Clojure - vals函数
    返回映射中的值列表。vals-语法以下是语法。(valshmap)参数   - "hmap"是哈希键和值的映射。返回值 - 返回映射中的值列表。vals-示例(nsclojure.examples.example(:gen-class))(defnexample[](defdemokeys(hash-map"z""1""b""2"&......
  • 无涯教程-Clojure - keys函数
    返回Map中的键列表。keys-语法以下是语法。(keyshmap)参数   -  "hmap"是哈希键和值的映射。返回值 - 返回Map中的键列表。keys-示例(nsclojure.examples.example(:gen-class))(defnexample[](defdemokeys(hash-map"z""1""b""2"......
  • 无涯教程-Clojure - pop函数
    对于列表或队列,返回没有第一项的新列表/队列,对于向量,返回没有最后一项的新向量。pop-语法以下是语法。(popvec)参数   - "vec"是元素的向量集。返回值 - 返回不带最后一项的新向量。pop-示例(nsclojure.examples.example(:gen-class))(defnexamp......
  • 无涯教程-Clojure - superset?函数
    判断set1是否是set2的超集。superset?-语法以下是语法。(superset?set1set2)参数   - "set1"是第一组元素,"set2"是第二组元素。返回值 - 如果set1是set2的超集,则为true,否则为false。superset?-示例(nsclojure.examples.example(:require[cloju......
  • 无涯教程-Clojure - conj函数
    返回一个新列表,其中该列表位于开头,而要附加的元素位于结尾。conj-语法以下是语法。(conjlstelementlst)参数   - "elementlst"是需要添加到列表中的元素列表,"lst"是元素列表。返回值 - 带有附加值的列表。conj-示例(nsclojure.examples.example......
  • 无涯教程-Clojure - nth函数
    此函数返回列表中第"n"个位置的元素。nth-语法以下是语法。(nthlstindex)参数   - "lst"是列表。"index"是元素的索引位置,需要返回。返回值 - 列表中索引位置的值。nth-示例(nsclojure.examples.example(:gen-class))(defnexample[](pr......
  • 无涯教程-Clojure - cons函数
    返回一个新列表,其中元素添加到列表的开头。cons-语法以下是语法。(conselementlst)参数   - "element"是需要添加到列表中的元素,"lst"是项目列表。返回值 - 带有附加值的列表。cons-示例(nsclojure.examples.example(:gen-class))(defnexample......
  • 无涯教程-Clojure - first函数
    此函数返回列表中的第一项。first-语法以下是语法。(firstlst)参数   - "lst"是项目列表。返回值 - 列表中的第一个值。first-示例以下是Clojure中first的示例。(nsclojure.examples.example(:gen-class))(defnexample[](println(first(......
  • 无涯教程-Clojure - upper-case函数
    将字符串转换为全部大写。upper-case-语法以下是语法。(upper-cases)参数   - 其中"s"是要转换的字符串。返回值 - 大写字符串。upper-case-示例以下是Clojure中大写字母的示例。(nsclojure.examples.hello(:gen-class))(defnhello-Learnfk[]......
  • 无涯教程-Clojure - float?函数
    如果数字为浮点数,则返回true。float?-语法以下是语法。(float?number)float?-示例以下是浮动测试函数的示例。(nsclojure.examples.hello(:gen-class));;ThisprogramdisplaysHelloLearnfk(defnExample[](defx(float?0))(printlnx)......