Understanding Streams Through Implementation

A quick guide to the inner workings of Streams in JS

Ross
The Startup

--

Since functional programming has been taking the main stage in programming history (again, LISP predates C), functional concepts have become popularly implemented in JavaScript. Luckily, JS is near perfectly suited to accept these functional ideologies. One such functional concept that’s gained a huge amount of traction in the JS community is the idea of Streams. RXJS calls them Observables, others will refer to them as Signals. Any way you put it, a stream is just a representation of a set of data over time. It’s like a collection, but unlike collections, which are consumed ‘eagerly’ (all at once), streams are ‘lazy’. After a stream is subscribed to, it will give us its data as it arrives, rather than making us request it or processing it all in one chunk. The advantages of this situation is that Streams are reactive and declarative — you define what will happen when data arrives in just one place in your program, and then you don’t have to care about responding to that data elsewhere. Another advantage that streams have over collections is that they can represent infinite sets — an eagerly evaluated collection could never do that, it would crash your program by overflowing the stack.

--

--

Ross
The Startup

Programming maniac, #JavaScript zealot. I'm crazy about #FunctionalProgramming and I love Rust. ETH coffee fund: 0x0c37584674e7143e03328254232102973a9cd468