Hey, I have a question on some code I wrote to do parsing for an advent of code problem:
(meander/defsyntax keywordize [a b]
`(meander/app #(keyword (str %1 "-" %2)) ~a ~b))
(meander/defsyntax to-map [a]
`(meander/app (partial apply hash-map) ~a))
(meander/defsyntax parse-int [str]
`(meander/app #(Integer/parseInt %) ~str))
(defn parse-description [description]
(meander/rewrite
(str/split description #" ")
[?adj ?col _ "contain" . !ns !adjs !cols _ ...]
{(keywordize ?adj ?col) (to-map [(keywordize !adjs !cols) (parse-int !ns) ...])}))
(def example
"plaid magenta bags contain 2 clear lavender bags, 3 clear teal bags, 4 vibrant gold bags.")
(parse-description example)
;; {:plaid-magenta {:clear-lavender 2, :vibrant-gold 4, :clear-teal 3}}
The solution is beautifully concise, but there's a bit more verbosity in the third argument of meander/rewrite
because I want to slightly transform the output (unifying two memory variables in a keyboard, get the parse of another, etc. This brings me to define all those defsyntax
rule. Is there a way to accomplish this with less ceremony and more clarity? (ps meander is beautiful)reading the code, I discovered meander/map-of
with which I can slightly clean the code and get rid of my to-map
. The other two definitions still stand though, I'd really like to at least turn them in inline constructs
Ok, this is the version I'm happy with ❤️
Looks pretty great to me! Glad you were able to figure things out