Skip to content

Fluent Interface

kgryte edited this page May 12, 2015 · 1 revision

For data pipelines, invoking serial methods can become verbose.

data = compute.roundn( data, -3 );
data = compute.mean( data );
data = compute.roundn( data, 0 );
...

Fluent interfaces can help alleviate this problem. Such interfaces have been popularized by libraries such as jQuery and D3 which utilize method chaining.

To create a fluent interface,

var flow = compute.flow();

A flow pipeline should be initialized.

flow.value( data );

Once initialized, all compute methods are now available. The lone difference is that data should not be explicitly passed as an argument. For example,

flow
	.value( data )
	.roundn( -3 )
	.mean()
	.roundn( 0 );

To return the flow value,

var mean = flow.value();

To help understand the transformations comprising a data pipeline, flow exposes an inspect() method, which logs the current value to the console while maintaining the fluent interface.

flow.inspect();

The above flow can be modified accordingly,

flow
	.value( data )
	.inspect()
	.roundn( -3 )
	.inspect()
	.mean()
	.inspect()
	.roundn( 0 )
	.inspect();

To summarize the flow API...

flow.value( [value] )

This method is a setter/getter. If no value is provided, returns the current flow value. If a value is provided, sets the flow value.

flow.value( [ 4, 3, 6, 2 ] );

flow.inspect()

Logs the current flow value to the console, while maintaining the fluent interface.

flow.inspect();

Notes

  1. When creating flows, ensure that the output from one computation matches the input argument requirements for the next computation.
  2. For large datasets, rather than loading datasets into memory, consider using file streams and utilize stream tools such as Flow.io.
Clone this wiki locally