FuncType - v0.8.85
    Preparing search index...

    Variable LazyType

    LazyType: <T extends unknown>(thunk: () => T) => LazyType<T> & {
        fail: <T extends unknown>(error: unknown) => LazyType<T>;
        fromEither: <E, T extends unknown>(either: Either<E, T>) => LazyType<T>;
        fromOption: <T extends unknown>(
            option: Option<T>,
            defaultThunk: () => T,
        ) => LazyType<T>;
        fromPromise: <T extends unknown>(promise: Promise<T>) => LazyType<T>;
        fromTry: <T extends unknown>(tryValue: Try<T>) => LazyType<T>;
        fromValue: <T extends unknown>(value: T) => LazyType<T>;
        of: <T extends unknown>(thunk: () => T) => LazyType<T>;
    }

    Creates a Lazy computation that defers evaluation until needed. Results are memoized after first evaluation.

    Type declaration

      • <T extends unknown>(thunk: () => T): LazyType<T>
      • Internal constructor for creating Lazy instances

        Type Parameters

        • T extends unknown

        Parameters

        • thunk: () => T

        Returns LazyType<T>

    • fail: <T extends unknown>(error: unknown) => LazyType<T>

      Creates a failed Lazy that will throw when evaluated

    • fromEither: <E, T extends unknown>(either: Either<E, T>) => LazyType<T>

      Creates a Lazy from an Either

    • fromOption: <T extends unknown>(option: Option<T>, defaultThunk: () => T) => LazyType<T>

      Creates a Lazy from an Option

    • fromPromise: <T extends unknown>(promise: Promise<T>) => LazyType<T>

      Creates a Lazy that will throw an error since promises need to be awaited first

    • fromTry: <T extends unknown>(tryValue: Try<T>) => LazyType<T>

      Creates a Lazy from a Try

    • fromValue: <T extends unknown>(value: T) => LazyType<T>

      Creates a Lazy from an immediate value

    • of: <T extends unknown>(thunk: () => T) => LazyType<T>

      Creates a Lazy from a thunk (deferred computation)

    // Basic lazy evaluation
    const expensive = Lazy(() => {
    console.log("Computing...")
    return 42
    })
    // Nothing printed yet
    const result = expensive.get() // Prints "Computing..." and returns 42
    const cached = expensive.get() // Returns 42 without printing
    // Error handling
    const risky = Lazy(() => {
    if (Math.random() > 0.5) throw new Error("Failed")
    return "Success"
    })
    const safe = risky.getOrElse("Default") // Returns "Success" or "Default"
    const option = risky.toOption() // Some("Success") or None
    const either = risky.toEither() // Right("Success") or Left(Error)
    // Chaining computations
    const result = Lazy(() => 10)
    .map(x => x * 2)
    .flatMap(x => Lazy(() => x + 5))
    .recover(err => 0)
    .get() // 25
    // Integration with functype
    const userOption = Option({ id: 1, name: "Alice" })
    const userName = Lazy.fromOption(userOption, () => ({ id: 0, name: "Anonymous" }))
    .map(user => user.name)
    .get() // "Alice"