Lazy overview

Whilst the definition of Lazy happens to be the same as IO, they represent different intentions, specifically with respect to Lazy representing a pure thunk.

Thinking in terms of Haskell, Lazy can be considered equivalent to a pure function that takes () (unit) as input.

Added in v0.12.0


Table of contents


0 Types

Lazy (type alias)

Re-exported from fp-ts for convenience.

Signature

export type Lazy<A> = () => A
type Lazy a = () -> a

Added in v0.12.0

1 Typeclass Instances

Applicative

Formal Applicative instance for Lazy to be provided to higher-kinded functions that require it.

Signature

export declare const Applicative: Applicative1<'Lazy'>
Applicative :: Applicative1 "Lazy"

Added in v0.12.0

Apply

Formal Apply instance for Lazy to be provided to higher-kinded functions that require it.

Signature

export declare const Apply: Apply1<'Lazy'>
Apply :: Apply1 "Lazy"

Added in v0.12.0

Chain

Formal Chain instance for Lazy to be provided to higher-kinded functions that require it.

Signature

export declare const Chain: Chain1<'Lazy'>
Chain :: Chain1 "Lazy"

Added in v0.12.0

ChainRec

Formal ChainRec instance for Lazy to be provided to higher-kinded functions that require it.

Signature

export declare const ChainRec: ChainRec1<'Lazy'>
ChainRec :: ChainRec1 "Lazy"

Added in v0.12.0

Functor

Formal Functor instance for Lazy to be provided to higher-kinded functions that require it.

Signature

export declare const Functor: Functor1<'Lazy'>
Functor :: Functor1 "Lazy"

Added in v0.12.0

Monad

Formal Monad instance for Lazy to be provided to higher-kinded functions that require it.

Signature

export declare const Monad: Monad1<'Lazy'>
Monad :: Monad1 "Lazy"

Added in v0.12.0

Pointed

Formal Pointed instance for Lazy to be provided to higher-kinded functions that require it.

Signature

export declare const Pointed: Pointed1<'Lazy'>
Pointed :: Pointed1 "Lazy"

Added in v0.12.0

2 Typeclass Methods

ApT

Identity for Lazy as applied to sequenceT.

Signature

export declare const ApT: Lazy<readonly []>
ApT :: Lazy ([])

Added in v0.12.0

Do

Initiate do notation in the context of Lazy.

Signature

export declare const Do: Lazy<{}>
Do :: Lazy {}

Added in v0.12.0

ap

Apply a function within a Lazy.

Signature

export declare const ap: <A>(fa: Lazy<A>) => <B>(fab: Lazy<(a: A) => B>) => Lazy<B>
ap :: Lazy a -> Lazy (a -> b) -> Lazy b

Added in v0.12.0

apFirst

Sequence actions, discarding the value of the first argument.

Signature

export declare const apFirst: <B>(second: Lazy<B>) => <A>(first: Lazy<A>) => Lazy<A>
apFirst :: Lazy b -> Lazy a -> Lazy a

Added in v0.12.0

apS

Bind the provided value to the specified key in do notation.

Signature

export declare const apS: <N, A, B>(
  name: Exclude<N, keyof A>,
  fb: Lazy<B>
) => (fa: Lazy<A>) => Lazy<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
apS :: (Exclude n (keyof a), Lazy b) -> Lazy a -> Lazy { [k in (n | (keyof a))]: k extends (keyof a) ? a[k] : b }

Added in v0.12.0

apSecond

Sequence actions, discarding the value of the second argument.

Signature

export declare const apSecond: <B>(second: Lazy<B>) => <A>(first: Lazy<A>) => Lazy<B>
apSecond :: Lazy b -> Lazy a -> Lazy b

Added in v0.12.0

bind

Bind the output of the provided function to the specified key in do notation.

Signature

export declare const bind: <N, A, B>(
  name: Exclude<N, keyof A>,
  f: (a: A) => Lazy<B>
) => (ma: Lazy<A>) => Lazy<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
bind :: (Exclude n (keyof a), (a -> Lazy b)) -> Lazy a -> Lazy { [k in (n | (keyof a))]: k extends (keyof a) ? a[k] : b }

Added in v0.12.0

bindTo

Bind the provided value, typically preceding it in a pipeline, to the specified key in do notation.

Signature

export declare const bindTo: <N>(name: N) => <A>(fa: Lazy<A>) => Lazy<{ readonly [K in N]: A }>
bindTo :: n -> Lazy a -> Lazy { [k in n]: a }

Added in v0.12.0

chain

Map and flatten the output of a Lazy.

Signature

export declare const chain: <A, B>(f: (a: A) => Lazy<B>) => (ma: Lazy<A>) => Lazy<B>
chain :: (a -> Lazy b) -> Lazy a -> Lazy b

Added in v0.12.0

chainFirst

Like chain, but discards the new output.

Signature

export declare const chainFirst: <A, _>(f: (a: A) => Lazy<_>) => (first: Lazy<A>) => Lazy<A>
chainFirst :: (a -> Lazy _) -> Lazy a -> Lazy a

Added in v0.12.0

flap

Takes a function in a functorial Lazy context and applies it to an ordinary value.

Signature

export declare const flap: <A>(a: A) => <B>(fab: Lazy<(a: A) => B>) => Lazy<B>
flap :: a -> Lazy (a -> b) -> Lazy b

Added in v0.12.0

flatMap

Alias of chain.

Signature

export declare const flatMap: <A, B>(f: (a: A) => Lazy<B>) => (ma: Lazy<A>) => Lazy<B>
flatMap :: (a -> Lazy b) -> Lazy a -> Lazy b

Added in v0.17.0

flatten

Flatten a nested Lazy.

Signature

export declare const flatten: <A>(mma: Lazy<Lazy<A>>) => Lazy<A>
flatten :: Lazy (Lazy a) -> Lazy a

Added in v0.12.0

let

Assign a variable in do notation.

Signature

export declare const let: <N, A, B>(
  name: Exclude<N, keyof A>,
  f: (a: A) => B
) => (fa: Lazy<A>) => Lazy<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
let :: (Exclude n (keyof a), (a -> b)) -> Lazy a -> Lazy { [k in (n | (keyof a))]: k extends (keyof a) ? a[k] : b }

Added in v0.17.0

map

Map the output of a Lazy.

Signature

export declare const map: <A, B>(f: (x: A) => B) => (fa: Lazy<A>) => Lazy<B>
map :: (a -> b) -> Lazy a -> Lazy b

Added in v0.12.0

of

Raise any value to a Lazy.

Signature

export declare const of: <A>(a: A) => Lazy<A>
of :: a -> Lazy a

Added in v0.12.0

sequenceArray

Equivalent to Array#sequence(Applicative).

Signature

export declare const sequenceArray: <A>(arr: readonly Lazy<A>[]) => Lazy<readonly A[]>
sequenceArray :: Array (Lazy a) -> Lazy (Array a)

Added in v2.9.0

traverseArray

Equivalent to Array#traverse(Applicative).

Signature

export declare const traverseArray: <A, B>(f: (a: A) => Lazy<B>) => (as: readonly A[]) => Lazy<readonly B[]>
traverseArray :: (a -> Lazy b) -> Array a -> Lazy (Array b)

Added in v0.12.0

traverseArrayWithIndex

Equivalent to Array#traverseWithIndex(Applicative).

Signature

export declare const traverseArrayWithIndex: <A, B>(
  f: (index: number, a: A) => Lazy<B>
) => (as: readonly A[]) => Lazy<readonly B[]>
traverseArrayWithIndex :: ((number, a) -> Lazy b) -> Array a -> Lazy (Array b)

Added in v0.12.0

traverseReadonlyArrayWithIndex

Equivalent to ReadonlyArray#traverseWithIndex(Applicative).

Signature

export declare const traverseReadonlyArrayWithIndex: <A, B>(
  f: (index: number, a: A) => Lazy<B>
) => (as: readonly A[]) => Lazy<readonly B[]>
traverseReadonlyArrayWithIndex :: ((number, a) -> Lazy b) -> Array a -> Lazy (Array b)

Added in v0.12.0

traverseReadonlyNonEmptyArrayWithIndex

Equivalent to ReadonlyNonEmptyArray#traverseWithIndex(Applicative).

Signature

export declare const traverseReadonlyNonEmptyArrayWithIndex: <A, B>(
  f: (index: number, a: A) => Lazy<B>
) => (as: ReadonlyNonEmptyArray<A>) => Lazy<ReadonlyNonEmptyArray<B>>
traverseReadonlyNonEmptyArrayWithIndex :: ((number, a) -> Lazy b) -> ReadonlyNonEmptyArray a -> Lazy (ReadonlyNonEmptyArray b)

Added in v0.12.0

3 Functions

execute

Execute a Lazy, returning the value within. Helpful for staying within function application and composition pipelines.

Signature

export declare const execute: <A>(x: Lazy<A>) => A
execute :: Lazy a -> a

Example

import * as Lazy from 'fp-ts-std/Lazy'

assert.strictEqual(Lazy.execute(Lazy.of(5)), 5)

Added in v0.12.0

lazy

A constructor for Lazy values. Given Lazy is a type alias around () => A, this function’s only purpose is to aid in readability and express intentional laziness, as opposed to for example forgetting or opting not to use constant.

Signature

export declare const lazy: <A>(f: () => A) => Lazy<A>
lazy :: (() -> a) -> Lazy a

Example

import { lazy } from 'fp-ts-std/Lazy'

const calc = lazy(() => 'do something expensive here')

Added in v0.13.0

memoize

Memoize a Lazy. Provided the input function is pure, this function is too.

Signature

export declare const memoize: <A>(f: Lazy<A>) => Lazy<A>
memoize :: Lazy a -> Lazy a

Example

import { lazy, memoize } from 'fp-ts-std/Lazy'

const expensive = lazy(() => 42)
const payOnce = memoize(expensive)

assert.strictEqual(payOnce(), payOnce())

Added in v0.14.0

4 Minutiae

URI

Typeclass machinery.

Signature

export declare const URI: 'Lazy'
URI :: "Lazy"

Added in v0.12.0

URI (type alias)

Typeclass machinery.

Signature

export type URI = typeof URI
type URI = typeof URI

Added in v0.12.0