Skip to content

Instantly share code, notes, and snippets.

@nythrox
Last active May 5, 2022 19:17
Show Gist options
  • Select an option

  • Save nythrox/bb369026dcecf710233582e7cbe1955b to your computer and use it in GitHub Desktop.

Select an option

Save nythrox/bb369026dcecf710233582e7cbe1955b to your computer and use it in GitHub Desktop.

Revisions

  1. nythrox revised this gist May 5, 2022. 2 changed files with 38 additions and 27 deletions.
    37 changes: 10 additions & 27 deletions do.ts
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,11 @@
    function doEither<
    R,
    Ftr extends Either<any, any>,
    L = Ftr extends Either<infer L, any> ? L : never
    >(
    fun: () => Generator<Ftr, R, any>
    ): Either<L, R> {
    const iterator = fun()
    const state = iterator.next()
    function run(state: any): any {
    if (state.done) {
    return Right(state.value)
    }
    return state.value.chain((val: any) => {
    return run(iterator.next(val))
    })
    }
    return run(state)
    // user: Either<string, { name: string, age: number }>
    const user = doEither(function*() {
    // name: string
    const name = yield* Right("test")
    // age: number
    const age = Math.random() > 0.5 ? yield* Right(100) : yield* Left("oopsies")
    return {
    name,
    age
    }

    export interface Either<L,R> {
    [Symbol.iterator]: () => Iterator<Either<L, R>, R, any>
    }


    Either.prototype[Symbol.iterator] = function*() {
    return (yield this) as any
    }
    })
    28 changes: 28 additions & 0 deletions z.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    function doEither<
    R,
    Ftr extends Either<any, any>,
    L = Ftr extends Either<infer L, any> ? L : never
    >(
    fun: () => Generator<Ftr, R, any>
    ): Either<L, R> {
    const iterator = fun()
    const state = iterator.next()
    function run(state: any): any {
    if (state.done) {
    return Right(state.value)
    }
    return state.value.chain((val: any) => {
    return run(iterator.next(val))
    })
    }
    return run(state)
    }

    export interface Either<L,R> {
    [Symbol.iterator]: () => Iterator<Either<L, R>, R, any>
    }


    Either.prototype[Symbol.iterator] = function*() {
    return (yield this) as any
    }
  2. nythrox renamed this gist Jul 17, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. nythrox revised this gist Jul 8, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _index.ts
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ const user = doEither(function*() {
    // name: string
    const name = yield* Right("test")
    // age: number
    const age = Math.random() > 0.5 ? Right(100) : Left("oopsies")
    const age = Math.random() > 0.5 ? yield* Right(100) : yield* Left("oopsies")
    return {
    name,
    age
  4. nythrox created this gist Jul 8, 2021.
    11 changes: 11 additions & 0 deletions _index.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    // user: Either<string, { name: string, age: number }>
    const user = doEither(function*() {
    // name: string
    const name = yield* Right("test")
    // age: number
    const age = Math.random() > 0.5 ? Right(100) : Left("oopsies")
    return {
    name,
    age
    }
    })
    28 changes: 28 additions & 0 deletions do.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    function doEither<
    R,
    Ftr extends Either<any, any>,
    L = Ftr extends Either<infer L, any> ? L : never
    >(
    fun: () => Generator<Ftr, R, any>
    ): Either<L, R> {
    const iterator = fun()
    const state = iterator.next()
    function run(state: any): any {
    if (state.done) {
    return Right(state.value)
    }
    return state.value.chain((val: any) => {
    return run(iterator.next(val))
    })
    }
    return run(state)
    }

    export interface Either<L,R> {
    [Symbol.iterator]: () => Iterator<Either<L, R>, R, any>
    }


    Either.prototype[Symbol.iterator] = function*() {
    return (yield this) as any
    }