News for hackers. Stuff that matters.
Re:What Clojure Spec is and what you can do with it by gw on Thursday September 10, 2020 @11:05AM (#24432987)
I think the instrumenting and generator stuff gets disproportionate attention. For me by far the biggest win from spec has been with parsing. This completely changes how you'd write a library that takes a data structure and parses it into something meaningful (for example, what hiccup does for html or what honeysql does for sql).

In the past, this required a lot of very ugly parsing code and manual error-checking. With spec, you write specs and call s/conform. If it failed, you get a nice error, especially if you pair it with expound. If it succeeded, you get a destructured value that is really easy to pull data out of. I've done this in a half dozen different libraries and i'm pretty sure i wouldn't have even written them without spec.

Re:What Clojure Spec is and what you can do with it by adamkl on Thursday September 10, 2020 @11:57AM (#24433500)
Totally agree with this.

I started playing with spec because of the idea of automated test generation, but the reality of it is that I use it as a super-charged validation library.

I think this emphasis actually does the library a disservice in that I see new users ask questions along the lines of "Should I use s/valid? to manually check inputs to my API"? The answer to that, in my usage, is "Yes! Of course!", but many people seem to think that they are using Spec wrong if they use it for something other than instrumentation and generation.

Re:What Clojure Spec is and what you can do with it by marxama on Thursday September 10, 2020 @02:38PM (#24435247)
I remember doing just that - writing some ugly parsing code, thinking that I should be a good team member and add some specs for what I was doing, and when I tried calling conform... Oh, it did the parsing for me!
Re:What Clojure Spec is and what you can do with it by didibus on Thursday September 10, 2020 @02:35PM (#24435219)
Yup, surprised the article doesn't mention parsing.

When writing complicated macros, Spec conforming is so useful!

Re:What Clojure Spec is and what you can do with it by stingraycharles on Thursday September 10, 2020 @11:49AM (#24433419)
and if you then combine s/conform with core.match, you can build really elegant code to traverse these structures.
Re:What Clojure Spec is and what you can do with it by bgorman on Thursday September 10, 2020 @12:29PM (#24433834)
Why not just pattern match immediately if you are going to bother with using core.match?
Re:What Clojure Spec is and what you can do with it by stingraycharles on Thursday September 10, 2020 @01:52PM (#24434785)
How would you pattern match with Clojure without using core.match? Do you mean using another library?

Edit: oh I understand what you mean, you were thinking of skipping the s/conform part, and use core.match directly. Personally, I consider spec an excellent library to describe the shape of data, and core.match allows for better describing the actions you want to take based upon that.

For example, with spec you can define a bunch of alternatives using s/or, and then use core.match to then easily traverse the results.

It’s more a matter of separation of concerns to me. I don’t use core.match for validation or describing shapes of data.

Re:What Clojure Spec is and what you can do with it by jmiskovic on Thursday September 10, 2020 @03:54PM (#24435920)
Do you have to adapt spec definition to core.match somehow? Is there a resource to read more how to use them together?
Re:What Clojure Spec is and what you can do with it by stingraycharles on Friday September 11, 2020 @04:50AM (#24440482)
No you don't have to do anything.

Let's say I have something like

  (s/def :thespec (s/or :foo ::foo 
                        :bar ::bar))
I can then use it in conjunction with core.match like

  (match [(s/conform :thespec val)]
    [:foo x] (do-something-with x)
    [:bar y] (do-something-else-with y))
Which imho is an easier way to navigate these things.
Re:What Clojure Spec is and what you can do with it by agumonkey on Thursday September 10, 2020 @02:11PM (#24434983)
can you "stream" conform ? for UI live input

so that "john mcallister" yields { :name "john" :lastname "mcallister" } but "john " would yield { :name "john" :lastname nil } (or :todo even)

Re:What Clojure Spec is and what you can do with it by didibus on Thursday September 10, 2020 @02:38PM (#24435245)
You could call it on every input change, though I'd say Spec conforming is not the most performant parsing library in the world, so not sure if it be fast enough for running it on each input.
Re:What Clojure Spec is and what you can do with it by agumonkey on Friday September 11, 2020 @03:22AM (#24439997)
yeah that's why I was looking for a lazy / stream variant in case it wasn't already possible

/me goes to prolog

Re:What Clojure Spec is and what you can do with it by Scarbutt on Thursday September 10, 2020 @12:25PM (#24433785)
This is why languages like Ocaml are really nice to work with for this stuff.
Re:What Clojure Spec is and what you can do with it by didibus on Thursday September 10, 2020 @02:24PM (#24435107)
Does OCaml have a good parsing DSL build in?
Re:What Clojure Spec is and what you can do with it by Scarbutt on Thursday September 10, 2020 @02:58PM (#24435433)
Yes, it's called Ocaml ;)
Re:What Clojure Spec is and what you can do with it by Nekorosu on Thursday September 10, 2020 @05:15PM (#24436641)
Could you elaborate on your point?
Re:What Clojure Spec is and what you can do with it by Scarbutt on Thursday September 10, 2020 @08:01PM (#24437889)
Ocaml's strong static typing with type inference and pattern-matching (with exhaustivity checking) suite this kind of work quite well, there's plenty of literature around for it.