Skip to content

Commit

Permalink
recursively unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
cevr committed Jan 29, 2024
1 parent 8a1885e commit 54834ff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-beers-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ftld": minor
---

`Do` now recursively unwraps monadic value similar to async await
15 changes: 13 additions & 2 deletions lib/src/do.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,20 @@ describe("Do", () => {

it("should unwrap nested monads", async () => {
const task = Do(function* ($) {
return yield* $(Result.from(() => Option.Some(1)));
const x = yield* $(
Promise.resolve(
Result.from(() =>
Option.Some(
Result.from(() =>
Option.Some(Task.from(() => Promise.resolve(1)))
)
)
)
)
);
return x + 3;
});

expect(task.run()).toEqual(Result.Ok(1));
expect(await task.run()).toEqual(Result.Ok(4));
});
});
16 changes: 10 additions & 6 deletions lib/src/do.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
isMonad,
} from "./utils.js";
import { Task } from "./task.js";
import { isPromise } from "./internals.js";

class Gen<T, A> implements Generator<T, A> {
called = false;
Expand Down Expand Up @@ -83,11 +84,14 @@ const toTask = (maybeGen: unknown): Task<unknown, unknown> => {
const value = maybeGen instanceof UnwrapGen ? maybeGen.value : maybeGen;
const onErr =
maybeGen instanceof UnwrapGen ? maybeGen.onErr ?? identity : identity;
return (
value instanceof Task
? value
: Task.from(() => (isMonad(value) ? value.unwrap() : value), identity)
).mapErr(onErr);
const unwrap = (value: unknown): unknown => {
return isMonad(value)
? unwrap(value.unwrap())
: isPromise(value)
? value.then(unwrap)
: value;
};
return Task.from(() => unwrap(value), identity).mapErr(onErr);
};

type ComputeTask<Gen, ReturnValue> = Gen extends Array<
Expand Down Expand Up @@ -131,7 +135,7 @@ type EnsureGenUnwrapped<Gen> = Gen extends UnwrapGen<infer T> ? T : Gen;
type UnwrapValue<A> = [A] extends [never]
? never
: A extends Monad<unknown, infer B>
? B
? UnwrapValue<B>
: A extends Promise<infer C>
? UnwrapValue<C>
: A extends (...args: any) => infer B
Expand Down

0 comments on commit 54834ff

Please sign in to comment.