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

Attempt at "Do notation" syntax #934

Closed
Herlevsen opened this issue Aug 17, 2019 · 4 comments
Closed

Attempt at "Do notation" syntax #934

Herlevsen opened this issue Aug 17, 2019 · 4 comments

Comments

@Herlevsen
Copy link

Hi,

I just want to start of by saying that i am fairly new to functional programming and fp-ts, so forgive me if there is some obvious flaw in my code or if my understanding of "Do notation" is off.

I just had a crack at "Do notation". I Have only implemented it for the Option data type, and therefore i don't know if this will work as well for other data types. The implementation uses an exception to short circuit, and i'm unsure what implications this might have?

Personally i think the syntax is more neat than other ones i've seen.

Looks like this:

binding(bind => {
    const number1 = bind(some(5))
    const number2 = bind(some(10))
    return some(number1 + number2)
}

The type of number1 and number2 is type safe and inferred as number by the compiler. Same goes for the return type of course.

The syntax is inspired by the Kotlin fp library, arrow.

The branch is available here
https://github.com/Herlevsen/fp-ts/tree/monad-binding

I've added 3 tests at the bottom of test/Option.ts.

What do you think?

Thanks

@gcanti
Copy link
Owner

gcanti commented Aug 18, 2019

@Herlevsen check out fp-ts-contrib's Do

@Herlevsen
Copy link
Author

@gcanti Thanks. I did have a look at it, but I think the syntax is a bit strange. Specifically using string literals and also the "scope object". However a nice thing is that it seems to be working for any monad.

Can i ask you, what you personally think of the syntax of both versions? And how it ideally would like with better typescript support?

@raveclassic
Copy link
Collaborator

@Herlevsen It would be ideal to have some kind of HKT for generators in typescript: microsoft/TypeScript#2983 (comment) - this is the most clean syntax available in TS/ES

@mattiamanzati
Copy link

@Herlevsen How will work your proposal when using things like IO and Task?
Do from fp-ts-contrib does work with that too! :)

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

No branches or pull requests

4 participants