Re: Spec of conform of spec
This is exactly my use case as well. I want to provide the user a nice DSL but make it easy to automatically generate input. The idea I had was the user could write things in the DSL and for automatic generation you could just generate the conformed structure. The other thing it's made me notice is that we really need a library to make errors clearer in large nested data structures. /j On Saturday, December 10, 2016 at 3:09:24 AM UTC+1, Leon Grapenthin wrote: > > Alex, I thought about this and it appears to be a convenience problem. > Spec is e. g. excellent to parse a e. g. a Query DSL (which is my current > side project) via conform. But then you have that large data structure that > you want to break down and operate on in several functions. So you need to > have the specs for the conformed structure built by spec. > >> -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Spec of conform of spec
Alex, I thought about this and it appears to be a convenience problem. Spec is e. g. excellent to parse a e. g. a Query DSL (which is my current side project) via conform. But then you have that large data structure that you want to break down and operate on in several functions. So you need to have the specs for the conformed structure built by spec. I found a surprisingly easy way to do so: (defmacro conformed "Takes a spec conformable and unformable spec. Returns a spec that validates and generates conformed values per spec." [spec] `(s/with-gen (fn [v#] (try (= v# (s/conform ~spec (s/unform ~spec v#))) (catch Throwable _# false))) (fn [] (gen/fmap (fn [v#] (s/conform ~spec v#)) (s/gen ~spec) Do you think it's a good idea to pursue this way? Kind regards, Leon. On Thursday, September 1, 2016 at 2:33:17 PM UTC+2, Alex Miller wrote: > > I think you may be confusing the return value of a predicate value (acting > as a spec) with the return value of the function passed to conformer. > > In the former case a predicate function's return value is a logically > truthy value and a return of nil or false indicates the value is invalid. > > The function passed to a conformer is expected to return either a > conformed value or ::s/invalid. > > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Spec of conform of spec
I think you may be confusing the return value of a predicate value (acting as a spec) with the return value of the function passed to conformer. In the former case a predicate function's return value is a logically truthy value and a return of nil or false indicates the value is invalid. The function passed to a conformer is expected to return either a conformed value or ::s/invalid. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Spec of conform of spec
I was asking how you would conform a value to a falsey value. If the interface for the function is that returning nil or false means that the input is not valid (which is what I understood from the discussion) then how can you conform to nil or false? However, on reading the docs for conformer that you posted here, it suggests that returning falsey values does not mean that they are invalid, only :clojure.spec/invalid means that. So apologies for the misunderstanding. I was away from a computer that I could reasonably run a REPL on, so I just asked rather than tested ;) Many thanks, Ed On Thursday, 1 September 2016 00:31:43 UTC+1, Alex Miller wrote: > > user=> (doc s/conformer) > - > clojure.spec/conformer > ([f] [f unf]) > Macro > takes a predicate function with the semantics of conform i.e. it should > return either a > (possibly converted) value or :clojure.spec/invalid, and returns a > spec that uses it as a predicate/conformer. Optionally takes a > second fn that does unform of result of first > > Conformers are expected return either a value or ::s/invalid if invalid. > > > On Wednesday, August 31, 2016 at 6:29:23 PM UTC-5, Alex Miller wrote: >> >> I don't understand the question. What are you trying to do? >> >> On Wednesday, August 31, 2016 at 4:08:00 PM UTC-5, l0st3d wrote: >>> >>> So how would you conform something to nil or false? For example: >>> >>> (s/conform (s/conformer read-string) "nil") >>> >>> ? >>> >>> -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Spec of conform of spec
user=> (doc s/conformer) - clojure.spec/conformer ([f] [f unf]) Macro takes a predicate function with the semantics of conform i.e. it should return either a (possibly converted) value or :clojure.spec/invalid, and returns a spec that uses it as a predicate/conformer. Optionally takes a second fn that does unform of result of first Conformers are expected return either a value or ::s/invalid if invalid. On Wednesday, August 31, 2016 at 6:29:23 PM UTC-5, Alex Miller wrote: > > I don't understand the question. What are you trying to do? > > On Wednesday, August 31, 2016 at 4:08:00 PM UTC-5, l0st3d wrote: >> >> So how would you conform something to nil or false? For example: >> >> (s/conform (s/conformer read-string) "nil") >> >> ? >> >> -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Spec of conform of spec
I don't understand the question. What are you trying to do? On Wednesday, August 31, 2016 at 4:08:00 PM UTC-5, l0st3d wrote: > > So how would you conform something to nil or false? For example: > > (s/conform (s/conformer read-string) "nil") > > ? > > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Spec of conform of spec
So how would you conform something to nil or false? For example: (s/conform (s/conformer read-string) "nil") ? -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Spec of conform of spec
I think Alex's point was given any arbitrary function can be used as the conform part of the spec, this wouldn't be possible. Ie boot.user=> (s/conform (s/conformer inc) 1) 2 On Saturday, June 18, 2016 at 10:35:10 AM UTC-7, Leon Grapenthin wrote: > > I am not sure whether I understand what you mean. > > Behavior of conform for predicates is to return its return value if it is > logically true, ::s/invalid otherwise. Thus the predicate itself is the > spec to its conform*. > > s/conformer is only limiting as much as it is to unform, a user would have > to provide a spec for conforms result as well as he has to provide an > unform-fn if he wants unforming. > > If each spec implemented a conform-spec* method, a spec could very well > provide a spec of it's conform. > > On Saturday, June 18, 2016 at 5:03:33 PM UTC+2, Alex Miller wrote: >> >> Given that conform takes an arbitrary (opaque) function, I don't think >> that's generically possible. >> >> >> On Saturday, June 18, 2016 at 7:37:33 AM UTC-5, Leon Grapenthin wrote: >>> >>> Assume I parse with conform. >>> >>> Then I have functions that operate on the value returned by conform. I >>> want to spec them. >>> >>> But I can't get a spec for the value returned by conform (so that I can >>> spec said functions) automatically. >>> >>> Imagine `(s/conform-spec ::my-spec)` would return the spec of the >>> result of calling (s/confom ::my-spec foo) >>> >>> So it would probably be valuable if a spec could give a spec of what its >>> conform* returns` >>> >>> -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Spec of conform of spec
Yes I have tried that. As soon as you use things like `or` or `alt` it becomes quite the tedious manual effort and you don't get away with s/keys anymore. On Saturday, June 18, 2016 at 8:35:20 PM UTC+2, Josh Tilles wrote: > > Have you considered choosing labels that are themselves qualified > keywords with registered specs? That might feel like a workaround, but > I think it could get you most of what you’re looking for. > > For example: > > ``` > (s/def ::even-spec even?) > ;= :user/even-spec > (s/def ::odd-spec odd?) > ;= :user/odd-spec > (s/def ::label-even (s/spec ::even-spec)) > ;= :user/label-even > (s/def ::label-odd (s/spec ::odd-spec)) > ;= :user/label-odd > (s/conform (s/cat ::label-even ::even-spec, ::label-odd ::odd-spec) [2 3]) > ;= {:user/label-even 2, :user/label-odd 3} > (s/conform (s/cat ::label-odd ::even-spec) [2]) > ;= {:user/label-odd 2} > (s/valid? (s/keys) *1) > ;= false > (s/explain (s/keys) *2) > ; In: [:user/label-odd] val: 2 fails spec: :user/label-odd at: > [:user/label-odd] predicate: odd? > ;= nil > ``` > > > On Saturday, June 18, 2016, Leon Grapenthin <grapent...@gmail.com > > wrote: > >> Assume I parse with conform. >> >> Then I have functions that operate on the value returned by conform. I >> want to spec them. >> >> But I can't get a spec for the value returned by conform (so that I can >> spec said functions) automatically. >> >> Imagine `(s/conform-spec ::my-spec)` would return the spec of the result >> of calling (s/confom ::my-spec foo) >> >> So it would probably be valuable if a spec could give a spec of what its >> conform* returns` >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clojure@googlegroups.com >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/d/optout. >> > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Spec of conform of spec
I just realized that in my example, I probably should have used `s/get-spec` instead of `s/spec` when defining the labels. Oh well. On Saturday, June 18, 2016, Josh Tilles <j...@signafire.com> wrote: > Have you considered choosing labels that are themselves qualified > keywords with registered specs? That might feel like a workaround, but > I think it could get you most of what you’re looking for. > > For example: > > ``` > (s/def ::even-spec even?) > ;= :user/even-spec > (s/def ::odd-spec odd?) > ;= :user/odd-spec > (s/def ::label-even (s/spec ::even-spec)) > ;= :user/label-even > (s/def ::label-odd (s/spec ::odd-spec)) > ;= :user/label-odd > (s/conform (s/cat ::label-even ::even-spec, ::label-odd ::odd-spec) [2 3]) > ;= {:user/label-even 2, :user/label-odd 3} > (s/conform (s/cat ::label-odd ::even-spec) [2]) > ;= {:user/label-odd 2} > (s/valid? (s/keys) *1) > ;= false > (s/explain (s/keys) *2) > ; In: [:user/label-odd] val: 2 fails spec: :user/label-odd at: > [:user/label-odd] predicate: odd? > ;= nil > ``` > > > On Saturday, June 18, 2016, Leon Grapenthin <grapenthinl...@gmail.com > <javascript:_e(%7B%7D,'cvml','grapenthinl...@gmail.com');>> wrote: > >> Assume I parse with conform. >> >> Then I have functions that operate on the value returned by conform. I >> want to spec them. >> >> But I can't get a spec for the value returned by conform (so that I can >> spec said functions) automatically. >> >> Imagine `(s/conform-spec ::my-spec)` would return the spec of the result >> of calling (s/confom ::my-spec foo) >> >> So it would probably be valuable if a spec could give a spec of what its >> conform* returns` >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clojure@googlegroups.com >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/d/optout. >> > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Spec of conform of spec
Have you considered choosing labels that are themselves qualified keywords with registered specs? That might feel like a workaround, but I think it could get you most of what you’re looking for. For example: ``` (s/def ::even-spec even?) ;= :user/even-spec (s/def ::odd-spec odd?) ;= :user/odd-spec (s/def ::label-even (s/spec ::even-spec)) ;= :user/label-even (s/def ::label-odd (s/spec ::odd-spec)) ;= :user/label-odd (s/conform (s/cat ::label-even ::even-spec, ::label-odd ::odd-spec) [2 3]) ;= {:user/label-even 2, :user/label-odd 3} (s/conform (s/cat ::label-odd ::even-spec) [2]) ;= {:user/label-odd 2} (s/valid? (s/keys) *1) ;= false (s/explain (s/keys) *2) ; In: [:user/label-odd] val: 2 fails spec: :user/label-odd at: [:user/label-odd] predicate: odd? ;= nil ``` On Saturday, June 18, 2016, Leon Grapenthin <grapenthinl...@gmail.com> wrote: > Assume I parse with conform. > > Then I have functions that operate on the value returned by conform. I > want to spec them. > > But I can't get a spec for the value returned by conform (so that I can > spec said functions) automatically. > > Imagine `(s/conform-spec ::my-spec)` would return the spec of the result > of calling (s/confom ::my-spec foo) > > So it would probably be valuable if a spec could give a spec of what its > conform* returns` > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > <javascript:_e(%7B%7D,'cvml','clojure@googlegroups.com');> > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > <javascript:_e(%7B%7D,'cvml','clojure%2bunsubscr...@googlegroups.com');> > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com > <javascript:_e(%7B%7D,'cvml','clojure%2bunsubscr...@googlegroups.com');>. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Spec of conform of spec
I am not sure whether I understand what you mean. Behavior of conform for predicates is to return its return value if it is logically true, ::s/invalid otherwise. Thus the predicate itself is the spec to its conform*. s/conformer is only limiting as much as it is to unform, a user would have to provide a spec for conforms result as well as he has to provide an unform-fn if he wants unforming. If each spec implemented a conform-spec* method, a spec could very well provide a spec of it's conform. On Saturday, June 18, 2016 at 5:03:33 PM UTC+2, Alex Miller wrote: > > Given that conform takes an arbitrary (opaque) function, I don't think > that's generically possible. > > > On Saturday, June 18, 2016 at 7:37:33 AM UTC-5, Leon Grapenthin wrote: >> >> Assume I parse with conform. >> >> Then I have functions that operate on the value returned by conform. I >> want to spec them. >> >> But I can't get a spec for the value returned by conform (so that I can >> spec said functions) automatically. >> >> Imagine `(s/conform-spec ::my-spec)` would return the spec of the result >> of calling (s/confom ::my-spec foo) >> >> So it would probably be valuable if a spec could give a spec of what its >> conform* returns` >> >> -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Spec of conform of spec
Given that conform takes an arbitrary (opaque) function, I don't think that's generically possible. On Saturday, June 18, 2016 at 7:37:33 AM UTC-5, Leon Grapenthin wrote: > > Assume I parse with conform. > > Then I have functions that operate on the value returned by conform. I > want to spec them. > > But I can't get a spec for the value returned by conform (so that I can > spec said functions) automatically. > > Imagine `(s/conform-spec ::my-spec)` would return the spec of the result > of calling (s/confom ::my-spec foo) > > So it would probably be valuable if a spec could give a spec of what its > conform* returns` > > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Spec of conform of spec
Assume I parse with conform. Then I have functions that operate on the value returned by conform. I want to spec them. But I can't get a spec for the value returned by conform (so that I can spec said functions) automatically. Imagine `(s/conform-spec ::my-spec)` would return the spec of the result of calling (s/confom ::my-spec foo) So it would probably be valuable if a spec could give a spec of what its conform* returns` -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.