<Self extends Iterable<unknown>>(
elements: Self
): Self extends NonEmptyIterable.NonEmptyIterable<infer A>
? Effect.Effect<A>
: Self extends Arr.NonEmptyReadonlyArray<infer A>
? Effect.Effect<A>
: Self extends Iterable<infer A>
? Effect.Effect<A, Cause.NoSuchElementError>
: neverGets a random element from an iterable.
When to use
Use to select one value uniformly from a collection using the active Random
service.
Details
If the input type is known to be non-empty, the returned effect cannot fail.
Otherwise, empty iterables fail with Cause.NoSuchElementError.
Example (Choosing a random value)
import { Effect, Random } from "effect"
const program = Effect.gen(function*() {
const value = yield* Random.choice(["red", "green", "blue"] as const)
console.log(value)
})export const const choice: <
Self extends Iterable<unknown>
>(
elements: Self
) => Self extends NonEmptyIterable.NonEmptyIterable<
infer A
>
? Effect.Effect<A>
: Self extends Arr.NonEmptyReadonlyArray<
infer A
>
? Effect.Effect<A>
: Self extends Iterable<infer A>
? Effect.Effect<A, Cause.NoSuchElementError>
: never
Gets a random element from an iterable.
When to use
Use to select one value uniformly from a collection using the active Random
service.
Details
If the input type is known to be non-empty, the returned effect cannot fail.
Otherwise, empty iterables fail with Cause.NoSuchElementError.
Example (Choosing a random value)
import { Effect, Random } from "effect"
const program = Effect.gen(function*() {
const value = yield* Random.choice(["red", "green", "blue"] as const)
console.log(value)
})
choice: <function (type parameter) Self in <Self extends Iterable<unknown>>(elements: Self): Self extends NonEmptyIterable.NonEmptyIterable<infer A> ? Effect.Effect<A> : Self extends Arr.NonEmptyReadonlyArray<infer A> ? Effect.Effect<A> : Self extends Iterable<infer A> ? Effect.Effect<A, Cause.NoSuchElementError> : neverSelf extends interface Iterable<T, TReturn = any, TNext = any>Iterable<unknown>>(
elements: Self extends Iterable<unknown>elements: function (type parameter) Self in <Self extends Iterable<unknown>>(elements: Self): Self extends NonEmptyIterable.NonEmptyIterable<infer A> ? Effect.Effect<A> : Self extends Arr.NonEmptyReadonlyArray<infer A> ? Effect.Effect<A> : Self extends Iterable<infer A> ? Effect.Effect<A, Cause.NoSuchElementError> : neverSelf
) => function (type parameter) Self in <Self extends Iterable<unknown>>(elements: Self): Self extends NonEmptyIterable.NonEmptyIterable<infer A> ? Effect.Effect<A> : Self extends Arr.NonEmptyReadonlyArray<infer A> ? Effect.Effect<A> : Self extends Iterable<infer A> ? Effect.Effect<A, Cause.NoSuchElementError> : neverSelf extends import NonEmptyIterableNonEmptyIterable.type NonEmptyIterable.NonEmptyIterable = /*unresolved*/ anyNonEmptyIterable<infer function (type parameter) AA> ? import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) AA>
: function (type parameter) Self in <Self extends Iterable<unknown>>(elements: Self): Self extends NonEmptyIterable.NonEmptyIterable<infer A> ? Effect.Effect<A> : Self extends Arr.NonEmptyReadonlyArray<infer A> ? Effect.Effect<A> : Self extends Iterable<infer A> ? Effect.Effect<A, Cause.NoSuchElementError> : neverSelf extends import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<infer function (type parameter) AA> ? import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) AA>
: function (type parameter) Self in <Self extends Iterable<unknown>>(elements: Self): Self extends NonEmptyIterable.NonEmptyIterable<infer A> ? Effect.Effect<A> : Self extends Arr.NonEmptyReadonlyArray<infer A> ? Effect.Effect<A> : Self extends Iterable<infer A> ? Effect.Effect<A, Cause.NoSuchElementError> : neverSelf extends interface Iterable<T, TReturn = any, TNext = any>Iterable<infer function (type parameter) AA> ? import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) AA, import CauseCause.type Cause.NoSuchElementError = /*unresolved*/ anyNoSuchElementError>
: never = ((elements: Iterable<unknown>elements: interface Iterable<T, TReturn = any, TNext = any>Iterable<unknown>) => {
const const buffer: unknown[]buffer = var Array: ArrayConstructorArray.ArrayConstructor.from<unknown>(iterable: Iterable<unknown> | ArrayLike<unknown>): unknown[] (+3 overloads)Creates an array from an iterable object.
from(elements: Iterable<unknown>elements)
return const buffer: unknown[]buffer.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length === 0
? import EffectEffect.fail(new import CauseCause.NoSuchElementError("Cannot select a random element from an empty array"))
: const randomWith: <A>(
f: (random: (typeof Random)["Service"]) => A
) => Effect.Effect<A>
randomWith((r: Context.Reference<{
nextIntUnsafe(): number
nextDoubleUnsafe(): number
}>
(parameter) r: {
nextIntUnsafe: () => number;
nextDoubleUnsafe: () => number;
}
r) => const buffer: unknown[]buffer[var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.min(...values: number[]): numberReturns the smaller of a set of supplied numeric expressions.
min(const buffer: unknown[]buffer.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length - 1, var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.floor(x: number): numberReturns the greatest integer less than or equal to its numeric argument.
floor(r: Context.Reference<{
nextIntUnsafe(): number
nextDoubleUnsafe(): number
}>
(parameter) r: {
nextIntUnsafe: () => number;
nextDoubleUnsafe: () => number;
}
r.nextDoubleUnsafe() * const buffer: unknown[]buffer.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length))]!)
}) as any