|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2024 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 | +# iterCuAny |
| 22 | + |
| 23 | +> Create an [iterator][mdn-iterator-protocol] which cumulatively tests whether at least one iterated value is truthy. |
| 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 iterCuAny = require( '@stdlib/iter/cuany' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### iterCuAny( iterator ) |
| 44 | + |
| 45 | +Returns an [iterator][mdn-iterator-protocol] which cumulatively tests whether at least one iterated value is truthy. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 49 | + |
| 50 | +var arr = array2iterator( [ 0, 0, 0, 1, 0 ] ); |
| 51 | + |
| 52 | +var it = iterCuAny( arr ); |
| 53 | +// returns <Object> |
| 54 | + |
| 55 | +var v = it.next().value; |
| 56 | +// returns false |
| 57 | + |
| 58 | +v = it.next().value; |
| 59 | +// returns false |
| 60 | + |
| 61 | +v = it.next().value; |
| 62 | +// returns false |
| 63 | + |
| 64 | +v = it.next().value; |
| 65 | +// returns true |
| 66 | + |
| 67 | +v = it.next().value; |
| 68 | +// returns true |
| 69 | + |
| 70 | +var bool = it.next().done; |
| 71 | +// returns true |
| 72 | +``` |
| 73 | + |
| 74 | +The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: |
| 75 | + |
| 76 | +- **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the [iterator][mdn-iterator-protocol] is finished. |
| 77 | +- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. |
| 78 | + |
| 79 | +</section> |
| 80 | + |
| 81 | +<!-- /.usage --> |
| 82 | + |
| 83 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 84 | + |
| 85 | +<section class="notes"> |
| 86 | + |
| 87 | +</section> |
| 88 | + |
| 89 | +<!-- /.notes --> |
| 90 | + |
| 91 | +<!-- Package usage examples. --> |
| 92 | + |
| 93 | +<section class="examples"> |
| 94 | + |
| 95 | +## Examples |
| 96 | + |
| 97 | +<!-- eslint no-undef: "error" --> |
| 98 | + |
| 99 | +```javascript |
| 100 | +var randu = require( '@stdlib/random/iter/randu' ); |
| 101 | +var iterMap = require( '@stdlib/iter/map' ); |
| 102 | +var iterCuAny = require( '@stdlib/iter/cuany' ); |
| 103 | + |
| 104 | +function threshold( r ) { |
| 105 | + return ( r > 0.95 ); |
| 106 | +} |
| 107 | + |
| 108 | +// Create an iterator which generates uniformly distributed pseudorandom numbers: |
| 109 | +var opts = { |
| 110 | + 'iter': 100 |
| 111 | +}; |
| 112 | +var riter = randu( opts ); |
| 113 | + |
| 114 | +// Create an iterator which applies a threshold to generated numbers: |
| 115 | +var miter = iterMap( riter, threshold ); |
| 116 | + |
| 117 | +// Create an iterator which cumulatively tests whether at least one iterated value is truthy: |
| 118 | +var it = iterCuAny( miter ); |
| 119 | + |
| 120 | +// Perform manual iteration... |
| 121 | +var r; |
| 122 | +while ( true ) { |
| 123 | + r = it.next(); |
| 124 | + if ( r.done ) { |
| 125 | + break; |
| 126 | + } |
| 127 | + console.log( r.value ); |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +</section> |
| 132 | + |
| 133 | +<!-- /.examples --> |
| 134 | + |
| 135 | +<!-- 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. --> |
| 136 | + |
| 137 | +<section class="references"> |
| 138 | + |
| 139 | +</section> |
| 140 | + |
| 141 | +<!-- /.references --> |
| 142 | + |
| 143 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 144 | + |
| 145 | +<section class="related"> |
| 146 | + |
| 147 | +</section> |
| 148 | + |
| 149 | +<!-- /.related --> |
| 150 | + |
| 151 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 152 | + |
| 153 | +<section class="links"> |
| 154 | + |
| 155 | +[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol |
| 156 | + |
| 157 | +</section> |
| 158 | + |
| 159 | +<!-- /.links --> |
0 commit comments