Hello ๐ I have yet to use cats, but I was wondering if I can implement some kind of monadic โeitherโ error handling/short-circuiting on arbitrary maps? I am using clj-rethinkdb, and it returns maps like:
{:changes [{:new_val {:id 1, :name "Carly Rae"}, :old_val {:id 1}}],
:deleted 0,
:errors 0,
:inserted 0,
:replaced 1,
:skipped 0,
:unchanged 0}
It can also returns then in a core-async channel. So how can I โtellโ cats which maps are errors, possibly to use in โmletโ?you can define a monad context for a new type easily enough @nha - the short-circuiting behaviour can be defined in the -mbind
method. if you take a look at the vanilla maybe
implementation you can see how it works: https://github.com/funcool/cats/blob/master/src/cats/monad/either.cljc#L158
i.e. short-circuiting involves -mbind
just returning the current m-value rather than applying the m-f
Thanks for the reference ๐
So I should make a function (say make-monad
) that take a map and returns something reified with a -mbind
implementation, is that correct?
@nha probably not - the monad context needs passing around either explicitly or by with-context
, and is usually a global context for a given type - a reified context would be a context for a value, so sounds wrong (unless i'm misunderstanding something)
or unless you are just talking about using reify to capture some closure stuff while creating a global context for your type
here's another example using deftype
rather than reify
https://github.com/funcool/cats/blob/master/src/cats/labs/manifold.clj#L47
what is the composition behaviour that you want though @nha ?
You are probably right - never used cats (or any language involving monads really) ๐
Well I guess the correct behaviour there would be to stop executing the queries as soon as there is an :error
, and return that result.
Ok the deftype
example clicks a bit more for me ๐ Iโll have a look thanks !
there's a core.async
context available too - https://github.com/funcool/cats/blob/master/src/cats/labs/channel.cljc - although i've never used that one
my best guess is that you will want something like a context which [1] has monadic-values something like PromiseChan<RethinkResponse>
[2] an -mbind
which in the case of non-error responses passes the :changes
value to the mf, and in the case of error responses just returns the RethinkResponse (in a promise-chan)
Thank you this help me getting started ๐