| 86 | export const Tag = Context.GenericTag<TRandom.TRandom>("effect/TRandom") |
| 87 | |
| 88 | class TRandomImpl implements TRandom.TRandom { |
| 89 | readonly [TRandomTypeId]: TRandom.TRandomTypeId = TRandomTypeId |
| 90 | constructor(readonly state: TRef.TRef<Random.PCGRandomState>) { |
| 91 | this.next = withState(this.state, randomNumber) |
| 92 | this.nextBoolean = core.flatMap(this.next, (n) => core.succeed(n > 0.5)) |
| 93 | this.nextInt = withState(this.state, randomInteger) |
| 94 | } |
| 95 | |
| 96 | next: STM.STM<number> |
| 97 | nextBoolean: STM.STM<boolean> |
| 98 | nextInt: STM.STM<number> |
| 99 | |
| 100 | nextRange(min: number, max: number): STM.STM<number> { |
| 101 | return core.flatMap(this.next, (n) => core.succeed((max - min) * n + min)) |
| 102 | } |
| 103 | nextIntBetween(low: number, high: number): STM.STM<number> { |
| 104 | return withState(this.state, randomIntegerBetween(low, high)) |
| 105 | } |
| 106 | shuffle<A>(elements: Iterable<A>): STM.STM<Array<A>> { |
| 107 | return shuffleWith(elements, (n) => this.nextIntBetween(0, n)) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** @internal */ |
| 112 | export const live: Layer.Layer<TRandom.TRandom> = Layer.effect( |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…