Fork me on GitHub
#clojure-spec
<
2020-02-18
>
bmaddy05:02:08

Is it possible to parse streams or lazy seqs with spec? Specifically, I'm interested in a stream where trying to consume too much data would cause it to hang forever.

bmaddy05:02:07

To clarify that a little, there would be a number at the beginning that tells me how many more numbers to consume.

seancorfield05:02:24

Spec is not designed to be used for parsing. I suspect you already know that but it's worth repeating.

bmaddy05:02:09

Huh, I guess I'd forgotten that. So definitely worth repeating! Thanks!

seancorfield05:02:00

(you can abuse Spec for parsing but it will often be suboptimal... at best)

jeroenvandijk08:02:35

@seancorfield I understand that Spec is not meant for high performance parsing. I do find it useful and use it sometimes to parse datastructures. Do you know of a nice alternative that is performant?

delaguardo08:02:33

https://github.com/youngnh/parsatron this library could be a good starting point but it was designed to parse strings instead of data so might need some tweaks

jeroenvandijk09:02:29

That's useful as well as alternative to instaparse. Thank you

delaguardo09:02:33

instaparse can not be turned into the parser of data structures unfortunately. Only strings are allowed and this is deeply hidden inside of implementation details

jeroenvandijk09:02:40

Yeah I might have combined instaparse with clojure.spec. Not sure actually

pithyless09:02:01

https://github.com/metosin/malli and https://github.com/noprompt/meander come to mind. They attack different problems, but are both useful when working in the domain of "I need to interpret some data structures".

👍 8
mmeix10:02:36

I just read this article: https://juxt.pro/blog/posts/parsing-with-clojure-spec.html (a small parser for ical-entries) and watched this video: https://www.youtube.com/watch?v=xLB8Xgt8lpA (a simple hiccup-parser). These seemed (to me, as a spec beginner) reasonable applications of spec - maybe, because, they are quite simple? Would there be a problem for using spec at this scale?

mmeix11:02:24

Additional question: is it considered idiomatic to dispatch a multimethod on the result of a s/conform? Are there drawbacks?

jeroenvandijk12:02:16

I have done that. Cannot think of real drawbacks expect for that if your spec changes you have to update the multimethods

jeroenvandijk12:02:18

(Changing) dispatch functions of multimethods are a bit of a hassle in a repl environment as it is hard to undefine (for good reasons). But to make this work in the repl you need to use some tricks

mmeix13:02:47

ok, thanks!

dergutemoritz14:02:29

Temporarily placing (def my-multimethod nil) in front of the defmulti and then recompiling the namespace is an easy way to do it!

jeroenvandijk15:02:04

@U06GVE6NR This is true, but you have to reload all the namespace or the method is not extended the way you expect. My trick is to define a dispatch function and define the method as (defmulti my-method #'dispatch-my-method). This has been the most reliable way for me when working in the repl for a long time

dergutemoritz15:02:39

Ah right, if you have method implementations in other namespaces, then this trick won't suffice, good point 🙂

dergutemoritz15:02:47

Good idea with the var