This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-10-02
Channels
- # beginners (98)
- # bigdata (1)
- # bitcoin (1)
- # boot (32)
- # cider (20)
- # cljs-dev (57)
- # cljsrn (130)
- # clojure (93)
- # clojure-dusseldorf (1)
- # clojure-germany (1)
- # clojure-greece (3)
- # clojure-italy (2)
- # clojure-russia (203)
- # clojure-spec (14)
- # clojure-uk (50)
- # clojurescript (127)
- # css (7)
- # cursive (6)
- # data-science (1)
- # datomic (4)
- # emacs (1)
- # events (1)
- # fulcro (8)
- # funcool (12)
- # graphql (7)
- # jobs (1)
- # lein-figwheel (2)
- # luminus (2)
- # off-topic (7)
- # om (16)
- # onyx (4)
- # parinfer (17)
- # pedestal (6)
- # portkey (36)
- # proton (3)
- # re-frame (10)
- # shadow-cljs (140)
- # spacemacs (12)
- # specter (1)
- # sql (1)
- # vim (10)
- # yada (10)
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)