This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-01-26
Channels
- # aws-lambda (1)
- # beginners (71)
- # boot (70)
- # bristol-clojurians (1)
- # cider (2)
- # clara (13)
- # cljs-dev (96)
- # cljsjs (6)
- # cljsrn (5)
- # clojure (74)
- # clojure-android (3)
- # clojure-austin (4)
- # clojure-dev (10)
- # clojure-russia (6)
- # clojure-spec (28)
- # clojure-uk (128)
- # clojurescript (64)
- # cursive (2)
- # datascript (18)
- # datomic (116)
- # dirac (1)
- # emacs (12)
- # events (10)
- # hoplon (109)
- # jobs (1)
- # jobs-discuss (21)
- # leiningen (2)
- # luminus (6)
- # off-topic (19)
- # om (21)
- # om-next (5)
- # onyx (4)
- # parinfer (29)
- # perun (20)
- # re-frame (53)
- # reagent (21)
- # remote-jobs (5)
- # ring-swagger (2)
- # spacemacs (6)
- # untangled (42)
- # vim (5)
oh, another total noob question.. I need a spec that generates vector of 1 to 5 elements, each element is of another spec (map), something like this:
(s/def ::col (s/keys :req-un [::id ::title]))
(gen/generate (s/gen (s/+ ::col)))
but it has to be 1) a vector 2) of 1 to 5 elements(s/with-gen vector? #(gen/vector (s/gen ::col) 2 5))
ain’t good, I need spec to conform to vector of ::col
@ag this?
(s/def ::id (s/int-in 1 100))
(s/def ::title string?)
(s/def ::col (s/keys :req-un [::id ::title]))
(s/def ::cols (s/coll-of ::col :kind vector?
:min-count 1
:max-count 5))
(gen/generate (s/gen ::cols))
@ag for your prior question, see s/fspec
To me they have very different uses: we use spec for input validation (and some coercion) so we have error handling that says (if (s/valid? ::spec value) (do-good-stuff) (handle-error))
— that’s not the same as (s/assert …)
I personally don’t much like asserts — I’ve never liked them in any language — and part of that is because folks tend to turn them off in production, which is when it’s even more critical that your code doesn’t attempt to operate on invalid data. Seems backwards to me...
good points @seancorfield…thx!
specs being macros is what gives them reasonable reporting on failures, a spec created at runtime would have to either do away with a lot of the error description, or have a very clunky api. there was some talk of some non-macro versions of some of the spec api maybe in the future though
@moxaj why is eval
evil?
@tbaldridge well, most of the time when you use it, there's a better solution (there are exceptions of course)
That doesn't make it evil though 🙂
I say this because the "eval is evil" trope is mis-applied in Lisps. Eval is evil in a language like Javascript (where the phrase comes from) where eval means concat-ing strings together. In Clojure eval is akin to something like stored procs in SQL. It makes code injection really hard
So all that being said, macros + eval works pretty well for dynamic specs, and done correctly you could even get pretty good source code mappings
fair enough