This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-09-22
Channels
- # announcements (2)
- # beginners (137)
- # chlorine-clover (13)
- # clj-kondo (3)
- # cljsrn (4)
- # clojure (52)
- # clojure-australia (3)
- # clojure-dev (2)
- # clojure-europe (34)
- # clojure-nl (1)
- # clojure-sg (3)
- # clojure-spec (1)
- # clojure-uk (12)
- # clojurescript (2)
- # clojureverse-ops (7)
- # code-reviews (3)
- # conjure (2)
- # cursive (18)
- # datavis (21)
- # datomic (34)
- # exercism (1)
- # figwheel-main (6)
- # graphql (3)
- # helix (21)
- # introduce-yourself (1)
- # jackdaw (1)
- # jobs (4)
- # jobs-discuss (32)
- # juxt (14)
- # leiningen (6)
- # lsp (35)
- # meander (19)
- # nrepl (2)
- # off-topic (37)
- # portal (40)
- # quil (5)
- # re-frame (45)
- # reagent (10)
- # releases (1)
- # remote-jobs (4)
- # reveal (15)
- # sci (7)
- # shadow-cljs (40)
- # spacemacs (3)
- # tools-build (2)
- # vim (17)
- # xtdb (11)
hello, I have another Meander puzzle 😅, this seems easier but I still hitting the wall on this:
; input data
'{:body-params {:baz int?}
:path-params {:foo string?
:bar uuid?}}
; expect output
'[{:name :baz, :kind :body-params, :type int?}
{:name :foo, :kind :path-params, :type string?}
{:name :bar, :kind :path-params, :type uuid?}]
; non-meander implementation
(defn params->fields [params]
(reduce-kv
(fn [out kind params]
(if (map? params)
(reduce-kv
(fn [out name type]
(conj out {:name name :kind kind :type type}))
out
params)
out))
[]
params))
with meander I tried using rewrites
, but I ended up with some cardinal products, so wonder how you would tackle this(m/rewrite '{:body-params {:baz int?}
:path-params {:foo string?
:bar uuid?}}
{?k ?v & ?more}
[& (m/cata [?k ?v]) & (m/cata ?more)]
[?k (m/map-of !ks !vs)]
[{:kind ?k & {:name !ks :type !vs}} ...]
?x ?x)
;; => [{:name :baz, :type int?, :kind :body-params}
;; {:name :foo, :type string?, :kind :path-params}
;; {:name :bar, :type uuid?, :kind :path-params}]
If you have any more problems, let me know. better than crossword puzzles and sudoku
2
awesome, thank you so much, and since you asked, there is a twist on this one that I wonder if we solve strait from meander
this is my current code:
(defn params->fields [params]
(->> (m/rewrite params
{?k ?v & ?more}
[& (m/cata [?k ?v]) & (m/cata ?more)]
[?k (m/map-of !ks !vs)]
[{:kind ?k & {:name !ks :type !vs}} ...]
?x ?x)
(mapv (fn [{:keys [name] :as field}]
(cond-> field
(and (coll? name) (= :opt (first name)))
(assoc :name (second name) :optional? true))))))
the mapv is to take care of optional params, here is a test including those:
(is (= (forms/params->fields
'{:path-params {:foo string?
(:opt :bar) uuid?}})
'[{:name :foo, :kind :path-params, :type string?}
{:name :bar, :kind :path-params, :type uuid?, :optional? true}]))
so in that case, it needs to extract from [:opt :name]
(sometimes its (:opt :name)
, so not always a vector
and add the optional? true
flag
(defn params->fields [params]
(->> (m/rewrite params
{?k ?v & ?more}
[& (m/cata [?k ?v]) & (m/cata ?more)]
[?k (m/map-of (m/or (m/and (m/keyword _ _ :as !ks) (m/let [!optional false]))
(m/and (:opt !ks) (m/let [!optional true]))) !vs)]
[{:kind ?k & {:name !ks :type !vs :optional? !optional}} ...]
{} {})))
🎉 1