Fork me on GitHub
#clojure-spec
<
2018-06-18
>
metametadata00:06:57

Hi. I've noticed that CLJS impl of explain prints more stuff than Clojure. Is it on purpose? CLJS:

=>  (s/explain ::x 23)
val: 23 fails spec: :cljs.user/x predicate: string?
:cljs.spec.alpha/spec  :cljs.user/x
:cljs.spec.alpha/value  23
Clojure:
=>  (s/explain ::x 23)
val: 23 fails spec: :cljs.user/x predicate: string?

4
zclj11:06:35

If I would like instrument to check my specs, what would be the preferred way of spec:ing a multimethod? Can I use a multispec with the same dispatch fn or do I need an indirection fn with a spec in each defmethod?

Alex Miller (Clojure team)11:06:48

You can make the defmulti dispatch function explicit and spec that

zclj17:06:40

In my case each defmethod gets called with a map and the map should have different contents for each method. As I read your answer it would be the same spec for all methods?

misha12:06:12

@kenny I wonder if qualified symbols can be replaced like this:

(case form-sym
      (clojure.spec.alpha/keys
        cljs.spec.alpha/keys) ...
;=>

    (case form-sym
      `s/keys ...

misha12:06:50

given you already required [clojure.spec.alpha :as s]

misha12:06:26

then, getting rid of alpha would be just 1 line :require update

valerauko14:06:44

I recall that spec was inspired in part by RDF. Is there any recommended way for describing XML structures?

Alex Miller (Clojure team)14:06:34

xml represented by what data structure?

valerauko14:06:00

I'm using clojure.data.xml, so whatever that uses internally.

valerauko14:06:58

Its sexp-as-element is the closest I've seen it get to simple data structures, but even that seems to be pretty complex to spec

Alex Miller (Clojure team)14:06:38

seems pretty straightforward?

valerauko14:06:23

I'm afraid I don't follow

Alex Miller (Clojure team)14:06:41

there are at least two formats there - the input and output of sexp-as-element. they both seem pretty straightforward to spec if you want to

valerauko14:06:38

the output's clojure.data.xml.Elements, right?

Alex Miller (Clojure team)14:06:58

Element is a defrecord, so you can just spec it as a map with 3 known keys

Alex Miller (Clojure team)14:06:57

the sexp form is just structured vectors and can be spec’ed with s/cat etc

Alex Miller (Clojure team)14:06:10

oh, there actually are specs already in data.xml

Alex Miller (Clojure team)14:06:56

that’s for the element forms

valerauko15:06:25

it's not in the stable release yet though, is it?

valerauko15:06:50

i see you bumped its clojure dependency up to 1.9 when specs were introduced

valerauko15:06:51

as you pointed out i can just spec them as maps with :tag :attrs and :content

lwhorton18:06:17

if I want to declare a with-gen ::spec generator-fn, generator fn needs to be a fn that returns a generator. how does one create a generator that’s simply a function, without using fmap or any of the other test.check.generators?

lwhorton18:06:52

for example I just want a “generator” that invokes (random-uuid) where the fn itself is already random and doesn’t need any randomness from the generator scaffolding

lwhorton18:06:36

i had some garbage like

(s/exercise ::p/id 10 {::p/id (fn [] (gen/fmap #(random-uuid) (s/gen (s/int-in 1 10))))})
but this is abusing fmap to just give me access to a fn I can call that will return a value that’s wrapped in the proper generator

guy18:06:05

maybe (gen/return .. ) ?

guy18:06:35

(gen/return (random-uuid)) maybe?

guy18:06:06

https://clojure.github.io/test.check/generator-examples.html

(def int-or-nil (gen/one-of [gen/int (gen/return nil)]))
(gen/sample int-or-nil)
;; => (nil 0 -2 nil nil 3 nil nil 4 2)

guy18:06:19

thats using gen/return to always return nil

guy18:06:37

I'm not entirely sure its what you are looking for though 😞

lwhorton18:06:25

that seems like the same idea as abusing fmap and an existing gen (int-in), but with different fns. it just feels like there’s a built-in fn like gen/return for this use case, right?

Alex Miller (Clojure team)21:06:21

using external sources of randomness prevents shrinking and repeatability so is discouraged

gfredericks22:06:41

if you want to do the discouraged thing, fmap will work and return won't.