Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support function* generators as a sort of do notation. #103

Open
graingert opened this issue Feb 14, 2017 · 12 comments · May be fixed by #159
Open

support function* generators as a sort of do notation. #103

graingert opened this issue Feb 14, 2017 · 12 comments · May be fixed by #159
Labels

Comments

@graingert
Copy link

No description provided.

@graingert graingert changed the title support funciton* generators as a sort of do notation. support function* generators as a sort of do notation. Feb 14, 2017
@ulfryk ulfryk added the Feature label Feb 14, 2017
@ulfryk
Copy link
Member

ulfryk commented Feb 14, 2017

@graingert can you give an example of how would you like it to look like ?

@graingert
Copy link
Author

const helloWorld = putStr("Hello")
  .flatMap(putStr(" "))
  .flatMap(putStr("world!"))
  .flatMap(putStr("\n"));
const helloWorld = moent.Do(function* () {
  yield putStr("Hello");
  yield putStr(" ");
  yield putStr("world!");
  yield putStr("\n");
});

you can also get values out too:

const hamSpam = monet.Do(function* () {
  const x = yield reader();
  const y = x.toLowerCase();
  yield putStr(y);
});

@sevcsik
Copy link

sevcsik commented Feb 22, 2018

@graingert
Copy link
Author

it would have to crash for monads with more than 1 element.

@graingert
Copy link
Author

@graingert
Copy link
Author

sadly it's really slow: https://github.com/pelotom/burrido#caveats

@sevcsik
Copy link

sevcsik commented Feb 22, 2018

Despite the limitations, I do think it would be useful for a limited set Monads where there's only one value, like Maybe and Either. Those who want to use it with streams despite the performance penalty can use burrido instead.

@ulfryk
Copy link
Member

ulfryk commented Feb 23, 2018

@graingert @sevcsik sorry for not being responsive. This seems to be quite an interesting feature, yet there is still a lot of work to make 'monet' stable and equipped with all needed/requested basic features. So probably it'll not appear in the library in some short time. I'm thinking also how to make it work without crashing lib for ES5 limited environments…

Anyway:

  1. PRs are welcome
  2. Maybe we should add Just another lib like @monet/generators and put all generator centric features there?

PS
I would love to see working do notation in JS/TS :)

@sevcsik
Copy link

sevcsik commented Feb 23, 2018

I sketched an implementation for the do notation (unfortunately do is a reserved word so I went with run). The first one is Monet-specific and the second one is more generic:

https://repl.it/@sevcsik/monet-monad-comprehension
https://repl.it/@sevcsik/generic-monad-comprehension

@sevcsik
Copy link

sevcsik commented Feb 23, 2018

The library itself should only contain the run function which itself doesn't contain a generator expression (a generator expression just yields a function which returns an iterator), so it will work fine in ES5. Of course, to use it, you'll need ES6 generator support.

My concern about writing a separate library is about determining the unit method which we should use to wrap the result. Now, I worked it around with optionally accepting the object which holds the unit function as the first argument; if the argument isn't supplied, the result won't be wrapped.

But if we would add it to every monadic type, like Maybe.do and Either.do, this wouldn't be an issue as the run function could just use this.unit (and the do word is not reserved for object keys, so there's that :))

@sevcsik
Copy link

sevcsik commented Feb 23, 2018

it would have to crash for monads with more than 1 element.

I agree. Do you know any way to detect that in a generic way? I couldn't think of anything... Of course, we could whitelist monads which are known to work if we only support Monet's monads.

@sevcsik
Copy link

sevcsik commented Mar 5, 2018

Opened a PR. I'm yet to figure out how to make the do function optional in TS, so it doesn't bring in an ES6 requirement.

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

Successfully merging a pull request may close this issue.

3 participants