Fresnel

A library for composing lenses and working with complex state objects. This library is a paired down version lens code embedded in Christophe Grand's Enliven.

Artifact

All artifacts are published to clojars.

Clojars Project

Note: Breaking changes in 0.3.0-SNAPSHOT

If you reified the Lens protocol directly you will have to now reify IFetch and IPutback protocols.

Concepts

The concept of a lens is very simple. A lens consists of two methods; one that transforms the state object into desired form and one that takes a transform object and pushes its state back into the main state object. Fresnel provides a mechanism to define lenses and to compose them. Fresnel builds on the concepts that already existing in Clojure. In Clojure we see two types of lenses for associative structures. The first is the keyword and we use get and assoc as the transform functions. The other is composition of these lenses in the form of a vector. We use get-in and assoc-in as transform functions in this case. Fresnel extends this concept beyond associative structures and unifies the access methods under a single protocol Lens.

(def state {:a {:aa "a1,b1,c1"
                :ab 2}
            :b {:ba "a2,b2,c2"
                :bb 2}})

;fresnel supports custom lenses
(deflens comma-to-map [oval nval]
  :fetch 
    (reduce #(assoc %1 (.trim %2) true) {} (split oval #","))
  :putback
    (reduce #(if %1 (str %2 "," %1) %2)
            nil
            (filter #(nval %1) (keys nval))))

;fresnel supports composing of lenses
(def compound-lens [:a :aa comma-to-map])

(fetch state compound-lens)
;{:a1 true :b1 true :c1 true}

(def new-state 
   (putback state 
            compound-lens 
            {:a1 false :b1 true :c1 true :d1 true}))

;;new-state {:a {:aa "b1,c1,d1" :ab 2}
;;           :b {:ba "a2,b2,c2" :bb 2}})     

License

Copyright © 2014 Creighton Kirkendall

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.



Fresnel

构成镜头和处理复杂状态对象的库。这个图书馆是Christophe Grand Enliven的嵌入式镜头码。

神器

所有工件都会发布到 clojars

注意:打破0.3.0-SNAPSHOT

中的更改

如果您直接使用了镜头协议,那么您将必须重新编写IFetch和IPutback协议。

概念

镜头的概念非常简单。镜头由两种方法组成:一个将状态对象转换为所需形式,一个将变换对象转换为主状态对象。菲涅耳提供了一种定义镜片并组合它们的机制。菲涅尔建立在Clojure已经存在的概念上。在Clojure中,我们看到两种类型的镜片用于关联结构。第一个是关键字,我们使用get和assoc作为变换函数。另一种是以载体形式的这些透镜的组成。在这种情况下,我们使用get-in和assoc-in作为变换函数。菲涅耳将这一概念扩展到关联结构之外,并在单一协议镜头下统一了访问方法。

(def state {:a {:aa "a1,b1,c1"
                :ab 2}
            :b {:ba "a2,b2,c2"
                :bb 2}})

;fresnel supports custom lenses (deflens comma-to-map [oval nval] :fetch (reduce #(assoc %1 (.trim %2) true) {} (split oval #",")) :putback (reduce #(if %1 (str %2 "," %1) %2) nil (filter #(nval %1) (keys nval))))

;fresnel supports composing of lenses (def compound-lens [:a :aa comma-to-map])

(fetch state compound-lens) ;{:a1 true :b1 true :c1 true}

(def new-state (putback state compound-lens {:a1 false :b1 true :c1 true :d1 true}))

;;new-state {:a {:aa "b1,c1,d1" :ab 2} ;; :b {:ba "a2,b2,c2" :bb 2}})

许可证

版权所有©2014 Creighton Kirkendall

根据Eclipse公共许可证分发版本1.0或(at 您的选项)任何更高版本。




相关问题推荐