What is reactive programming?
paulstovell.com
paulstovell.com
I should really learn what that means one day...
This fits well with the functional paradigm, where values are immutable and therefore 'timeless'.
In contrast, imperative code, if it has any actual semantics at all, tends to be operational:
Operational semantics: the "meaning" of code is how it affects the behaviour of the machine that's interpreting it.
The problem with operational semantics is exactly that of imperative code: in order to understand what some code will do, you need to know everything which 'happened before' (ie. the current state of the machine).
But in Haskell, say, because of lazy evaluation, it's easier to lose track of how much CPU effort something is going to cost, and when you're going to have to pay for it.
This is becoming less true, of course. Java getting lambdas hides a TON of places where you just managed to allocate a ton of memory and/or perform a ton of operations.
And note, I do think this downside can be oversold. So, don't take this as a condemnation of "functional" languages and methods. It definitely exists, though.
Granted, I am almost certainly simply infatuated with what is essentially an "anti immutable" algorithm. http://taeric.github.io/Sudoku.html
It isn't even that I don't think they have a good product. They certainly do. One need only actually read SICP to realize just how bloody amazing some of these ideas are. (Granted, they didn't use all of the modern names. But they certainly seem to have hit all of the modern ideas.)
Wouldn't knowledge of everything within the current scope be sufficient?
Also used as the corporate standard database management system and as what boils down to a mail merge client, and although this appears to outsiders as bad standup comedy, it is unfortunately merely factual.
Possibly one of the quickest ways to measure the dilbertian level of rot of an organization is to propose a small database and ask what tool they'd use to hold that information. Mysql, OK. Postgresql, eh, OK. Oracle, ummm. Excel, find the exit door and start running. Microsoft Word, Cthulhu take us to end the agony. I've seen all of the above. And survived. Barely. Perhaps not with my sanity intact.
edited: spreadsheets fit into the design pattern of jokes where you got a problem to fix, so you try to implement a spreadsheet to fix it, now you got two problems to fix. This is probably funnier in its original regex and perl and web framework variants.
Seriously though, Angular gives you the ability to do this in the browser, here's an example of how to implement an Excel-like spreadsheet using Angular: http://thomasstreet.com/blog/legacy/spreadsheet.html
http://dthompson.us/functional-reactive-programming-in-schem...
a = 10
b = function() { return expensive_computation(a) }
Now, even if you can call b without parens, it still has to redo the expensive calculation every time you access it (because a might have changed... even if it hasn't).The "destiny" operator is more like
a = 10
b = function() {
persistent cached_value = null
if is_null(cached_value) or has_changed(a) {
cached_value = expensive_computation(a)
}
return cached_value
}
but instead of that, you get to write (in this totally fictional programming language) a = 10
b <- expensive_computation(a)
and have the compiler take care of the caching and updating for you.Pull has the major downside that you never know when you need to pull a new value. Which is a pretty major downside.
And really, this does just feel like we baked some new semantics onto the observer pattern.
In the Rx-style reactive model the big thing is making the event streams, in addition to the events, first-class objects, so they can be composed and manipulated with the usual FP subjects like map/filter/reduce. This makes, for instance, throttling to optimize the propagation of changes almost trivial compared to the situation where you only have individual events without context.
More specifically, I think things are a lot easier when you can work way above those abstractions. As soon as you get into the weeds, all of the abstractions suck.
I also think this is specifically why people like angular so much. For many use cases, you are just setting values and having the UI present said values. Note, setting values, not appending to streams.
Now, do I expect that angular's internals would benefit from the streams metaphor? I certainly suspect so. Don't know, though.
Even Excel knows how to bind a displayed value directly to functional dependencies and only recalculate when necessary.
I'm not denying these techniques are useful, I'm just pointing out that a lot of what seems nice about them is actually attempts to reimplement functional programming inside languages that don't do it very well natively.
Before monads were 'invented' for functional programming, reactive programming (or stream based I/O) was the only way of handling state and side effects in Haskell.
That's true in most programming languages, but not in languages that distinguish side-effect free code. (Such a language implementation might still rerun a pure function because it doesn't include a way of "remembering" that a function has been called with particular arguments before, but that's an implementation detail.)
Wherever "destiny" would be appropriate, you necessarily have a referentially transparent function. Wherever you have a referentially transparent function, a smart compiler or runtime can avoid recalculating anyway, without bothering the programmer with the distinction between "function" and "destiny".
Below is Prof. David Harel's (the inventor of statecharts, formalized as UML State Machines) general characterization[3] of a reactive system; reactive programming would then be any programming methodology, language or environment which aims to service those characteristics.
* It continuously interacts with its environment, using inputs and outputs that are either continuous in time or discrete. The inputs and outputs are often asynchronous, meaning that they may arrive or change values unpredictably at any point in time.
* It must be able to respond to interrupts, that is, high-priority events, even when it is busy doing something else.
* Its operation and reaction to inputs often reflects stringent time requirements.
* It has many possible operational scenarios, depending on the current mode of operation and the current values of its data as well as its past behavior.
* It is very often based on interacting processes that operate in parallel.
[1] http://www.cs.cornell.edu/~jnfoster/papers/jnfoster-disserta...
[2] http://www.cis.upenn.edu/~bcpierce/papers/lenses-etapsslides...
[3] http://www.wisdom.weizmann.ac.il/~harel/reactive_systems.htm...
Sigh.
... and no types. So if a and b are strings in a "mostly string-ish language" then the value of b is "101" halfway thru and "111" at the end, right?
(I'd cut them a break if they used a single expressive and meaningful kanji even ifs only one glyph, although I'm struggling to think of a kanji that implies type and a compiler smart enough to understand the type embedded in the kanji... although it could happen)
http://conal.net/papers/icfp97/
Perhaps a more neutral description would be:
Reactive = Values are parameterised by the current time