Add insert to tools

This commit is contained in:
Jon Staab
2024-11-20 10:02:38 -08:00
parent 75e0d0603b
commit ca00081b2b
+2
View File
@@ -290,6 +290,8 @@ export const hash = (s: string) =>
export const splitAt = <T>(n: number, xs: T[]) => [xs.slice(0, n), xs.slice(n)]
export const insert = <T>(n: number, x: T, xs: T[]) => [...xs.slice(0, n), x, ...xs.slice(n)]
export const choice = <T>(xs: T[]): T => xs[Math.floor(xs.length * Math.random())]
export const shuffle = <T>(xs: Iterable<T>): T[] => Array.from(xs).sort(() => Math.random() > 0.5 ? 1 : -1)