Fork me on GitHub

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
Ben Sless06:11:52

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)

Ben Sless20:11:57

Here, it's a big backwards but it's the most general way of making small building blocks of it

(defn parser
  (fn [success fail]
    (fn [x]
        (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-coercer identity on-error)