Fork me on GitHub
#clojure-spec
<
2018-01-17
>
borkdude19:01:56

I cannot pass a keyword as function to s/conformer, since it will try to resolve it as a spec. e.g.: (s/conformer (fn [parsed] (:foo parsed))) is needed right?

borkdude19:01:50

Other question: I vaguely remember something about not using spec as a string/text parser from this channel, but what exactly was the argument against it?

seancorfield19:01:42

The main objection is that is complects parsing and validation -- and "forces" the parsing effect on all clients of the spec.

borkdude19:01:37

forces the parsing effect?

borkdude19:01:34

If anyone wants to review my clojure.spec parser (for performance comparison with other parsers like Instaparse, Kern), please do: https://github.com/borkdude/aoc2017/blob/master/src/day16.clj#L234

seancorfield19:01:42

The main objection is that is complects parsing and validation -- and "forces" the parsing effect on all clients of the spec.

seancorfield19:01:29

(what we tend to do is have a spec that accepts both the target type as well as a string that could represent the target type ... if the parse/conversion fails, the result is ::s/invalid; if the parse/conversion succeeds or if the target type was provided, the validation is performed)

donaldball20:01:14

Do you have an example you can share of this style?

seancorfield21:01:08

I've shared it before here but it'll take me a while to de-tangle the generic code from the proprietary code so don't hold your breath for me to get around to it (too busy this afternoon and I'll probably forget after that).

seancorfield21:01:45

I probably ought to just open source it at some point...

Alex Miller (Clojure team)20:01:14

my main objection to it is that the regex op capabilities of spec are designed to support the structure of data and only incidentally can be used as a way to describe the structure of strings. Inevitably, I suspect you will find either the capabilities or performance of “spec as string parser” to be lacking and will be disappointed in our lack of interest in addressing those. There are very good tools already in existence for creating parsers on strings and I those are the right tool for the job.

seancorfield21:01:30

And to be clear, what we do at World Singles is not any sort of spec-as-parser -- we just have a few, very specific specs that accept a string or a target type and, for the string, do a simple conversion. Those target types are Long, Double, Boolean, and Date. We don't attempt to do anything else via spec -- this is just for simple form or query string data.

borkdude21:01:20

I was surprised spec’s performance was even better than Instaparse (see link above). Handwritten parser is fastest, Kern in between.

borkdude21:01:10

But yeah, I understand there is no interesting in optimizing spec for strings, although it does quite well

donaldball21:01:47

I was also fairly delighted at how both expressive and fast spec was when validating strings as seqs of chars, at least for my silly use cases.

aengelberg21:01:34

to be fair, Instaparse has enough processing overhead that parsing one or two characters at a time in a 47 KB string isn't the best (or most common) use of it 🙂

borkdude21:01:31

@aengelberg yeah, I’ve discussed this example with you before in #instaparse I believe, it’s part of Advent of Code. Nothing conclusive, just very contextual.

Alex Miller (Clojure team)22:01:44

I think the perf differences are going to be highly dependent on the complexity of the grammar and the size of the data so it’s hard to make predictions from a single example.

rickmoynihan22:01:31

Also instaparse is a far more general purpose parsing solution, supporting both left and right recursion; I’d be surprised if that didn’t incur an overhead.

dpsutton23:01:12

i've written some specs and i put some calls to s/valid? in comment macros in the namespaces where they are. is there a good way to take this out of a comment macro and have them run at some convenient but non-production time?

dpsutton23:01:14

clojurescript is the target if there's a compiler option to prevent spec checks on advanced compilation