This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-10-11
Channels
- # aleph (7)
- # bangalore-clj (11)
- # boot (70)
- # cider (11)
- # cljsjs (3)
- # cljsrn (17)
- # clojure (88)
- # clojure-brasil (8)
- # clojure-dev (17)
- # clojure-greece (1)
- # clojure-italy (6)
- # clojure-poland (8)
- # clojure-russia (2)
- # clojure-spec (44)
- # clojure-uk (32)
- # clojurescript (176)
- # cloverage (2)
- # component (5)
- # cursive (3)
- # datomic (23)
- # defnpodcast (6)
- # dirac (15)
- # emacs (6)
- # garden (19)
- # hoplon (126)
- # leiningen (1)
- # off-topic (3)
- # om (66)
- # onyx (56)
- # re-frame (8)
- # reagent (7)
- # ring-swagger (43)
- # specter (18)
- # untangled (110)
- # vim (3)
- # yada (39)
I'm calling explain on a spec and it's returning nil
, why would that be?
answer: i need to use explain-str
, explain
just prints to out
Interesting clojure.spec + datomic question: http://stackoverflow.com/questions/39971727/using-clojure-spec-with-datomic-entities
how would you create a spec for the following:
1) ns store
- defines ::state
as (s/keys :req [::data ::metadata])
where ::data
is a map?
2) ns x
produces a ::state
with concrete ::data
How would the ::x-state
spec be defined? (s/and ::store/state #(s/assert ::x-data (::store/data %)))
doesn't look right...
On it's own, [1 1]
conforms to the ::pattern
spec
(ns example.paths
(:require
[cljs.spec :as s]
[cljs.spec.impl.gen :as gen]
[cljs.spec.test :as test]))
(s/def ::keyword-or-string
(s/with-gen
(s/or :keyword keyword?
:string string?)
#(s/gen #{"a" "b" :c :d :e/z})))
(def path (s/coll-of ::keyword-or-string
:min-count 1
:kind vector?))
(s/def ::path
(s/with-gen
path
(fn []
(s/gen
(gen/such-that
#(-> % count (< 5))
(s/gen path))))))
#object[Error Error: Unable to construct gen at: [] for: [object Object]]
Error: Unable to construct gen at: [] for: [object Object]
So I am trying to reduce the size of generated ::paths
to at most be of length 5. It isn’t part of the actual spec for a ::path
, but it is something I would like when generating, any idea what I am doing wrong?
@samedhi there is a 20% chance that what i’m about to say is relevant, but i’ll say it just in case
this is definitely a thing if your :kind is set?
, you need to define :into #{}
for generators to work afaict
(ns example.paths (:require [cljs.spec :as s] [cljs.spec.impl.gen :as gen] [cljs.spec.test :as test]))
(s/def ::keyword-or-string
(s/with-gen
(s/or :keyword keyword?
:string string?)
#(s/gen #{"a" "b" :c :d :e/z})))
(gen/generate (s/gen ::keyword-or-string)) => :d
(def path (s/coll-of ::keyword-or-string
:min-count 1
:kind vector?
:into []))
(gen/generate (s/gen path)) => [“a” :c “b” “b” “b” “b” “a”]
(s/gen
(gen/such-that
#(-> % count (< 5))
(s/gen path)))
=> (errors with)
#object[Error Error: Unable to construct gen at: [] for: [object Object]]
Error: Unable to construct gen at: [] for: [object Object]
...
@jrheard is this what you mean? In this case I am trying to return a vector of keywords or strings, so I set :kind
to vector?
and :into
to []
(s/def ::path
(s/with-gen
path
(fn []
(s/gen
(gen/such-that
#(-> % count (< 5))
(s/gen path))))))
I think I am basing it off of this from http://clojure.org/guides/spec#_explain
(s/def ::kws (s/with-gen (s/and keyword? #(= (namespace %) "my.domain"))
#(s/gen #{:my.domain/name :my.domain/occupation :my.domain/id})))
> Note that with-gen (and other places that take a custom generator) take a no-arg function that returns the generator, allowing it to be lazily realized.
(s/def ::path
(s/with-gen
path
(fn [] (gen/such-that #(-> % count (< 5)) (s/gen path)))))
Thanks, that helped me out a lot, data seemed to just take forever on generating without this. (though that may actually be my formater and not the actual generation). Fixed issues.
@alexmiller Suggestion: a special s/cat, primarily intended for specifying args in fdef, that automatically calls s/spec
on each of its arguments
@alexmiller I feel like a lot of people have bumped their toe on the nested regex gotcha
I think that’s unlikely something we would add
and in general, I don’t find that that is (usually) what you want
it seems more common from specs I’ve written to have s/cat of s/coll-of for functions. For macros, you sometimes have this but I find it’s really important in that case to develop the awareness of when you are really needing to drop nested levels - that’s critical to build reusable parts, and often for making workable recursive specs
I think just making this clearer in the guides, in the specing functions section, would help
I found where it's mentioned now, but not when I was trying to work out what was wrong
You could argue I should have read the guide more thoroughly