【发布时间】:2018-08-21 22:01:44
【问题描述】:
我正在开发一个迷你社交媒体 API,允许用户插入新的个人资料,将两个个人资料连接在一起(如朋友),然后根据“我朋友的朋友”规则接收推荐。
现在我正在尝试为 Profile 创建 API。
我有一个原子,其中包含一个地图列表,每个配置文件一个。
(def profiles (atom ()))
(defn create [request]
(swap! profiles conj {:id (get-in request [:body "id"])
:name (get-in request [:body "name"])
:age (get-in request [:body "age"])
:recommendable (get-in request [:body "recommendable"])
:friends (list)
})
(created "")
)
当我偶然发现一个问题时,我试图为 API 的 GET http 动词开发 find-by-id。如何从所述列表中的地图中获取值,以便对其应用函数?
例如,在这里我试图使用过滤器函数只返回包含给定 ID 的地图。但我不断收到错误:
(defn find-by-id [id]
(filter #(= (:id %) id) profiles)
)
Dont know how to create ISeq from: clojure.lang.Atom
在我看来,过滤器不适用于 Atom。
同样的事情要删除:
(defn delete-by-id [id]
(swap! profiles (remove #(= (:id %) id) profiles))
)
当我尝试使用@profiles 时,我得到一个空数组。更糟糕的是,当我尝试使用 REPL 的过滤器功能时,它工作得很好。
这让我想知道我在这里错过了什么。
谁能告诉我这是怎么回事?
提前致谢。
【问题讨论】:
标签: clojure