iterPipeline

Create an iterator pipeline.

Usage

var iterPipeline = require( '@stdlib/iter/pipeline' );

iterPipeline( iterFcn0[, ...iterFcn] )

Returns an iterator pipeline.

var array2iterator = require( '@stdlib/array/to-iterator' );
var iterThunk = require( '@stdlib/iter/pipeline-thunk' );
var iterHead = require( '@stdlib/iter/head' );
var iterSome = require( '@stdlib/iter/some' );

// Convert iterator functions to unary functions which accept an iterator:
var it1 = iterThunk( iterHead, 5 );
var it2 = iterThunk( iterSome, 3 );

// Create an iterator pipeline:
var p = iterPipeline( it1, it2 );

// Create a source iterator:
var arr = array2iterator( [ 0, 0, 1, 1, 1, 0, 0, 0, 1, 1 ] );

// Provide the source iterator to our iterator pipeline:
var bool = p( arr );
// returns true

// Create a new source iterator:
arr = array2iterator( [ 0, 0, 1, 0, 1, 0, 0, 0, 1, 1 ] );

// Run the pipeline for the new source iterator:
bool = p( arr );
// returns false

The function accepts iterator functions provided as separate arguments or as a single argument consisting of an array of iterator functions.

var array2iterator = require( '@stdlib/array/to-iterator' );
var iterThunk = require( '@stdlib/iter/pipeline-thunk' );
var iterHead = require( '@stdlib/iter/head' );
var iterSome = require( '@stdlib/iter/some' );

var it1 = iterThunk( iterHead, 5 );
var it2 = iterThunk( iterSome, 3 );

var p = iterPipeline( [ it1, it2 ] );

var arr = array2iterator( [ 0, 0, 1, 1, 1, 0, 0, 0, 1, 1 ] );
var bool = p( arr );
// returns true

arr = array2iterator( [ 0, 0, 1, 0, 1, 0, 0, 0, 1, 1 ] );
bool = p( arr );
// returns false

Notes

  • Within the context of an iterator pipeline (as defined by this function), an iterator function is defined as a unary function which accepts an iterator as its only argument.
  • Each iterator function, except the last iterator function, within an iterator pipeline must return an iterator.
  • Starting from the left, each returned iterator is passed to the next iterator function.
  • The result of the last iterator function is the result of the pipeline.

Examples

var randu = require( '@stdlib/random/iter/randu' );
var iterHead = require( '@stdlib/iter/head' );
var iterMap = require( '@stdlib/iter/map' );
var iterSome = require( '@stdlib/iter/some' );
var iterThunk = require( '@stdlib/iter/pipeline-thunk' );
var iterPipeline = require( '@stdlib/iter/pipeline' );

function threshold( r ) {
    return ( r > 0.95 );
}

// Create a pipeline which tests whether at least 5% of values exceed a threshold:
var p = iterPipeline(
    // Apply a threshold to iterated values:
    iterThunk( iterMap, threshold ),

    // Limit the sequence to 100 values:
    iterThunk( iterHead, 100 ),

    // Test whether at least 5 values exceed the threshold:
    iterThunk( iterSome, 5 )
);

// Define the number of random number sequences to analyze:
var N = 100;

// Initialize a counter for sequences satisfying the 5% threshold:
var count = 0;

// Perform analysis...
var bool;
var i;
for ( i = 0; i < N; i++ ) {
    bool = p( randu() );
    if ( bool ) {
        count += 1;
    }
    console.log( bool );
}
console.log( '%d of %d', count, N );
Did you find this page helpful?