coarse

Clojure lens library, implements a small subset of ekmett's lens


License
EPL-1.0

Documentation

coarse

Clojure lens library, useful for data manipulation.

Getting Started

To install, add the following dependency to your project or build file:

[coarse "0.1.0-SNAPSHOT"]
(ns example.example
  (:require [coarse.core :refer [view ix]]))

(println (view (ix 3) [1 2 3 4 5]))

See the interactive guide for more in depth information.

Stability

While this library has been tested, do expect possible bugs. I feel this library is stable for use in toy projects, so obviously do not use this in production.

I will adhere to semver whenever possible, but expect breaking changes.

Examples

Basics

(view (ix 0) [:a :b :c :d])
; :a

(sett (ix 0) :yeah [:foo :b :c :d])
; [:yeah :b :c :d]

(over (ix 1) inc [1 2 3 4])
; [1 3 3 4]

(over (_index even?) inc [1 2 3 4])
; [2 2 4 4]

(over (_index odd?) inc [1 2 3 4])
; [1 3 3 5]

(sett (_taking 2) 99 [1 2 3 4])
; [99 99 3 4]

(to-list-of (_taking 2) [:a :b :c :d])
; (:a :b)

(to-list-of (*> each each) [[1] [2 3] [4 5] [6]])
; (1 2 3 4 5 6)

Pseudo-Imperative Code

(def game-state
  {:player {:pos {:x 0 :y 0} :hp 100 :inventory []}})

(view :x {:x 99 :z 3})
; 99
  
(->> game-state
  (+>> (*> :player :pos)
    (+= :x 1)
    (+= :y 1)))
; {:player {:pos {:x 1 :y 1}} :hp 100 :inventory []}

See Also

specter

Specter solves the same problem, but it is much more mature and stable. Use specter if you are interested in getting things done. Use this library if you are interested in toying with Haskell concepts.

traversy

Traversy is similar in nature, with a better encoding of lens, but fewer features than this library. This library is also closer to the original Haskell implementation.

Also I borrowed the *> notation from traversy.

License

Copyright © 2018 RuneBlaze

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