Git Product home page Git Product logo

coarse's Introduction

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 []}

Implementation

In hindsight, there was no apparent reason for me to use the van Laarhoven encoding for this library (aside from acting as a partial reimplementation for micro-lens). I still believe the current encoding is better than the naive encoding (as a struct with a function get and set), but I will try to explore more "clojure-ish" encodings of functional lenses. Currently the usages of pseudo-typeclasses in the code are like paper-cuts that really annoys me.

See Also

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 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.

coarse's People

Contributors

runeblaze avatar

Stargazers

Philipp Küng avatar  avatar  avatar Eric Shull avatar Guido Schmidt avatar Pete Doherty avatar Burin Choomnuan avatar Dustin Getz avatar Vic avatar Indraniel avatar Tommi Reiman avatar Daniel Slutsky avatar

Watchers

James Cloos avatar Daniel Slutsky avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.