This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-12-09
Channels
- # adventofcode (201)
- # bangalore-clj (5)
- # beginners (90)
- # cider (1)
- # cljs-dev (3)
- # cljsrn (28)
- # clojure (128)
- # clojure-argentina (1)
- # clojure-brasil (15)
- # clojure-dev (9)
- # clojure-greece (18)
- # clojure-italy (11)
- # clojure-madison (1)
- # clojure-poland (19)
- # clojure-russia (28)
- # clojure-spec (33)
- # clojure-uk (26)
- # clojurescript (20)
- # core-async (2)
- # cursive (1)
- # datomic (2)
- # emacs (4)
- # figwheel (3)
- # fulcro (15)
- # graphql (7)
- # hoplon (1)
- # lein-figwheel (2)
- # lumo (2)
- # numerical-computing (3)
- # off-topic (9)
- # re-frame (13)
- # ring (1)
- # shadow-cljs (1)
- # unrepl (1)
- # vim (17)
I have a question, I’m using spec to validate the format of a “layout string” and in the spec I use conformers to convert from string to seq of chars:
(s/def ::layout-string
(s/and string?
not-empty
(s/conformer seq)
(s/+ (s/alt :col-align-bracket-expr
(s/cat :left-bracket #{\[}
:col-align-char #{\L \C \R \l \c \r}
:right-bracket #{\]})
:col-padding-non-bracket-char
(complement #{\[ \]})))
(s/conformer char-seq->str-conformer)))
, is conformers the way to go or is there some cleaner way to deal with strings as sequences of chars?I was hoping to use https://github.com/bhb/expound to make the error messages more readable, but expound does not seem to support conformers
I think I’m landing at using instaparse for parsing strings instead of spec. Seems like coercing proper error messages (including the location in the string etc) from spec would be more work than it’s worth for this problem
While spec can be twisted into parsing strings, it’s not designed for that and it’s going to be way slower than either a proper regex or a parser.
does anyone have an example of how you would conform a string into a data structure, like a hasmap: (conform ::foo "8/blue") => {:age 8 :eye-color "blue"}
It seems i would need to specify almost like a regex over the string. This feels doable, but i'm not sure.
See s/conformer. You basically pass a fn to it that does the conforming or returns ::s/invalid
I would say, don’t use spec for this, use a parser like instaparse
spec is about describing the structure of Clojure data, not strings
@mbjarland I just used Instaparse for a similar problem: https://github.com/borkdude/aoc2017/blob/master/src/day9_instaparse.clj#L14
I was going to ask here, Instaparse allows to hide output or hide tags to prevent extra nesting. Is such a thing with Spec also possible?
You could maybe use s/and
and s/conformer
to flatten things as they are being parsed / conformed.
Just clojure
It includes spec so you don’t need to declare that
Use of generators is optional. If you want that, you’ll also need [org.clojure/tools.check “0.9.0”]
@alexmiller thanks. I'm going to use spec to validate csv files, seems like a good fit
I don’t think that’s a good fit
CSV is text with tricky quoting rules
The right tool is a parser, like instaparse
Spec is for Clojure data
hmm... I'll take a look at instaparse. I have fields that can either be an int or "Missing", which I need to create a log of an errors for the user to correct before importing the file, which is why I thought spec would be good for that
Could you use instaparse to parse the data, then use clojure spec to just check it after?
Yeah i just used https://github.com/clojure/data.csv which was pretty handy
@aengelberg thank you — and thanks for Instaparse, it’s a really amazing tool 🙂
Glad you find it useful!