This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-04-18
Channels
- # announcements (1)
- # babashka (16)
- # beginners (39)
- # calva (17)
- # cider (21)
- # cljs-dev (1)
- # clojars (2)
- # clojure (39)
- # clojure-australia (1)
- # clojure-europe (1)
- # clojure-spec (7)
- # conjure (1)
- # cursive (6)
- # datomic (2)
- # depstar (5)
- # graalvm (20)
- # instaparse (11)
- # meander (4)
- # pathom (4)
- # pedestal (3)
- # polylith (18)
- # re-frame (13)
- # reagent (4)
- # reitit (3)
- # shadow-cljs (2)
- # spacemacs (14)
- # vrac (1)
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
Looks pretty great to me! Glad you were able to figure things out