funcool / lentes

Functional references for Clojure and ClojureScript
https://cljdoc.org/d/funcool/lentes/CURRENT
BSD 2-Clause "Simplified" License
94 stars 10 forks source link

idea: cursors, but better #4

Closed ghost closed 7 years ago

ghost commented 7 years ago

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

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

niwinz commented 7 years ago

Hi @sj4

The idea is very nice, but I'm not so sure if I want this on the core, I need some time to think about this. It seems that it is very easy to do on client side and in general, it looks a little bit opinionated maybe?

Give me some days for think about that and how it can impact on the library usage :D and thanks for the idea!

ghost commented 7 years ago

let's keep it pure