MCPcopy Index your code
hub / github.com/Effect-TS/effect / shuffleWith

Function shuffleWith

packages/effect/src/internal/stm/tRandom.ts:48–83  ·  view source on GitHub ↗
(
  iterable: Iterable<A>,
  nextIntBounded: (n: number) => STM.STM<number>
)

Source from the content-addressed store, hash-verified

46}
47
48const shuffleWith = <A>(
49 iterable: Iterable<A>,
50 nextIntBounded: (n: number) => STM.STM<number>
51): STM.STM<Array<A>> => {
52 const swap = (buffer: TArray.TArray<A>, index1: number, index2: number): STM.STM<void> =>
53 pipe(
54 buffer,
55 tArray.get(index1),
56 core.flatMap((tmp) =>
57 pipe(
58 buffer,
59 tArray.updateSTM(index1, () => pipe(buffer, tArray.get(index2))),
60 core.zipRight(
61 pipe(
62 buffer,
63 tArray.update(index2, () => tmp)
64 )
65 )
66 )
67 )
68 )
69 return pipe(
70 tArray.fromIterable(iterable),
71 core.flatMap((buffer) => {
72 const array: Array<number> = []
73 for (let i = array.length; i >= 2; i = i - 1) {
74 array.push(i)
75 }
76 return pipe(
77 array,
78 stm.forEach((n) => pipe(nextIntBounded(n), core.flatMap((k) => swap(buffer, n - 1, k))), { discard: true }),
79 core.zipRight(tArray.toArray(buffer))
80 )
81 })
82 )
83}
84
85/** @internal */
86export const Tag = Context.GenericTag<TRandom.TRandom>("effect/TRandom")

Callers 1

shuffleMethod · 0.70

Calls 2

swapFunction · 0.70
pipeFunction · 0.50

Tested by

no test coverage detected