下面举个例子
(defn simpfy-piece
"throw away the unnessesaries"
[piece]
[(schema/piece-name-id
(b/get-state :piece-name-ids)
(:entity piece)
(:key piece))
(:val piece)])
下面是修改之后的代码。
(defn piece->simple-piece
"throw away the unnessesaries"
[piece-name->id]
(fn [piece]
[(piece-name->id (:entity piece) (:key piece))
(:val piece)] ))
piece->simple-piece
。schema/piece-name-id
这个依赖的方法,通过参数传递进来。(defn query
[clause]
(query/query->entities
(query/vindex->entities
(query/entity-id->entity
(query/entity-id->pieces
(idx/entity-id->kindex ref-kindex)
(store/ids->pieces
(store/id->piece (fn [entity-name id]
(p/get! (b/get-state :pieces-db) id)))))
(query/pieces->entity (sc/piece-name->id
(b/get-state :piece-name-ids)))))
{:piece-name->id (sc/piece-name->id (b/get-state :piece-name-ids))
:piece-name->vindex ref-vindex}))
最后,有没有同学可以分享自己的心得,如何用好functional programming.
如果能分享代码更好。
我上面实现的功能比较简单,piece->simple-piece
是将一个map结构的数据,删除掉不需要的部分,同时将里面key为"name"的值转换成id,所以依赖piece-name->id
,如果各位来实现,是怎么个思路。
1
jiang42 2015-07-14 19:46:05 +08:00 via Android
看上去
|
2
jiang42 2015-07-14 19:46:53 +08:00 via Android
看上去像clojure,它的尾递归就是灾难
|
3
jiang42 2015-07-14 19:47:10 +08:00 via Android
像clojure,它的尾递归就是灾难
|
4
blacktulip 2015-07-14 19:49:25 +08:00 1
楼上这是在模拟尾递归?
|
5
jiang42 2015-07-14 19:53:45 +08:00 via Android
@blacktulip nope, 手机回复……我以为没回复上
|