-
-
Notifications
You must be signed in to change notification settings - Fork 400
/
index.d.ts
273 lines (250 loc) · 6.96 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// TypeScript Version: 2.0
/// <reference types="@stdlib/types"/>
import { ArrayLike } from '@stdlib/types/array';
/**
* Interface defining function options.
*/
interface Options {
/**
* Sample size.
*/
size?: number;
/**
* Element probabilities.
*/
probs?: Array<number>;
/**
* Boolean indicating whether to sample with replacement (default: true).
*/
replace?: boolean;
}
/**
* Interface defining population function options.
*/
interface PopOptions {
/**
* Sample size.
*/
size?: number;
/**
* Boolean indicating whether to sample with replacement (default: true).
*/
replace?: boolean;
/**
* Boolean indicating whether to mutate the `pool` when sampling without replacement (default: false).
*/
mutate?: boolean;
}
/**
* Interface defining `factory` options when `pool` is not supplied.
*/
interface FactoryOptions {
/**
* Integer-valued seed.
*/
seed?: number;
/**
* Sample size.
*/
size?: number;
/**
* Boolean indicating whether to sample with replacement (default: true).
*/
replace?: boolean;
}
/**
* Interface defining `factory` options when `pool` is supplied.
*/
interface FactoryPopOptions extends FactoryOptions {
/**
* Boolean indicating whether to mutate the `pool` when sampling without replacement (default: false).
*/
mutate?: boolean;
}
/**
* Samples elements from an array-like object.
*
* @param x - array-like object from which to sample elements
* @param options - function options
* @param options.size - sample size
* @param options.probs - element probabilities
* @param options.replace - boolean indicating whether to sample with replacement (default: true)
* @throws must provide valid options
* @throws `size` option must be less than or equal to the length of `x` when the `replace` option is `false`
* @returns sample
*/
type SampleFunction = ( x: ArrayLike<any>, options?: Options ) => Array<any>;
/**
* Samples elements from a population.
*
* @param options - function options
* @param options.size - sample size
* @param options.replace - boolean indicating whether to sample with replacement (default: true)
* @param options.mutate - boolean indicating whether to mutate the `pool` when sampling without replacement (default: false)
* @throws must provide valid options
* @throws `size` option must be less than or equal to the population when the `replace` option is `false`
* @returns sample
*/
type SamplePopFunction = ( options?: PopOptions ) => Array<any>;
/**
* Interface for sampling elements from an array-like object.
*/
interface Sample {
/**
* Samples elements from an array-like object.
*
* @param x - array-like object from which to sample
* @param options - function options
* @param options.size - sample size
* @param options.probs - element probabilities
* @param options.replace - boolean indicating whether to sample with replacement (default: true)
* @throws must provide valid options
* @throws `size` option must be less than or equal to the length of `x` when the `replace` option is `false`
* @returns sample
*
* @example
* var out = sample( [ 3, null, NaN, 'abc', function(){} ] );
* // e.g., returns [ 3, 'abc', null, 3, null ]
*/
( x: ArrayLike<any>, options?: Options ): Array<any>;
/**
* Returns a function to sample elements from an array-like object.
*
* @param pool - array-like object from which to sample
* @param options - function options
* @param options.seed - integer-valued seed
* @param options.size - sample size
* @param options.replace - boolean indicating whether to sample with replacement (default: true)
* @param options.mutate - boolean indicating whether to mutate the `pool` when sampling without replacement (default: false)
* @throws must provide valid options
* @returns function to sample elements from an array-like object
*
* @example
* var fcn = sample.factory( [ 1, 2, 3, 4, 5, 6 ], {
* 'seed': 232,
* 'size': 2
* });
* var out = fcn();
* // e.g., returns [ 6, 4 ]
*
* out = fcn();
* // e.g., returns [ 6, 5 ]
*
* @example
* var fcn = sample.factory( [ 1, 2, 3, 4, 5, 6 ], {
* 'seed': 474,
* 'size': 3,
* 'mutate': true,
* 'replace': false
* });
* var out = fcn();
* // e.g., returns [ 4, 3, 6 ]
*
* out = fcn();
* // e.g., returns [ 1, 5, 2 ]
*
* out = fcn();
* // returns null
*
* @example
* var fcn = sample.factory( [ 0, 1 ], {
* 'size': 2
* });
*
* var out = fcn();
* // e.g., returns [ 1, 1 ]
*
* out = fcn({
* 'size': 10
* });
* // e.g., returns [ 0, 1, 1, 1, 0, 1, 0, 0, 1, 1 ]
*
* @example
* var fcn = sample.factory( [ 0, 1 ], {
* 'size': 2
* });
*
* var out = fcn();
* // e.g., returns [ 1, 1 ]
*
* out = fcn({
* 'replace': false
* });
* // e.g., returns [ 0, 1 ] or [ 1, 0 ]
*
* out = fcn();
* // e.g., returns [ 1, 1 ]
*
* @example
* var fcn = sample.factory( [ 0, 1 ], {
* 'size': 2,
* 'mutate': true
* });
*
* var out = fcn();
* // e.g., returns [ 1, 1 ]
*
* out = fcn({
* 'replace': false
* });
* // e.g., returns [ 0, 1 ] or [ 1, 0 ]
*
* out = fcn();
* // returns null
*/
factory( pool: ArrayLike<any>, options?: FactoryPopOptions ): SamplePopFunction; // tslint-disable-line max-line-length
/**
* Returns a function to sample elements from an array-like object.
*
* @param options - function options
* @param options.seed - integer-valued seed
* @param options.size - sample size
* @param options.replace - boolean indicating whether to sample with replacement (default: true)
* @throws must provide valid options
* @returns function to sample elements from an array-like object
*
* @example
* var fcn = sample.factory({
* 'seed': 232
* });
* var out = fcn( 'abcdefg' );
* // e.g., returns [ 'g', 'd', 'g', 'f', 'c', 'e', 'f' ]
*/
factory( options?: FactoryOptions ): SampleFunction;
}
/**
* Samples elements from an array-like object.
*
* @param x - array-like object from which to sample
* @param options - function options
* @param options.size - sample size
* @param options.probs - element probabilities
* @param options.replace - boolean indicating whether to sample with replacement (default: true)
* @throws must provide valid options
* @throws `size` option must be less than or equal to the length of `x` when the `replace` option is `false`
* @returns sample
*
* @example
* var out = sample( [ 3, null, NaN, 'abc', function(){} ] );
* // e.g., returns [ 3, 'abc', null, 3, null ]
*/
declare var sample: Sample;
// EXPORTS //
export = sample;