14

I'd like to know how Reactive Programming is tied to Functional-Programming.

Most papers refer "Reactive Programming" as "Functional Reactive Programming".

Does Reactive Programming can be implemented outside Functional Programming?

Is it easier to write Reactive Programs with Functional Language?

7
  • 1
    Your questions is maybe better fitted for this site: programmers.stackexchange.com
    – mwhs
    Commented Nov 8, 2013 at 13:26
  • 2
    There's one wikipedia for Reactive programming, and another for Functional Reactive programming. I'd start there.
    – keyser
    Commented Nov 8, 2013 at 13:27
  • 2
    If you look at the definition of reactive programming you will see that the concept behind it can be achieved with any paradigm that supports the idea of eventing or signaling. This just means that it is highly concurrent. And functional languages do support concurrency very well (by design / lamda calculus). That is all
    – mwhs
    Commented Nov 8, 2013 at 13:29
  • 1
    also: stackoverflow.com/questions/1028250/…
    – ljgw
    Commented Nov 8, 2013 at 13:33
  • 1
    @mwhs how lambda calculus enables good support for concurrency?
    – om-nom-nom
    Commented Nov 8, 2013 at 13:54

5 Answers 5

12

I use what I would call Reactive Programming or SEDA (Staged Event Driven Architecture) but I don't use much in the way of functional programming. http://www.slideshare.net/PeterLawrey/writing-and-testing-high-frequency-trading-engines-in-java

While it is easier to write reactive programs functionally it is not easier to write them to perform faster by using functional programming. Reusing mutable state is often 2-5x faster than creating new immutable objects all the time. So if you are using the reactive programming for performance, I wouldn't use functional programming.

Often developers feel they have to use multiple threads or cores, because they are there. This is like saying you need to use 100% of disk space or you are wasting it.

IMHO you should only add the complexity of multiple threads if it improves performance and it is the simplest way to achieve this improvement. What is often forgotten in the discussion about making concurrency easier is that the easiest solution is to use one thread, and, unless you have proven your solution is faster than that, you haven't convinced me that using multiple threads was ever helpful.

9
  • Not using provisioned resources is wasting them, in fact. Commented Nov 8, 2013 at 14:38
  • @RandallSchulz true, but putting in extra effort to use all the resources and being worse off is wasting more. Commented Nov 8, 2013 at 14:56
  • @Peter, thanks for your response. I agreed with you that sometimes you just don't need to use parelallel programming. But I'd like to know more about the principles of reactive programming and how it affects the design as we do today in O.O. programming. I'm reading a paper from Odersky that he "deprecated" the observer pattern and he stated how the observer violetes some software engineering principles. So do we need to think about functional programming in order to make reactive applications? Commented Nov 9, 2013 at 13:03
  • Actually yes, you should use multiple threads and cores. That is the hardware we have at our hands now. If this isn't important then lets all fall back to some Intel Pentium or so.
    – Chiron
    Commented Nov 12, 2013 at 10:41
  • 1
    @Chiron So you would use multiple threads even if that were slower than using one thread, just so you can use all the CPUs? So the priority is to burn all the CPUs, not make programs go faster? Commented Nov 12, 2013 at 14:01
4

My guess is you are taking the Reactive Programming course by Odersky/Meijer/Kuhn? Then you will have seen Martin Odersky's interpretation in the first session: He uses a very broad description from a dictionary, whereby reactive means "readily responsive to a stimulus". So it's about a program observing and waiting for some stimulus to which it responds.

So from this perspective reactions are fore most some sort of observation triggered functions. When you can compose them, e.g. you map events or dataflow variables, you would probably call this "functional" in the sense that a set of future values is declared as a function of event originated values.

Functional reactive programming or FRP on the other hand is a term coined by Conal Elliott and Paul Hudak (originally: Functional Reactive Animation, as it was referring to graphical interfaces). It is closely tied to their work and the Haskell programming language.

Many libraries which implement reactive ideas (see the WP article on reactive programming for example) share the event composition aspect with FRP, while they do not necessarily extend to the analytical/continuous signals or "behaviours" of FRP which complement the events.


You will find that some people claim that reactive programming without adhering to the canonical FRP—e.g. when using actors or channels—is "stealing" the term from the "true bearers" of that title. So this discussion can easily become ideological. On the other side of ideological, you will find that reactive is often (ab)used as a new buzz word. The "reactive manifesto" (manifesto… really!? you can even sign that stuff…) would probably be an example of this side.

2
  • Good point. I'm reading a paper from Odersky that he "deprecated" the observer pattern and he stated how the observer violetes some software engineering principles. This paper, IMHO, shows that the future is functional programming, because this paradigm addresses most of concerns in reactive programming. Here the link for this paper [link]lampwww.epfl.ch/~imaier/pub/DeprecatingObserversTR2010.pdf Commented Nov 9, 2013 at 12:52
  • 1
    Not everything can be nicely composed, so the idea that functional is the only remedy doesn't hold. An example are collection views: A view shows a list of elements, and some related views need to react to elements being added or removed from the underlying model. This is trivial with standard MVC, but how do you define that purely functional? What is the type to be composed, a Seq[A]? This can get very inefficient when you really want to know which element was added/removed. Functional is good for expressions, things that evaluate to an individual, but for collections it is less suitable.
    – 0__
    Commented Nov 9, 2013 at 13:15
2

To me, this "Reactive Manifesto" is just a buzzword. Erlang is implementing the whole thing since the 80s for free -and silently-.

I would say it is easier to adhere to reactive principles in functional programming since FP is usually embracing immutable states and free side-effects functions. This means it is easier to implement concurrent, distributed and parallel systems.

Good luck implementing a "Reactive" system using shared states, threads, locks, semaphores... And you know, they say only two people can get Java concurrent system right, Doug Lea and Brian Goetz.

0

You may be interested to take a look at the Reactive Manifesto.

I think it's possible to write reactive applications with OO language (by ex. Java as NIO2 and Netty web server) but it is far more appropriate to use a functional language.

1
  • I sign up. I think the principles and quality attributes that they present as being the keys to reactive programming are valid. My main concern is that to achive this level of resiliance, and paralellism and fault tolerance, the "only" way is through functional programming. Commented Nov 9, 2013 at 15:39
0

No it is not both are independent programming paradigms, reactive being stream based approach and functional based on the philosophy of pure functions for all.

Not the answer you're looking for? Browse other questions tagged or ask your own question.