Fork me on GitHub
#clojure-spec
<
2016-07-24
>
Alex Miller (Clojure team)01:07:00

Namespaced keywords should be fine in edn

Alex Miller (Clojure team)01:07:34

Autoresolved keywords are not part of edn though (as there is no namespace context)

Alex Miller (Clojure team)01:07:05

Do you have an example?

lvh01:07:45

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?

lvh01:07:37

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.

Alex Miller (Clojure team)02:07:57

you can s/and with a restriction on the key set

lvh02:07:26

yeah; I got that part; trying to fix the generator though

lvh02:07:54

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

lvh02:07:13

(I understand the general argument for having extra keys; it just doesn’t make sense for what I’m doing)

lvh02:07:18

@alexmiller: Thanks! Much appreciated 🙂

Alex Miller (Clojure team)02:07:15

yeah, def leverage the existing specs as much as possible for gen

lvh16:07:51

When using with-gen, is there some kind of automatic spec verification, or are generators assumed to be correct

lvh16:07:06

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 🙂

xcthulhu18:07:57

Hey guys, I've been working on a patch to port test.check/let to clojure.spec.gen

xcthulhu18:07:12

Since we all have fmap blues from time to time.

xcthulhu18:07:16

Here's the unit-test vector I want to push to clojure.test-clojure.spec:

xcthulhu18:07:21

(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))))

xcthulhu18:07:14

That test vector should give a pretty good idea of how gen/let would work.

xcthulhu18:07:56

My idea is that it's really annoying having to coerce specs, predicates and constants to generators so it just does it automagically

xcthulhu18:07:33

I'll wait for @alexmiller to tell me if he likes it or not before I attempt a patch

mike_ananev19:07:39

hi there! how to specify :ret value for fdef if function returns void?

mike_ananev19:07:36

:ret nil? or :ret #(nil? %) ?

xcthulhu19:07:30

I think :ret nil? but I'm not 100%

xcthulhu19:07:33

#(nil? %) is probably wrong

xcthulhu20:07:50

@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)

Alex Miller (Clojure team)20:07:19

@mike1452 either will work, I'd use the shorter one

Alex Miller (Clojure team)20:07:41

@lvh re with-gen, the generator is not trusted and all generated samples will be verified by checking the spec as well

Alex Miller (Clojure team)20:07:20

@mike1452: sorry read further back - functions are never void in Clojure, only comes up in some interop cases