What is Functional Reactive Programming?

What is Functional Reactive Programming?

TLDR: Functional Reactive Programming (FRP) is a Reactive Programming paradigm powered by Functional Programming.

Reactive Programming

Programming paradigm Subjective Objective
Proactive paradigm proactive passive
Reactive paradigm observable reactive

In (traditional) proactive programming, when a source changes, all its dependence is triggered to apply the changes.

class Proactive {
    constructor(passive){this.passive = passive}
	change(newState){
    	this.passive.apply(newState)
    }
}

With the new reactive paradigm, the objective has the responsibility to watch for changes from the subjective

class Objective{
	constructor(observable){
        this.observable = observable
        observable.addListener(this.apply)
    }
    destroy(){
        this.observable.removeListener(this.apply)
    }
    apply(newState){
        //watch for change from observable
    }
}

From the simple (pseudo-)code above, it can be observed that the reactive paradigm requires more boilerplate code. However, it exposes the possibility for the objective to be able to watch for changes actively.

Functional Programming enhancement

With the power of functional programming, the newState in the example in the previous part can be filtered, modified, chained, ... flexibly.

The Observable class can add generic function like

observable.map(state => !state) //reverse the new state whennever be notified
observable.filter(state => staate === 'disconnect') //ignore 'disconnect' state
Buy Me A Coffee