<B, A>(b: B, f: (b: B) => Option.Option<readonly [A, B]>): Array<A>Builds an array by repeatedly applying a function to a seed value. The
function returns Option.some([element, nextSeed]) to continue, or
Option.none() to stop.
Example (Generating a sequence)
import { Array, Option } from "effect"
console.log(Array.unfold(1, (n) => n <= 5 ? Option.some([n, n + 1]) : Option.none()))
// [1, 2, 3, 4, 5]export const const unfold: <B, A>(
b: B,
f: (b: B) => Option.Option<readonly [A, B]>
) => Array<A>
Builds an array by repeatedly applying a function to a seed value. The
function returns Option.some([element, nextSeed]) to continue, or
Option.none() to stop.
Example (Generating a sequence)
import { Array, Option } from "effect"
console.log(Array.unfold(1, (n) => n <= 5 ? Option.some([n, n + 1]) : Option.none()))
// [1, 2, 3, 4, 5]
unfold = <function (type parameter) B in <B, A>(b: B, f: (b: B) => Option.Option<readonly [A, B]>): Array<A>B, function (type parameter) A in <B, A>(b: B, f: (b: B) => Option.Option<readonly [A, B]>): Array<A>A>(b: Bb: function (type parameter) B in <B, A>(b: B, f: (b: B) => Option.Option<readonly [A, B]>): Array<A>B, f: (b: B) => Option.Option<readonly [A, B]>f: (b: Bb: function (type parameter) B in <B, A>(b: B, f: (b: B) => Option.Option<readonly [A, B]>): Array<A>B) => import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<readonly [function (type parameter) A in <B, A>(b: B, f: (b: B) => Option.Option<readonly [A, B]>): Array<A>A, function (type parameter) B in <B, A>(b: B, f: (b: B) => Option.Option<readonly [A, B]>): Array<A>B]>): interface Array<T>Array<function (type parameter) A in <B, A>(b: B, f: (b: B) => Option.Option<readonly [A, B]>): Array<A>A> => {
const const out: A[]out: interface Array<T>Array<function (type parameter) A in <B, A>(b: B, f: (b: B) => Option.Option<readonly [A, B]>): Array<A>A> = []
let let next: Bnext: function (type parameter) B in <B, A>(b: B, f: (b: B) => Option.Option<readonly [A, B]>): Array<A>B = b: Bb
while (true) {
const const o: Option.Option<readonly [A, B]>o = f: (b: B) => Option.Option<readonly [A, B]>f(let next: Bnext)
if (import OptionOption.const isNone: <A>(
self: Option<A>
) => self is None<A>
Checks whether an Option is None (absent).
When to use
Use when you need to branch on an absent Option before accessing .value.
Details
- Acts as a type guard, narrowing to
None<A>
Example (Checking for None)
import { Option } from "effect"
console.log(Option.isNone(Option.some(1)))
// Output: false
console.log(Option.isNone(Option.none()))
// Output: true
isNone(const o: Option.Option<readonly [A, B]>o)) {
break
}
const [const a: Aa, const b: Bb] = const o: Option.Some<readonly [A, B]>const o: {
_tag: "Some";
_op: "Some";
value: A;
valueOrUndefined: A;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
o.Some<readonly [A, B]>.value: readonly [A, B](property) Some<readonly [A, B]>.value: {
0: A;
1: B;
length: 2;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<B | A>>): Array<B | A>; (...items: Array<B | A | ConcatArray<B | A>>): Array<B | A> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<B | A>;
indexOf: (searchElement: B | A, fromIndex?: number) => number;
lastIndexOf: (searchElement: B | A, fromIndex?: number) => number;
every: { (predicate: (value: B | A, index: number, array: ReadonlyArray<B | A>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: B | A, index: number, array: ReadonlyArray<B | A>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: B | A, index: number, array: ReadonlyArray<B | A>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: B | A, index: number, array: ReadonlyArray<B | A>) => void, thisArg?: any) => void;
map: (callbackfn: (value: B | A, index: number, array: ReadonlyArray<B | A>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: B | A, index: number, array: ReadonlyArray<B | A>) => value is S, thisArg?: any): Array<S>; (predicate: (value: B | A, index: number, array: ReadonlyArray<B | A>) => unknown, thisArg?: any): Array<B | A> };
reduce: { (callbackfn: (previousValue: B | A, currentValue: B | A, currentIndex: number, array: ReadonlyArray<B | A>) => B | A): B | A; (callbackfn: (previousValue: B | A, currentValue: B | A, currentIndex: number, array: ReadonlyArray<B | A>) => …;
reduceRight: { (callbackfn: (previousValue: B | A, currentValue: B | A, currentIndex: number, array: ReadonlyArray<B | A>) => B | A): B | A; (callbackfn: (previousValue: B | A, currentValue: B | A, currentIndex: number, array: ReadonlyArray<B | A>) => …;
find: { (predicate: (value: B | A, index: number, obj: ReadonlyArray<B | A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: B | A, index: number, obj: ReadonlyArray<B | A>) => unknown, thisArg?: any): B | A | undefined };
findIndex: (predicate: (value: B | A, index: number, obj: ReadonlyArray<B | A>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, B | A]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<B | A>;
includes: (searchElement: B | A, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: B | A, index: number, array: Array<B | A>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => B | A | undefined;
findLast: { (predicate: (value: B | A, index: number, array: ReadonlyArray<B | A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: B | A, index: number, array: ReadonlyArray<B | A>) => unknown, thisArg?: any): B | A | undefined };
findLastIndex: (predicate: (value: B | A, index: number, array: ReadonlyArray<B | A>) => unknown, thisArg?: any) => number;
toReversed: () => Array<B | A>;
toSorted: (compareFn?: ((a: B | A, b: B | A) => number) | undefined) => Array<B | A>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<B | A>): Array<B | A>; (start: number, deleteCount?: number): Array<B | A> };
with: (index: number, value: B | A) => Array<B | A>;
}
value
const out: A[]out.Array<A>.push(...items: A[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(const a: Aa)
let next: Bnext = const b: Bb
}
return const out: A[]out
}