Beams: Streams with Computation

Alex R. Young





node modules npm gbnc

Beams: Streams with Computation

Posted by Alex R. Young on .

node modules npm gbnc

Beams: Streams with Computation

Posted by Alex R. Young on .

Beam (GitHub: darach / beam-js, License: MIT, npm: beam) by Darach Ennis is a module for performing computation on streamed data.

Beams are like streams, in that they support pipes but are lightweight and work with in memory types without buffering, copying, parsing.

Beams have some interesting properties:

  • Events are executed in order
  • Events can be filtered
  • Beams can be branched and joined together


I've taken one of Darach's basic examples and interleaved it with comments to explain the major concepts. The goal is to pipe numbers through an operator that can square numbers.

First, load the module and create a sink.

var Beam = require('beam');  
var sink = Beam.Sink();

sink.on('data', function(data) {  
  console.log('d:', data);

Sinks emit a 'data' event whenever data is received, so they can be used to capture results. Operators can be used to transform data -- this example just squares each value:

var square = Beam.Operator.transform(function(x) { return x * x; });  

Sources can be connected to sinks with pipe, much like streams:

var source = Beam.Source();  

Values can be sent to a source with push:

for (var i = 0; i <= 10; i++) {  

This example introduces sinks, sources, and operators, but it doesn't even scratch the surface of what Darach is aiming for with Beams. The next step is to define a filter and declare a pipe that uses it:

var even = Beam.Operator.filter(function(x) { return x % 2 == 0; });  

Now only even numbers will be squared. The Beams module also has built-in functions, which you can find in Beam.bifs. These are operators and filters that you can use to filter and compare values in beams:

var mod2 = source.pipe(b.mod(2))  

To see all of the defined Bifs, see _beam_bifs.js.