向关键字值添加函数

时间:2014-01-16 13:29:15

标签: clojure

我有这样的地图结构...

    {"email@gmail.com"{:email "email@gmail.com", :password "pass"},
    "email2@yahoo.com"{:email "email2@yahoo.com", :password "pass2"},
    "email3@yahoo.com"{:email "email3@yahoo.com", :password "pass3"},
    "email4@gmail.com"{:email "email4@gmail.com", :password "pass4"}}

如何在所有值中添加一个函数来引用:password关键字?我的函数有一个只接受字符串的参数......这是我试过但无法解决的问题......

(更新 - 在mymap [:密码](myfunction“这里假设是来自:password关键字的值”))

2 个答案:

答案 0 :(得分:2)

您应该分别更新每一行并累积,然后在列表上使用reduce进行迭代,然后对每个元素update-in password键和assoc生成的行进行迭代结果。

(reduce (fn[result row] 
          (assoc result  
                 (first row) ;;email
                 (update-in (second row) 
                            [:password] 
                            (fn[pass] ..do something..))))
        {}
        mymap)

答案 1 :(得分:1)

(reduce-kv (fn [result k cfg-map]
             (assoc result
               k (update-in cfg-map [:password] your-fn)))
           {} your-ds)

(into {} (mapv #(update-in % [1 :password] your-fn) your-ds))