reflex-frp / reflex

Interactive programs without callbacks or side-effects. Functional Reactive Programming (FRP) uses composable events and time-varying values to describe interactive systems as pure functions. Just like other pure functional code, functional reactive code is easier to get right on the first try, maintain, and reuse.
https://reflex-frp.org
BSD 3-Clause "New" or "Revised" License
1.07k stars 149 forks source link

What about lazy event? #160

Closed MarisaKirisame closed 6 years ago

MarisaKirisame commented 6 years ago

So suppose I created an computationally expensive event with foldDyn (so it is inside a monadwidget), and pass it into some user defined function. Now there is a good chance that the user defined function will ignore it and do nothing.

So basically, a event is registered but never used.

Will it still be busy calculating? Or will it get optimized away?

ryantrinkle commented 6 years ago

It will calculate until it is eliminated by the garbage collector. The reason is that, otherwise, it would miss input event occurrences that came before the output was subscribed to. All functions that create state (i.e. monadhold functions) work this way.

On Jan 21, 2018 10:49 AM, "雾雨魔理沙" notifications@github.com wrote:

So suppose I created an computationally expensive event with foldDyn (so it is inside a monadwidget), and pass it into some user defined function. Now there is a good chance that the user defined function will ignore it and do nothing.

So basically, a event is register but never used.

Will it still be busy calculating? Or will it get optimized away?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/reflex-frp/reflex/issues/160, or mute the thread https://github.com/notifications/unsubscribe-auth/ABGlYDua7wV54-0JhloGS04nnCWbo-fvks5tM1x-gaJpZM4Rl3Em .

MarisaKirisame commented 6 years ago

When will it be eliminated though?

ryantrinkle commented 6 years ago

When the garbage collector runs, if there aren't any outstanding references to the state cell ('hold's), it will eliminate all the subscriptions from the events, which will stop the updating. Unfortunately, GC isn't super predictable, although you can trigger it manually.

One other thing to note: I don't think foldDyn will actually force each value, so even though the folding will continue, it may not actually do the work of your combining function until something needs it.

MarisaKirisame commented 6 years ago

So let me repeat: foldDyn will eagerly push, but the value inside is still lazy, and is a thunk, so that expensive computation is not carry out.

Did I get it?