Skip to content

Instantly share code, notes, and snippets.

@ds300
Created September 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ds300/e999180e504c81bae0f3 to your computer and use it in GitHub Desktop.
Save ds300/e999180e504c81bae0f3 to your computer and use it in GitHub Desktop.
; 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?
@alandipert
Copy link

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.

@ds300
Copy link
Author

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?

@alandipert
Copy link

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
Copy link

ul commented Oct 5, 2015

Will WeakMap and its polyfills help us?

@alandipert
Copy link

@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