Fork me on GitHub
#malli
<
2021-03-06
>
ikitommi20:03:29

pulling out malli schemas for defns (Vars):

(-var-schema #'+)
;[:function
; [:=> :catn :any]
; [:=> [:catn [x :any]] :any]
; [:=> [:catn [x :any] [y :any]] :any]
; [:=> [:catn [x :any] [y :any] [more [:+ :any]]] :any]]

(-var-schema #'println)
; [:=> [:catn [more [:* :any]]] :any]

borkdude20:03:36

Is this valid syntax?

[:=> :catn :any]

ikitommi20:03:32

yes, vectors are optional if there are no childs or props, e.g. [:int] can be written as :int.

ikitommi20:03:01

same for :catn. “empty sequence with named children”

ikitommi20:03:18

the imp btw:

(defn -var-schema [var]
  (let [-=> (fn [as] (let [[f s [t]] (partition-by #{'&} as)
                           [fas ras rop] (cond t [f t :+], s [nil (first s) :*], :else [f])]
                       [:=> (if (or (seq fas) ras)
                              (vec (concat [:catn] (mapv (fn [a] [a :any]) fas) (when ras [[ras [rop :any]]])))
                              :catn) :any]))
        {:keys [arglists]} (meta var)
        -=>s (mapv -=> arglists)]
    (if (second arglists) (into [:function] -=>s) (first -=>s))))

👍 3
ikitommi20:03:36

not sure if it’s a good idea to use :arglistsvar meta, but, works on my repl just now at least 🙂

borkdude20:03:43

not a bad idea

borkdude20:03:14

some Clojure vars use something like x* which denotes multiple xs, so the arglist is not always reliable, but in most cases it's auto-generated from the defn itself