Re: Is there an easy way for s/keys spec to work against both qualified/unqualiffied keys at the same time?

2016-10-17 Thread Mauricio Aldazosa
On Sun, Oct 16, 2016 at 11:40 PM, Ikuru Kanuma  wrote:

> Mauricio, thanks for the response!
> I agree that that gets what I asked for done, but that solution is in
> essence writing the same
> qualified/unqualified version of the spec twice and sounded redundant,
> which lead to my question.
> I guess it is what it is in that case...
>

Oh I see, maybe the reduncancy can be removed by creating a macro.
Something like:

(defmacro qualified-or-unqualified
  [k & specs]
  `(s/def ~k
 (s/or :qualified (s/keys :req ~specs)
   :unqualified (s/keys :req-un ~specs

And then defining the new specs would be something like:

(qualified-or-unqualified ::map-with-numbers ::some-number)
(s/valid? ::map-with-numbers {::some-number 3}) => true
(s/valid? ::map-with-numbers {:some-number 3})  => true

I'm sure the proposed macro is by no means complete, but I hope it points
you to one possible solution.

Cheers,
Mauricio

-- 
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: Is there an easy way for s/keys spec to work against both qualified/unqualiffied keys at the same time?

2016-10-16 Thread Ikuru Kanuma
Mauricio, thanks for the response!
I agree that that gets what I asked for done, but that solution is in 
essence writing the same 
qualified/unqualified version of the spec twice and sounded redundant, 
which lead to my question.
I guess it is what it is in that case...

Leon, thanks for the response.
what I meant to say by 'exclusive' is the 'same'(difference in namespaced 
or not)
keyword that get checked by req/req-un options.
i.e. req only checks for the qualified version and req-un only checks for 
the unqualified version.
Sorry, should have been clearer.

-- 
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: Is there an easy way for s/keys spec to work against both qualified/unqualiffied keys at the same time?

2016-10-14 Thread Leon Grapenthin
They are not exclusive

(s/def ::baz (skeys :req [::foo] :req-un [::bar]))

works.

On Friday, October 14, 2016 at 7:07:39 AM UTC+2, Ikuru Kanuma wrote:
>
> Hi,
>
> I was reading the official spec guide's explanation for the s/keys macro, 
> played around with it a bit,
> and was a little surprised that the :req and :req-un are exclusive specs 
> as illustrated below:
>
> (ns my.ns
>   (:require [clojure.spec :as s]))
>
> (s/def ::some-number int?)
> (s/def ::map-with-numbers1 (s/keys :req [::some-number]))
> (s/valid? ::map-with-numbers1 {::some-number 3}) => true
> (s/valid? ::map-with-numbers1 {:some-number 3}) => false
>
> (s/def ::map-with-numbers2 (s/keys :req-un [::some-number]))
> (s/valid? ::map-with-numbers2 {::some-number 3}) => false
> (s/valid? ::map-with-numbers2 {:some-number 3}) => true
>
> In other words, would there a convenient way to specify a spec that would 
> work for both qualified/unqualified keys at the same time?
> ;;Want a spec that validates as follows
> (s/valid? ::map-with-numbers {::some-number 3}) => true
> (s/valid? ::map-with-numbers {:some-number 3}) => true
>
> Regards,
>
> Ikuru
>

-- 
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: Is there an easy way for s/keys spec to work against both qualified/unqualiffied keys at the same time?

2016-10-14 Thread Mauricio Aldazosa
Hi,

You can achieve that using *spec/or*:

(s/def ::map-with-numbers
  (s/or :qualified ::map-with-numbers1
:unqualified ::map-with-numbers2))

(s/valid? ::map-with-numbers {::some-number 3}) => true
(s/valid? ::map-with-numbers {:some-number 3}) => true
​
Cheers,
Mauricio

-- 
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.


Is there an easy way for s/keys spec to work against both qualified/unqualiffied keys at the same time?

2016-10-13 Thread Ikuru Kanuma
Hi,

I was reading the official spec guide's explanation for the s/keys macro, 
played around with it a bit,
and was a little surprised that the :req and :req-un are exclusive specs as 
illustrated below:

(ns my.ns
  (:require [clojure.spec :as s]))

(s/def ::some-number int?)
(s/def ::map-with-numbers1 (s/keys :req [::some-number]))
(s/valid? ::map-with-numbers1 {::some-number 3}) => true
(s/valid? ::map-with-numbers1 {:some-number 3}) => false

(s/def ::map-with-numbers2 (s/keys :req-un [::some-number]))
(s/valid? ::map-with-numbers2 {::some-number 3}) => false
(s/valid? ::map-with-numbers2 {:some-number 3}) => true

In other words, would there a convenient way to specify a spec that would 
work for both qualified/unqualified keys at the same time?
;;Want a spec that validates as follows
(s/valid? ::map-with-numbers {::some-number 3}) => true
(s/valid? ::map-with-numbers {:some-number 3}) => true

Regards,

Ikuru

-- 
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.