This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-11-04
Channels
- # announcements (7)
- # aws (5)
- # babashka (72)
- # beginners (43)
- # calva (12)
- # cider (9)
- # clara (3)
- # clj-kondo (12)
- # cljdoc (32)
- # cljs-dev (10)
- # cljsrn (1)
- # clojure (78)
- # clojure-dev (50)
- # clojure-europe (17)
- # clojure-gamedev (8)
- # clojure-nl (1)
- # clojure-spec (30)
- # clojure-uk (3)
- # clojurescript (52)
- # core-async (1)
- # cursive (5)
- # datomic (8)
- # emacs (58)
- # events (2)
- # fulcro (5)
- # graalvm (7)
- # holy-lambda (37)
- # honeysql (9)
- # jobs (5)
- # leiningen (3)
- # lsp (7)
- # lumo (2)
- # malli (3)
- # meander (13)
- # membrane-term (64)
- # missionary (19)
- # music (3)
- # nextjournal (8)
- # off-topic (29)
- # pathom (16)
- # polylith (14)
- # portal (16)
- # re-frame (2)
- # reagent (5)
- # sci (14)
- # shadow-cljs (20)
- # spacemacs (6)
- # sql (1)
- # tools-deps (58)
- # vim (14)
Is there a recommended way to combine parsing and decoding/transforming operations? I'm trying to parse a value and return the transformed value (there are subschemas with custom transformers) in the parsed structure.
❤️ 1
The tldr is cps transform Have a function which returns a parser Every parser gets two continuations, success and failure On providing two continuations you return the actual parsing function Same with decode (you should validate, too while there)
✅ 1
Here, it's a big backwards but it's the most general way of making small building blocks of it
(defn parser
[parse-fn]
(fn [success fail]
(fn [x]
(try
(success (parse-fn x))
(catch Exception e
(fail e x))))))
(defn coercer
[schema transformer]
(let [schema (m/schema schema)
decoder (m/decoder schema transformer)
validator (m/validator schema)
explainer (m/explainer schema)]
(fn [success fail]
(fn [x]
(let [x (decoder x)]
(if (validator x)
(success x)
(fail (explainer x) x)))))))
(def my-parser (parser ,,,))
(def my-coercer (coercer ,,,))
(def work
(my-parser
(my-coercer identity on-error)
on-error))