Fork me on GitHub

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:


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


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 - - 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 🙂