Add /arubinosky (3)
This commit is contained in:
@@ -5,3 +5,13 @@ export function tryOneOf<const T>(
|
||||
if (consts.includes(value)) return value;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function sample<T>(arr: T[], n: number = arr.length): T[] {
|
||||
if (n > arr.length) return sample(arr, arr.length);
|
||||
const copy = [...arr];
|
||||
for (let i = 0; i < n; i++) {
|
||||
const j = i + Math.floor(Math.random() * (copy.length - i));
|
||||
[copy[i], copy[j]] = [copy[j] as T, copy[i] as T];
|
||||
}
|
||||
return copy.slice(0, n);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user