This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-06
Channels
- # announcements (2)
- # beginners (97)
- # boot (3)
- # cider (23)
- # clara (9)
- # cljs-dev (40)
- # cljsrn (6)
- # clojure (107)
- # clojure-finland (2)
- # clojure-india (3)
- # clojure-italy (15)
- # clojure-nl (2)
- # clojure-spec (107)
- # clojure-uk (91)
- # clojurescript (28)
- # cursive (10)
- # data-science (4)
- # datomic (26)
- # duct (1)
- # emacs (6)
- # events (9)
- # figwheel-main (4)
- # fulcro (4)
- # graphql (2)
- # jobs (3)
- # jobs-discuss (12)
- # juxt (7)
- # kaocha (6)
- # off-topic (8)
- # onyx (2)
- # parinfer (13)
- # pedestal (32)
- # portkey (1)
- # re-frame (58)
- # reagent (17)
- # reitit (21)
- # ring-swagger (3)
- # shadow-cljs (35)
- # spacemacs (1)
- # tools-deps (33)
- # yada (13)
is there a way to spec map's key+value, but the key isn't specific
like how do I say: map key is symbol or keyword, if key is symbol then value must be string but if key is keyword then value must be integer
I tried s/coll-of :kind map?
but that only conforms value
Is this useful to you perhaps? https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html#clojure.spec.alpha/map-of
@roklenarcic can the map also have combinations of these key/val pairs?
You can use s/every with an s/or of s/tuple for entry combinations
Self-hosted cljs with spec in the browser: https://twitter.com/borkdude/status/1059758785613479937
thank you alex, I'll try that
hm well every doesn't conform
hm... when using coll-of, if I specify kind as map? and if I don't specify kind, conforming works differently 😄
@roklenarcic specify it as a seq of tuples?
tuple spec is no better than map-of spec... each item is independent
(s/tuple pred1 pred2)
you can't link type of 1 and type of 2
so s/coll-of that?
(s/conform (s/coll-of (s/or :s1 (s/tuple symbol? string?) :s2 (s/tuple keyword? int?))) '{a "A" :b 3})
=> {:s1 [a "A"], :s2 [:b 3]}
this is completely different to how I would expect things to go
I would expect a collection of tuples
@roklenarcic you mean the conformed value?
this looks completely wrong and unexpected to me
(s/conform (s/coll-of (s/or :s1 (s/tuple symbol? string?) :s2 (s/tuple keyword? int?))) '{a "A" :b 3 c "B"})
=> {:s1 [c "B"], :s2 [:b 3]}
see... now I lost a term
input map has 3 key-values
result has two items
nah, I'll just switch back
to :kind map?
and take vals
problem solved
don't worry, I have a solution, I was just looking for something more elegant
yes, (s/conform (s/every (s/or :s1 (s/tuple symbol? string?) :s2 (s/tuple keyword? int?))) '{a "A" :b 3 c "B"})
works
every doesn't do any conforming at all FYI
at least I think so
(s/coll-of x :kind map?)
, conforms key+value into value
so you just have to take (vals )
of value in the map then
(s/conform (s/cat :map-entries (s/* (s/alt :s1 (s/tuple symbol? string?) :s2 (s/tuple keyword? int?)))) '{a "A" :b 3 c "B"})
I don’t think this approach will work on latest spec - regex specs now only work on sequential collections (too confusingly dangerous to do sequential spec’ing on unordered collections)
(s/conform (s/coll-of (s/or :s1 (s/tuple symbol? string?) :s2 (s/tuple keyword? int?))) '{a "A" :b 3 c "B"})
would be better
that was already proposed by me, but that didn’t work, because the result would be: {:s1 [c "B"], :s2 [:b 3]}
and you lose map-entries
(s/conform (s/coll-of (s/or :s1 (s/tuple symbol? string?) :s2 (s/tuple keyword? int?)) :into []) '{a "A" :b 3 c "B"})
=> [[:s1 [a "A"]] [:s2 [:b 3]] [:s1 [c "B"]]]
user=> (s/conform (s/coll-of (s/nonconforming (s/or :s1 (s/tuple symbol? string?) :s2 (s/tuple keyword? int?))) :into []) '{a "A" :b 3 c "B"})
[[a "A"] [:b 3] [c "B"]]
if you want to hide the or tag
well that's clever 🙂
I'm having some problems adding :args
specs to a function, I think it kind of boils down to this:
(spec/def ::title string?)
(spec/def ::entry-x (spec/cat :title ::title))
(spec/valid? ::entry-x ["Title"]) ; => true
(spec/valid? (spec/cat :entry ::entry-x) ["Title"]) ; => true
(spec/valid? (spec/cat :entry ::entry-x) [["Title"]]) ; => false
I would expect the additional cat
to require additional wrapping but it appears that ::entrty-x
and (spec/cat :entry ::entry-x)
are identical for some reason.(s/def ::foo (s/cat :a int? :bcat (s/cat :b int?)))
(s/conform ::foo [1 2]) ;; {:a 1, :bcat {:b 2}}
regex ops nest to describe the same structured collection
if you need a new “level”, insert s/spec
Interesting I would have expected that spec to match [1 [2]]
I guess
this is covered in the spec guide
Ok, s/spec
did the trick. Also found the section in the spec guide now
@roklenarcic please watch the thread, Alex gave some advice there
btw, handy link if you quickly want to check examples posted here: http://app.klipse.tech/?cljs_in=(require%20%27[clojure.spec.alpha%20:as%20s])
is this feature in clojure 1.10 related to upcoming changes in clojure.spec maybe? https://twitter.com/timbaldridge/status/1059815352106795008
no, it’s a genericization of stuff from datafy
although datafy is potentially something we will do in spec, still tbd
what is instance based protocol polymorphism?
in short you could have a custom impl of some protocol fn bound to a value (via metadata) instead of a type
cant you dispatch now based on meta?
sorry for being annoying, but I am very curriuos about it 🙂
https://github.com/clojure/clojure/blob/master/changes.md#22-protocol-extension-by-metadata
i will have a look, thanks!
well, I thought, maybe this way predicates could be bound to keywords and spec could use that
they don't, this is ~ specify
limited to IObj
s (modulo the different method resolution rules around direct impl)
no, I was looking at a related fix for CLJ-1814 but I think somebody should make a ticket just for this specific issue
Here's the thing: https://dev.clojure.org/jira/browse/CLJ-2426 It's my first ticket so I hope it's clear.
Hi, I'm trying to get a feel for where to 'draw the line' on more dynamic behavior in specs. I've created an EDN based data description language for one of my projects. I have it spec'ed out pretty thoroughly, but I have a situation where I have say :data/type
that could be one of a fixed set of types (:string :long, etc) or a type that's defined elsewhere in the larger data structure I'm validating. my code is already doing some additional validation based on core.logic etc once the definitions are loaded. So just wondering if it's better to defer this type of check to that point