Fork me on GitHub
#funcool
<
2017-10-02
>
nha10:10:53

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”?

mccraigmccraig10:10:09

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

mccraigmccraig10:10:26

i.e. short-circuiting involves -mbind just returning the current m-value rather than applying the m-f

nha11:10:43

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?

mccraigmccraig12:10:43

@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)

mccraigmccraig12:10:55

or unless you are just talking about using reify to capture some closure stuff while creating a global context for your type

mccraigmccraig12:10:35

what is the composition behaviour that you want though @nha ?

nha12:10:55

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 !

mccraigmccraig12:10:26

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

mccraigmccraig12:10:48

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)

nha13:10:04

Thank you this help me getting started 🙂