|
| 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 | +# mskfilterMap |
| 22 | + |
| 23 | +> Apply a mask to a provided input array and return a new array after applying a mapping function. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var mskfilterMap = require( '@stdlib/array/base/mskfilter-map' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### mskfilterMap( x, mask, clbk\[, thisArg] ) |
| 34 | + |
| 35 | +Applies a mask to a provided input array and returns a new array after applying a mapping function. |
| 36 | + |
| 37 | +```javascript |
| 38 | +function clbk( value ) { |
| 39 | + return value * 2; |
| 40 | +} |
| 41 | + |
| 42 | +var x = [ 1, 2, 3, 4 ]; |
| 43 | +var m = [ 0, 1, 0, 1 ]; |
| 44 | + |
| 45 | +var y = mskfilterMap( x, m, clbk ); |
| 46 | +// returns [ 4, 8 ] |
| 47 | +``` |
| 48 | + |
| 49 | +The function supports the following parameters: |
| 50 | + |
| 51 | +- **x**: input array. |
| 52 | +- **mask**: mask array. |
| 53 | +- **clbk**: function to apply. |
| 54 | +- **thisArg**: applied function execution context (_optional_). |
| 55 | + |
| 56 | +To set the applied function's execution context, provide a `thisArg`. |
| 57 | + |
| 58 | +<!-- eslint-disable no-invalid-this --> |
| 59 | + |
| 60 | +```javascript |
| 61 | +function clbk( x ) { |
| 62 | + this.count += 1; |
| 63 | + return x; |
| 64 | +} |
| 65 | + |
| 66 | +var x = [ 1, 2, 3, 4 ]; |
| 67 | +var m = [ 1, 1, 0, 1 ]; |
| 68 | + |
| 69 | +var ctx = { |
| 70 | + 'count': 0 |
| 71 | +}; |
| 72 | + |
| 73 | +var y = mskfilterMap( x, m, clbk, ctx ); |
| 74 | +// returns [ 1, 2, 4 ] |
| 75 | + |
| 76 | +var v = ctx.count; |
| 77 | +// returns 3 |
| 78 | +``` |
| 79 | + |
| 80 | +The function **always** returns a new "generic" array. |
| 81 | + |
| 82 | +#### mskfilterMap.assign( x, mask, out, stride, offset, clbk\[, thisArg] ) |
| 83 | + |
| 84 | +Applies a mask and mapping function to a provided input array and assigns results to elements in a provided output array. |
| 85 | + |
| 86 | +```javascript |
| 87 | +function clbk( value ) { |
| 88 | + return value * 2; |
| 89 | +} |
| 90 | + |
| 91 | +var x = [ 1, 2, 3, 4 ]; |
| 92 | +var m = [ 0, 1, 0, 1 ]; |
| 93 | + |
| 94 | +var out = [ 0, 0, 0, 0 ]; |
| 95 | +var arr = mskfilterMap.assign( x, m, out, -2, out.length-1, clbk ); |
| 96 | +// returns [ 0, 8, 0, 4 ] |
| 97 | + |
| 98 | +var bool = ( arr === out ); |
| 99 | +// returns true |
| 100 | +``` |
| 101 | + |
| 102 | +The function supports the following parameters: |
| 103 | + |
| 104 | +- **x**: input array. |
| 105 | +- **mask**: mask array. |
| 106 | +- **out**: output array. |
| 107 | +- **stride**: output array stride. |
| 108 | +- **offset**: output array offset. |
| 109 | +- **clbk**: function to apply. |
| 110 | +- **thisArg**: applied function execution context (_optional_). |
| 111 | + |
| 112 | +</section> |
| 113 | + |
| 114 | +<!-- /.usage --> |
| 115 | + |
| 116 | +<section class="notes"> |
| 117 | + |
| 118 | +## Notes |
| 119 | + |
| 120 | +- If a `mask` array element is truthy, the corresponding element in `x` is **included** in the output array; otherwise, the corresponding element in `x` is "masked" and thus **excluded** from the output array. |
| 121 | + |
| 122 | +</section> |
| 123 | + |
| 124 | +<!-- /.notes --> |
| 125 | + |
| 126 | +<section class="examples"> |
| 127 | + |
| 128 | +## Examples |
| 129 | + |
| 130 | +<!-- eslint no-undef: "error" --> |
| 131 | + |
| 132 | +```javascript |
| 133 | +var zeroTo = require( '@stdlib/array/base/zero-to' ); |
| 134 | +var bernoulli = require( '@stdlib/random/array/bernoulli' ); |
| 135 | +var abs2 = require( '@stdlib/math/base/special/abs2' ); |
| 136 | +var mskfilterMap = require( '@stdlib/array/base/mskfilter-map' ); |
| 137 | + |
| 138 | +// Generate a linearly spaced array: |
| 139 | +var x = zeroTo( 20 ); |
| 140 | +console.log( x ); |
| 141 | + |
| 142 | +// Generate a random mask: |
| 143 | +var mask = bernoulli( x.length, 0.5, { |
| 144 | + 'dtype': 'generic' |
| 145 | +}); |
| 146 | +console.log( mask ); |
| 147 | + |
| 148 | +// Filter an array using the mask: |
| 149 | +var y = mskfilterMap( x, mask, abs2 ); |
| 150 | +console.log( y ); |
| 151 | +``` |
| 152 | + |
| 153 | +</section> |
| 154 | + |
| 155 | +<!-- /.examples --> |
| 156 | + |
| 157 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 158 | + |
| 159 | +<section class="related"> |
| 160 | + |
| 161 | +</section> |
| 162 | + |
| 163 | +<!-- /.related --> |
| 164 | + |
| 165 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 166 | + |
| 167 | +<section class="links"> |
| 168 | + |
| 169 | +</section> |
| 170 | + |
| 171 | +<!-- /.links --> |
0 commit comments