Monads, bind and do #1708
lucasavila00
started this conversation in
Ideas & suggestions
Replies: 2 comments
-
We can also have import gleam/javascript/promise
pub type ReaderPromise(r, t) =
fn(r) -> promise.Promise(t)
pub fn of(a: a) -> ReaderPromise(r, a) {
fn(_r: r) { promise.resolve(a) }
}
pub fn map(fa: ReaderPromise(r, a), f: fn(a) -> b) -> ReaderPromise(r, b) {
fn(r: r) { promise.map(fa(r), f) }
}
pub fn chain(
fa: ReaderPromise(r, a),
f: fn(a) -> ReaderPromise(r, b),
) -> ReaderPromise(r, b) {
fn(r: r) { promise.then(fa(r), fn(then) { f(then)(r) }) }
} It would be a solution to the callback example shown in https://gleam.run/news/v0.16-gleam-compiles-to-javascript/ Specifically, we could have a task monad, which is a lazy promise. It wouldn't auto-flatten, aswell. |
Beta Was this translation helpful? Give feedback.
0 replies
-
This is really cool! I've left some thoughts in the issue you made relating to a rebindable syntax |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm re-implementing parts of
fp-ts
in Gleam.It works very well, inference and type-checking works perfectly.
As an example:
But I can't get any way to implement bind, or do notation.
fp-ts
uses typescript records to simulate a scope: https://gcanti.github.io/fp-ts/guides/do-notation.htmlOn ocaml, there are the let operators: https://v2.ocaml.org/manual/bindingops.html
On haskell, there is do notation: https://en.wikibooks.org/wiki/Haskell/do_notation
In gleam there is something close to it that is the
try
keyword used for Results.Is there a way to work around it? Are there plans to extend something similar to, or
try
but for any other monad?To sum it up, I'd like to turn this:
into something similar to haskell/ocaml
or fp-ts
Beta Was this translation helpful? Give feedback.
All reactions