Flyd: Reactive Functional Programming

2015-05-01 18:02:42 +0100 by Alex R. Young

When I see yet another reactive programming library, I think to myself "that's just not functional enough!" And the libraries that keep me interested in JavaScript are almost always pushing functional practices or static type checking.

Therefore I was interested to see Flyd (GitHub: paldepind/flyd, License: MIT, npm: flyd) by Simon Friis Vindum in my DailyJS inbox today. This project aims to model values that change over time using reactive and functional concepts without adding too much extra baggage.

Flyd is more functional than existing FRP libraries. Instead of methods it gives you curried functions with arguments in the order suitable for partial application. This gives more expressive power and modularity.

The building block in Flyd is streams. You can plug them in wherever a function is expected:

var clicks = flyd.stream();
document.getElementById('button').addEventListener('click', clicks);
var messages = flyd.stream();
webSocket.onmessage = messages;

Streams can depend on other streams, which is where the functional composition of Flyd comes in.

The author has provided some nice interactive examples that make the library easier to visualise. Take a look at the sum example to get started. And if you're interested in comparing to RxJS, then compare this Flyd code to functionally equivalent RxJS code.