This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-03-06
Channels
- # architecture (25)
- # bangalore-clj (1)
- # beginners (21)
- # boot (45)
- # cljs-dev (38)
- # clojure (272)
- # clojure-austin (7)
- # clojure-finland (7)
- # clojure-france (3)
- # clojure-italy (7)
- # clojure-japan (1)
- # clojure-russia (13)
- # clojure-spec (36)
- # clojure-uk (31)
- # clojurescript (96)
- # core-async (15)
- # cursive (16)
- # datascript (3)
- # datomic (97)
- # emacs (107)
- # hoplon (16)
- # jobs (9)
- # keechma (1)
- # luminus (1)
- # off-topic (19)
- # om (39)
- # onyx (15)
- # pedestal (3)
- # planck (22)
- # protorepl (4)
- # re-frame (20)
- # reagent (3)
- # ring-swagger (25)
- # specter (26)
- # test-check (19)
- # testing (1)
- # untangled (381)
Hi all. Trying to get from
”a b (c d)”
to [:a :b [:c :d]]
...(->> (clojure.string/split “a b (c d)” #” “) (map keyword))
only gets me this far of course.(map keyword [”a” “b” “c” “d”])
works, of course.But it’s the parsing of string such that bracketed terms are returned as (sub) vectors of results that stumps me.
Look at instaparse
as a relatively easy way to go from strings to data structures. Assuming you know the grammar of the string.
e.g.
“a (b c)" -> [a [b c]]
Yes my grammar actually. Thanks will look it up.
@seancorfield Not light reading but I’m pretty sure this will do it. Thanks again.
@gamecubate do you want nested parens to give you nested lists? e.g. a (b (c d))
Actually, want to parse a string for nested parens, turning it into nested sequence
So, yes.
(def my-string “a b (c d)”)
Wanted:
[:a :b [:c :d]]
or '(:a :b (:c :d))
@gamecubate Interesting little problem. My attempt looks like this:
(def my-string "a b (c d)")
((fn to-keywords [a] (if (coll? a) (map to-keywords a) (keyword a)))
(read-string (str "[" my-string "]")))
I had to surround the string with [
and ]
or some other thing to make it a data structure, and then the reader works okay.
FYI, read-string
isn’t safe to use on arbitrary input since it can execute code. Consider clojure.edn/read-string
instead.