Git Product home page Git Product logo

lentes's People

Contributors

bas080 avatar jellelicht avatar niwinz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lentes's Issues

idea: cursors, but better

Libraries such as reagent and freactive come with built-in
support for cursors, e.g. (cursor a [:a :b]) vs. (l/derive (l/in [:a :b]) a)

Since lenses are even more generic, it is possible to create a
cursor-like abstraction with lentes, by implementing IIndexed and ILookup in RWFocus

  IIndexed
  (-nth
    ([self i] (l/derive (l/nth i) self)))
  ILookup
  (-lookup
    ([self k] (l/derive (l/key k) self))))
  • instead of having to write: (l/derive (l/key :items) a)
    it is now possible to simply write: (:items a)
  • similarly, use (get-in a [:a :b]) instead of (l/derive (l/in [:a :b]) a)
  • and (nth a 3) instead of (l/derive (l/nth 3) a)

Since a is not dereferenced, this will return a new lentes (RW)-focus,

This is even better than the cursors in e.g. reagent :) - it makes code very readable, because it allows the usage of 'get' 'nth' 'get-in' etc. instead of introducing a new 'cursor' function

see also this commit for an example:
https://github.com/sj4/todomvc/commit/3a3690f67c46d8bff11d813f0cf5f9f246c7c97b
Thanks in advance for considering

missing default value?

(lens/over (lens/in [:path] ["default"])
           #(conj % "hi")
           {})

returns {:path ("hi")} but I expect {:path ["default" "hi"]}

I would expect the default value to get passed into the update function. Is the above how it's supposed to work?

content.adoc Composition example gets "nth not supported on this type: Long" error

When I evaluate the composition example (using cider nrepl) I get an "nth not supported on this type: Long" error.

user> (require '[lentes.core :as l])
nil
user> (def my-lens (comp l/fst l/fst (l/nth 2)))
#'user/my-lens
user> (def data
[[0 1 2]
[3 4 5]])
#'user/data
user> (l/focus my-lens data)
UnsupportedOperationException nth not supported on this type: Long clojure.lang.RT.nthFrom (RT.java:888)
user>

Either my-lens should have one fewer l/fst function in the composed function or the data needs another level of square brackets, right?

This works for me:

user> (def my-lens2 (comp l/fst (l/nth 2)))
#'user/my-lens2
user> (l/focus my-lens2 data)
2
user> (def data2
[[[0 1 2]
[3 4 5]]])
#'user/data2
user> (l/focus my-lens data2)
2
user>

(satisfies? IAtom (l/focus-atom l/id (atom 5))) => false

With lentes, two kinds of lenses can be created: two-way, writable lenses, and one-way, read-only lenses.

(def foobar (atom 5))
(def bar (l/focus-atom l/id foobar)) ;; has value 5
@foobar ; => 5
@bar ; => 5
(swap! foobar inc) ;; Both foobar and bar have value 6
(swap! bar inc) ;; Both have value 7

The bug is, bar does not satisfy IAtom:

(satisfies? IAtom bar) ;; false, should be true
(satisfies? IWatchable bar) ;; true
(satisfies? IReset bar) ;; true
(satisfies? ISwap bar) ;; true

There is another problem related to the read-only lenses.

(def foobar (atom 5))
(def read-only (l/focus-atom (l/getter identity) foobar)) ; has value 5
(swap! inc foobar) ;; Both have value 6
(swap! inc read-only) ;; Fails as it should, it's a read-only lense

(satisfies? IAtom read-only) ;false, should be true
(satisfies? IWatchable read-only) ; true
(satisfies? IReset read-only) ; true, should be false?
(satisfies? ISwap read-only) ; true, should be false?

So, three problems really:

  1. Lenses should satisfy IAtom
  2. Read-only lenses should not satisfy ISwap
  3. Read-only lenses should not satisfy IReset

IEquiv for RWFocus?

In an app I'm currently developing I found a use cases where it would be beneficial if I could check if two lenses are the same. Currently this is only possible by using deref and compare the values (and that does not guarantee the lenses are pointing at the same location in a datastructure)

would it be possible to implement IEquiv, e.g. by comparing the id of the lens?

`nth` should support lists

This would be convenient because often parts of bigger data end up being lists after applying core functions (e.g. map).

Steps:

(require '[lentes.core :as l])
(let [data (list 0 1 2 3)
      lense (l/nth 2)]
  (println "A ->" (l/focus lense data))
  (l/over lense inc data)
  (println "B ->" (l/focus lense data)))

Expected:

A -> 2
B -> 3

Actual:

1 -> 2
Uncaught Error: No protocol method IAssociative.-assoc defined for type cljs.core/List: (0 1 2 3)

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.