This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-07-24
Channels
- # admin-announcements (2)
- # beginners (5)
- # boot (29)
- # cider (46)
- # cljsjs (1)
- # clojure (19)
- # clojure-quebec (1)
- # clojure-russia (73)
- # clojure-spec (30)
- # clojure-uk (23)
- # clojurescript (35)
- # datascript (12)
- # datomic (7)
- # emacs (26)
- # hoplon (168)
- # leiningen (7)
- # off-topic (2)
- # om (32)
- # perun (8)
- # protorepl (8)
- # ring (45)
- # specter (10)
- # test-check (2)
- # yada (6)
Namespaced keywords should be fine in edn
Autoresolved keywords are not part of edn though (as there is no namespace context)
Do you have an example?
What exactly does calling spec with a map do? map-spec-impl suggests I can do that, but spec’s docstring only mentions predicates and regex specs; I guess a map can behave like a predicate since it’s an IFn?
Is there an easy way to get a keys that’s not open for extension? I’m digging into map-spec-impl and just extracting the generator bit isn’t super trivial.
in short, no
you can s/and with a restriction on the key set
I guess I can fmap over the gen I get from keys, and then dissoc the noise it comes up with I don’t want
(I understand the general argument for having extra keys; it just doesn’t make sense for what I’m doing)
@alexmiller: Thanks! Much appreciated 🙂
yeah, def leverage the existing specs as much as possible for gen
When using with-gen, is there some kind of automatic spec verification, or are generators assumed to be correct
My excl-keys macro ostensibly works, but merging it doesn’t seem to.
(defmacro excl-keys
"Like [[s/keys]], but closed for extension."
[& {:keys [req-un opt-un req opt] :as keys-spec}]
(let [bare-un-keys (map (comp keyword name) (concat req-un opt-un))
all-keys (set (concat bare-un-keys req opt))]
`(let [ks# (s/keys ~@(apply concat keys-spec))]
(s/with-gen
(s/and ks# (fn [m#] (set/subset? (set (keys m#)) ~all-keys)))
(fn [] (gen/fmap (fn [m#] (select-keys m# ~all-keys)) (s/gen ks#)))))))
(s/def ::a (excl-keys :opt-un [::b] :req-un [::c]))
(s/def ::b string?)
(s/def ::c keyword?)
(gen/sample (s/gen ::a))
(s/def ::x (excl-keys :opt-un [::y] :req-un [::z]))
(s/def ::y integer?)
(s/def ::z rational?)
(gen/sample (s/gen ::x))
(s/def ::mix (s/merge ::a ::x))
(gen/sample (s/gen ::mix))
Samples for ::a
, ::x
work fine, merging less so. I guess I’ll dive into how s/merge builds generators 🙂(deftest gen-let-macro
(s/def ::a (s/spec keyword?))
(s/def ::b (s/int-in 7 42))
(let [t (s/tuple keyword? string?)
m (s/keys :req [::a ::b])]
(are [spec generator]
(s/valid? spec (gen/generate generator))
t (gen/let [x :abc, y "efg"] [x y])
t (gen/let [x ::a, y string?] [x y])
t (gen/let [[_ y] t] [::a y])
t (gen/let [] [keyword? string?])
t (gen/let [] t)
t (gen/let [[x y] (gen/let [] t)] [x y])
m (gen/let [{a ::a, b ::b} m] {::a a, ::b b})
m (gen/let [a keyword?] {::a a, ::b ::b})
m (gen/let [] {::a keyword?, ::b 7})
m (gen/let [] {::a keyword?, ::b #{7 8 9}})
int? (gen/let [i 'clojure.test.check.generators/int] i)
#{'abc} (gen/let [x 'abc] x))))
My idea is that it's really annoying having to coerce specs, predicates and constants to generators so it just does it automagically
I'll wait for @alexmiller to tell me if he likes it or not before I attempt a patch
hi there! how to specify :ret value for fdef if function returns void?
:ret nil? or :ret #(nil? %) ?
@alexmiller: Since you're a category theory fan, I could generalize let
from generators to specs if we model specs as having type (a -> Bool, Gen a)
(á la Haskell)
@mike1452 either will work, I'd use the shorter one
@lvh re with-gen, the generator is not trusted and all generated samples will be verified by checking the spec as well
@mike1452: sorry read further back - functions are never void in Clojure, only comes up in some interop cases