Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
; So given this:
(def cellcell (cell (cell 5)))
; My question boils down to whether there is any way to make a cell which
; contains the value of the inner cell of cellcell regardless of whether we do
; things like
(reset! cellcell (cell 10))
; or
(reset! @cellcell 15)
; the naive thing is
(def value (cell= @cellcell))
; but that doesn't work for the latter case because only cellcell is
; hooked up to the propagation graph.
; So by 'Can formula cells have dynamic set of inputs?' I mean:
; Can the propagation graph structure be influenced by the contents of cells?
; Or is it fixed at the point when the 'lifted' functions returned by `formula`
; are invoked?

This is one way:

(def cellcell (cell (cell 15)))

((formula
  (fn [cc]
    ((formula
      (fn [c]
        (println c)))
     cc)))
 cellcell)
;; 15 is printed

(reset! cellcell (cell 10))
;; 10 is printed

(swap! @cellcell inc)
;; 11 is printed

An implication of this is that after reset!, the first formula over (fn [c] ...) was not explicitly destroyed, and so will not be garbage collected.

Owner
ds300 commented Sep 29, 2015

Does that mean if, at the end, you still had a reference to the original inner cell containing 15, incrementing it would print 16?

That's right. If we had weak references, the JavaScript GC could deem such cells unreachable/immutable for us, and formulas would be collected automatically. Unfortunately, it doesn't look like JavaScript will get them anytime soon, if ever.

ul commented Oct 5, 2015

Will WeakMap and its polyfills help us?

@ul sadly no... WeakMap is only really suitable for caching, or similar scenarios where the consumer supplies the key. Since they're not iterable, they're not suitable for general weak storage, which is how we would want to use them - to store a weak collection cell references that could be iterated through. WeakReference really is the thing we want, since we could make them values in our priority map, and the propagator could discard entries nulled by the GC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment