Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Added run function imitating "do-notation". #53

Closed
wants to merge 3 commits into from

Conversation

zegerhoogeboom
Copy link

This PR adds a function called run that imitates Haskell's do-notation, as inspired by this blogpost. This implemented version here is slightly different to make it nicer-to-use. This implementation accepts returning Maybe<T> or just T from the generator function.

This example is also implemented as a unit test but for clarification, this PR adds the possibility to do the following:

const result: Maybe<number> = Maybe.run<number>(function* () {
  const x = yield Maybe.just(1);
  const y = yield Maybe.just("2");
  return x + parseInt(y);
});

Without the run function, this example could be solved using sequence or using a combination of .bind().map(), which doesn't scale in terms of indentation. Using sequence seems like an odd solution in this case as it's not particularly natural to represent these values in a map.

const result: Maybe<number> = Maybe.just(1).bind((x) => {
    return Maybe.just("2").map((y) => {
        return x + parseInt(y);
    })
});

Note though that the variables within the generator function (e.g. in above example const x) will be of type any, but any mixture of types within Maybe<T> is accepted (e.g. in above example Maybe<number> and Maybe<string>

@cbowdon
Copy link
Owner

cbowdon commented Dec 15, 2018

Sorry this has languished so long. It's a neat idea, but I'm no longer actively maintaining this project and therefore not going to merge any more PRs. Thank you very much for your contribution all the same.

@cbowdon cbowdon closed this Dec 15, 2018
@zegerhoogeboom
Copy link
Author

No worries, I'm still actively using the library, thanks for creating it!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants