Closed apiep closed 4 years ago
I gave a look at the link you posted and it don't give any hint of what it's supposed to do.
Anyway, you can easily implement your own monad with dartz.
As a clean example, you can have a look at IdMonad:
class IdMonad extends Functor with Applicative, Monad {
@override pure<A>(A a) => a;
@override bind<A, B>(A fa, Function1<A, B> f) => f(fa);
IList<A> replicate<A>(int n, A fa) => new IList.from(new List.filled(n, fa));
}
Monad requires you to implement bind
(the pure
is from Applicative; you may want to implement it as well or not), and you can do it freely as long as it respect the method signature.
For more details on the Monad class, you may refer to the source code. Depending on your use case, you may also want to implement MonadOps.
For more examples on Monad implementations, you may also refer to the source:
Hi thanks for the answer.
The idea is to have an abstraction over effectful function which will fetch data over http.
At first I want to implement it with free monad, but after reading some blog post, it seems I can achive the same thing with just monad instance. Thats why I ask if I can make an instance of monad with dartz.
I am still learning about functional programming, and want to learn it by implementing it on my project. And I don't quite understand what is the different between Monad and MonadOps.
@apiep, super short recap:
Monad
is for stand alone monad instances for existing types. They are currently not half as useful as they are in scala or haskell, since Dart lacks higher-kinded types (see issue #32 for a potential workaround).
MonadOps
declares an OO-friendly monadic interface, intended to be used directly on types you write yourself. This allows for saner static typing and nicer syntax compared to Monad
, but doesn't fully capture the concept of monad instances.
As the author of "Free monad considered harmful" mentions, the title is intentional click bait. The points made in the article are valid and worthy of thought. However, Free
actually works really well on Dart right now, whereas FTE to my knowledge doesn't. If all you want to do is embed a pure DSL for REST calls, then Free
should be a perfectly reasonable solution.
Thanks for the clarification, even though I'm not quite "click" yet with your explanation for MonadOps
.
I will try to implement it with Free
.
Thanks again for the help.
How do I implement something like in this post with dartz?