我有一个字符串"101,R:102,R:301,L:302,L:999",我想编写一个函数,它处理字符串并返回如下所示的映射:
{
:left [301 302],
:right [101 102],
:unknown [999]
}下面是我所写的,但我仍然停留在减函数上。任何帮助都将不胜感激。谢谢。
(defn process
[data return-hash]
(let [id-side (str/split data #",")
id (first id-side)
side (second id-side)]
(cond
(= "L" side) (update-in return-hash [:left] conj id)
(= "R" side) (update-in return-hash [:right] conj id)
:else (update-in return-hash [:unknown] conj id)
)
))
(defn get-hash
[data]
(let [id-side-array (str/split data #":")]
(reduce
// a function calling process() method to update the map
id-side-array)
))
(get-hash "101,R:102,R:301,L:302,L:999")
=>
{
:left [301 302],
:right [101 102],
:unknown [999]
} 发布于 2014-03-22 15:00:43
你就快到了:
process的参数顺序,使return-hash优先;(fnil conj [])调用中使用conj代替conj;(reduce process {} id-side-array)中使用get-hash。https://stackoverflow.com/questions/22578966
复制相似问题