Fork me on GitHub
#clojure-spec
<
2016-11-04
>
mpenet11:11:37

just wondering: why in multispec we have to repeat the dispatch fn/key in s/multi-spec call itself (since it's already in the defmulti definition)

mpenet11:11:28

-> retagging

Alex Miller (Clojure team)12:11:34

You can use multispec on cases other than keyword maps too - in that case you need a fn for retagging instead.

mpenet13:11:59

yup, makes sense

mpenet13:11:10

it's not obvious from the guide tho

mpenet13:11:21

but the docstring is clear about it

sveri17:11:00

Lets say I have two specs A and B and now I have a function that has a paramater that is the union of the two maps so like (keys ::A ::B). How would I define that spec?

sveri17:11:17

I tried a (merge ::A ::B) but that does not work

bfabry18:11:43

@sveri did you use core/merge or spec/merge?

sveri18:11:23

@bfabry Ah, I did not know there was a spec/merge, thank you, I used the core one. With spec/merge it works 🙂

d._.b23:11:30

I'm feeling dumb. I have a map like

{:abc "abc" ; required key, and must have a value of "abc"
 :x {:y nil ; required key, but value can be anything
     :z 123 ; same as above}
}

d._.b23:11:38

How do I spec this?

bfabry23:11:14

(s/def :your/abc #{"abc"}) (s/def :your/x (s/keys :req-un [:your/y :your/z])) (s/def :your/y any?) (s/def :your/z any?) (s/keys :req-un [:your/abc :your/x])

d._.b23:11:48

(s/def ::abc "abc")
(s/def ::x (s/map-of :req-un [keyword? identity])
(s/def ::my-map (s/keys :req-un [::abc ::x]))

d._.b23:11:03

@bfabry thank you, i knew i was missing something

d._.b23:11:44

is it possible to also use every-kv or map-of?

bfabry23:11:03

anything is possible

d._.b23:11:13

haha, im just trying to understand when to reach for map-of or every-kv

d._.b23:11:17

and when not to

bfabry23:11:43

reach for s/keys if you know what your keys will be

d._.b23:11:46

in my example, i know that all of the k/v pairs will be :key any?

d._.b23:11:43

@bfabry why do you not s/def the last line in your example?

bfabry23:11:05

shrug I didn't need to refer to it by a name

bfabry23:11:09

if I did I would

d._.b23:11:38

Is there a way to def multiple things at once?

d._.b23:11:29

Use for or doseq to generate the defs? What's idiomatic?

d._.b23:11:33

Something like (doseq [k [::abc :def]] (s/def ~k any?))` ?

d._.b23:11:54

I think I must just be missing something obvious

d._.b23:11:48

If I have like 20 keys which are req-un in a map, all of which should have values checked of any?, that seems like it should be more straightforward than resorting to doseq'ing to generate s/defs

d._.b23:11:58

Sorry for the repost but, just to reiterate:

{
 :abc "abc" ; required key, and must have a value of "abc"
 :x {:y nil ; required key, but value can be anything
     :z 123 ; same as above
     :w :wow ; same as above
     :o :out ; same as above
  }
}

d._.b23:11:48

(s/def ::abc #{"abc"})
(s/def ::x (s/keys :req-un [::y ::z ::w ::o]))

(s/def ::y any?)
(s/def ::z any?)
(s/def ::w any?)
(s/def ::o any?)

(s/def ::entire-thing (s/keys :req-un [::abc ::x]))

d._.b23:11:59

Do I really need to write all 4 of those defs?

d._.b23:11:52

I was thinking I could (s/every-kv #{::y ::z ::w ::o} any?) or something... No?