Last active
May 5, 2022 19:17
-
-
Save nythrox/bb369026dcecf710233582e7cbe1955b to your computer and use it in GitHub Desktop.
Revisions
-
nythrox revised this gist
May 5, 2022 . 2 changed files with 38 additions and 27 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,28 +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 ? yield* Right(100) : yield* Left("oopsies") return { name, age } }) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 } -
nythrox renamed this gist
Jul 17, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
nythrox revised this gist
Jul 8, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ? yield* Right(100) : yield* Left("oopsies") return { name, age -
nythrox created this gist
Jul 8, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 } }) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 }