Skip to content

Commit f5ac0c2

Browse files
committed
Add utility to create an iterator pipeline
1 parent 0c762f9 commit f5ac0c2

File tree

8 files changed

+938
-0
lines changed

8 files changed

+938
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2019 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# iterPipeline
22+
23+
> Create an [iterator][mdn-iterator-protocol] pipeline.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var iterPipeline = require( '@stdlib/iter/pipeline' );
41+
```
42+
43+
#### iterPipeline( iterFcn0\[, ...iterFcn] )
44+
45+
Returns an [iterator][mdn-iterator-protocol] pipeline.
46+
47+
```javascript
48+
var array2iterator = require( '@stdlib/array/to-iterator' );
49+
var iterThunk = require( '@stdlib/iter/pipeline-thunk' );
50+
var iterHead = require( '@stdlib/iter/head' );
51+
var iterSome = require( '@stdlib/iter/some' );
52+
53+
// Convert iterator functions to unary functions which accept an iterator:
54+
var it1 = iterThunk( iterHead, 5 );
55+
var it2 = iterThunk( iterSome, 3 );
56+
57+
// Create an iterator pipeline:
58+
var p = iterPipeline( it1, it2 );
59+
60+
// Create a source iterator:
61+
var arr = array2iterator( [ 0, 0, 1, 1, 1, 0, 0, 0, 1, 1 ] );
62+
63+
// Provide the source iterator to our iterator pipeline:
64+
var bool = p( arr );
65+
// returns true
66+
67+
// Create a new source iterator:
68+
arr = array2iterator( [ 0, 0, 1, 0, 1, 0, 0, 0, 1, 1 ] );
69+
70+
// Run the pipeline for the new source iterator:
71+
bool = p( arr );
72+
// returns false
73+
```
74+
75+
The function accepts [iterator][mdn-iterator-protocol] functions provided as separate arguments **or** as a single argument consisting of an array of [iterator][mdn-iterator-protocol] functions.
76+
77+
```javascript
78+
var array2iterator = require( '@stdlib/array/to-iterator' );
79+
var iterThunk = require( '@stdlib/iter/pipeline-thunk' );
80+
var iterHead = require( '@stdlib/iter/head' );
81+
var iterSome = require( '@stdlib/iter/some' );
82+
83+
var it1 = iterThunk( iterHead, 5 );
84+
var it2 = iterThunk( iterSome, 3 );
85+
86+
var p = iterPipeline( [ it1, it2 ] );
87+
88+
var arr = array2iterator( [ 0, 0, 1, 1, 1, 0, 0, 0, 1, 1 ] );
89+
var bool = p( arr );
90+
// returns true
91+
92+
arr = array2iterator( [ 0, 0, 1, 0, 1, 0, 0, 0, 1, 1 ] );
93+
bool = p( arr );
94+
// returns false
95+
```
96+
97+
</section>
98+
99+
<!-- /.usage -->
100+
101+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
102+
103+
<section class="notes">
104+
105+
## Notes
106+
107+
- Within the context of an [iterator][mdn-iterator-protocol] pipeline (as defined by this function), an [iterator][mdn-iterator-protocol] function is defined as a **unary** function which accepts an [iterator][mdn-iterator-protocol] as its **only** argument.
108+
- Each [iterator][mdn-iterator-protocol] function, except the last [iterator][mdn-iterator-protocol] function, within an [iterator][mdn-iterator-protocol] pipeline must return an [iterator][mdn-iterator-protocol].
109+
- Starting from the left, each returned [iterator][mdn-iterator-protocol] is passed to the next [iterator][mdn-iterator-protocol] function.
110+
- The result of the last [iterator][mdn-iterator-protocol] function is the result of the pipeline.
111+
112+
</section>
113+
114+
<!-- /.notes -->
115+
116+
<!-- Package usage examples. -->
117+
118+
<section class="examples">
119+
120+
## Examples
121+
122+
<!-- eslint-disable function-paren-newline -->
123+
124+
<!-- eslint no-undef: "error" -->
125+
126+
```javascript
127+
var randu = require( '@stdlib/random/iter/randu' );
128+
var iterHead = require( '@stdlib/iter/head' );
129+
var iterMap = require( '@stdlib/iter/map' );
130+
var iterSome = require( '@stdlib/iter/some' );
131+
var iterThunk = require( '@stdlib/iter/pipeline-thunk' );
132+
var iterPipeline = require( '@stdlib/iter/pipeline' );
133+
134+
function threshold( r ) {
135+
return ( r > 0.95 );
136+
}
137+
138+
// Create a pipeline which tests whether at least 5% of values exceed a threshold:
139+
var p = iterPipeline(
140+
// Apply a threshold to iterated values:
141+
iterThunk( iterMap, threshold ),
142+
143+
// Limit the sequence to 100 values:
144+
iterThunk( iterHead, 100 ),
145+
146+
// Test whether at least 5 values exceed the threshold:
147+
iterThunk( iterSome, 5 )
148+
);
149+
150+
// Define the number of random number sequences to analyze:
151+
var N = 100;
152+
153+
// Initialize a counter for sequences satisfying the 5% threshold:
154+
var count = 0;
155+
156+
// Perform analysis...
157+
var bool;
158+
var i;
159+
for ( i = 0; i < N; i++ ) {
160+
bool = p( randu() );
161+
if ( bool ) {
162+
count += 1;
163+
}
164+
console.log( bool );
165+
}
166+
console.log( '%d of %d', count, N );
167+
```
168+
169+
</section>
170+
171+
<!-- /.examples -->
172+
173+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
174+
175+
<section class="references">
176+
177+
</section>
178+
179+
<!-- /.references -->
180+
181+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
182+
183+
<section class="links">
184+
185+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
186+
187+
</section>
188+
189+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isFunction = require( '@stdlib/assert/is-function' );
25+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var randu = require( '@stdlib/random/iter/randu' );
27+
var iterHead = require( '@stdlib/iter/head' );
28+
var iterSomeBy = require( '@stdlib/iter/some-by' );
29+
var iterThunk = require( '@stdlib/iter/pipeline-thunk' );
30+
var pkg = require( './../package.json' ).name;
31+
var iterPipeline = require( './../lib' );
32+
33+
34+
// MAIN //
35+
36+
bench( pkg+'::arguments', function benchmark( b ) {
37+
var fcn;
38+
var it1;
39+
var it2;
40+
var i;
41+
42+
it1 = iterThunk( iterHead, 10 );
43+
it2 = iterThunk( iterSomeBy, 5, threshold );
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
fcn = iterPipeline( it1, it2 );
48+
if ( !isFunction( fcn ) ) {
49+
b.fail( 'should return a function' );
50+
}
51+
}
52+
b.toc();
53+
if ( !isFunction( fcn ) ) {
54+
b.fail( 'should return a function' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
59+
function threshold( r ) {
60+
return ( r > 0.95 );
61+
}
62+
});
63+
64+
bench( pkg+'::array', function benchmark( b ) {
65+
var args;
66+
var fcn;
67+
var it1;
68+
var it2;
69+
var i;
70+
71+
it1 = iterThunk( iterHead, 10 );
72+
it2 = iterThunk( iterSomeBy, 5, threshold );
73+
args = [ it1, it2 ];
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
fcn = iterPipeline( args );
78+
if ( !isFunction( fcn ) ) {
79+
b.fail( 'should return a function' );
80+
}
81+
}
82+
b.toc();
83+
if ( !isFunction( fcn ) ) {
84+
b.fail( 'should return a function' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
89+
function threshold( r ) {
90+
return ( r > 0.95 );
91+
}
92+
});
93+
94+
bench( pkg+'::pipeline_throughput', function benchmark( b ) {
95+
var bool;
96+
var fcn;
97+
var it0;
98+
var it1;
99+
var it2;
100+
var i;
101+
102+
it0 = randu(); // infinite iterator which can be reused for each run
103+
it1 = iterThunk( iterHead, 10 );
104+
it2 = iterThunk( iterSomeBy, 5, threshold );
105+
fcn = iterPipeline( it1, it2 );
106+
107+
b.tic();
108+
for ( i = 0; i < b.iterations; i++ ) {
109+
bool = fcn( it0 );
110+
if ( !isBoolean( bool ) ) {
111+
b.fail( 'should return a boolean' );
112+
}
113+
}
114+
b.toc();
115+
if ( !isBoolean( bool ) ) {
116+
b.fail( 'should return a boolean' );
117+
}
118+
b.pass( 'benchmark finished' );
119+
b.end();
120+
121+
function threshold( r ) {
122+
return ( r > 0.95 );
123+
}
124+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( iterFcn[, ...iterFcn] )
3+
Returns an iterator pipeline.
4+
5+
Parameters
6+
----------
7+
iterFcn: Function|Array
8+
Iterator function or an array of iterator functions.
9+
10+
...iterFcn: Function (optional)
11+
Iterator functions.
12+
13+
Returns
14+
-------
15+
fcn( src ): Function
16+
Iterator pipeline which accepts a single argument, a source iterator.
17+
18+
Examples
19+
--------
20+
> var it1 = {{alias:@stdlib/iter/pipeline-thunk}}( {{alias:@stdlib/iter/head}}, 100 );
21+
> function f( r ) { return ( r > 0.95 ); };
22+
> var it2 = {{alias:@stdlib/iter/pipeline-thunk}}( {{alias:@stdlib/iter/some-by}}, 5, f );
23+
> var p = {{alias}}( it1, it2 );
24+
> var bool = p( {{alias:@stdlib/random/iter/randu}}() )
25+
<boolean>
26+
27+
See Also
28+
--------
29+

0 commit comments

Comments
 (0)