Skip to content

Help understanding Reader Monad #1554

Answered by 0x706b
a-laughlin asked this question in Q&A
Discussion options

You must be logged in to vote

The type definition for Reader is

interface Reader<R, A> {
  (r: R): A
}

Reader is a function from R to A.

The type definition for IO is

interface IO<A> {
  (): A
}

IO is a function from no input to A. In some contexts, it may also be thought of as Reader<unknown, A>.

The signature for Reader's bind is

export declare const bind: <N extends string, A, E, B>(
  name: Exclude<N, keyof A>,
  f: (a: A) => Reader<E, B>
) => (ma: Reader<E, A>) => Reader<E, { readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>

bind takes a name and a function from the output of the Reader to a new value and returns a function from a Reader to a Reader with that new value assigned to the name in an object.

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@a-laughlin
Comment options

Answer selected by a-laughlin
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants