Skip to content

carpentry-org/lens.carp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

lens

A simple Lens and Prism library for Carp.

The implementation of lenses and prisms is too general and does not follow the laws (i.e. you can construct lenses and prisms that shouldn’t work), but I think this is useful anyway.

Usage

(load "git@github.com:carpentry-org/lens.carp@master")

(deftype Address [city String street (Pair Int String)])

(defn main []
  (let-do [addr (Lens.for Address street)
           stre (Lens.for Pair b)
           lens (Lens.compose &addr &stre)
           data (Address.init @"Berlin" (Pair.init 10 @"Paul-Lincke-Ufer"))]
    (println* &(Lens.get &lens &data))
    (println* &(Lens.set &lens @&data @"no"))
    (println* &(Lens.over &lens @&data &(fn [a] (reverse &a))))
  )
)

It also provides you with helpers to construct Prisms for Array and Map types, using:

; will construct a prism for the second element of an array
(Prism.Array.for 1)

; will construct a prism for the map key "hello"
(Prism.Map.for "hello")

Have fun!

About

A simple Lens library for Carp

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published