This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-07-10
Channels
- # arachne (2)
- # beginners (5)
- # boot (6)
- # cider (3)
- # cljsrn (2)
- # clojure (34)
- # clojure-brasil (1)
- # clojure-poland (1)
- # clojure-spec (52)
- # clojure-sweden (1)
- # clojurescript (17)
- # datomic (14)
- # leiningen (3)
- # luminus (2)
- # om (85)
- # proton (89)
- # protorepl (1)
- # re-frame (1)
- # reagent (8)
- # ring-swagger (1)
- # spacemacs (2)
- # yada (6)
@donaldball: I get that error when I execute in a deftest.
I get it both when executing in a deftest and in cider, though it works fine from a repl
I was just experimenting with ignoring Rich’s advice and expressing specs for strings as spec regexes. I find I actually quite like a couple of things that fall out: the form is easier to understand that the string regex, and you get a generator for free.
But unless I’m missing something, you pretty much have to write one spec for the seq of chars and another spec for the string that unwraps it as a seq
Setting aside the merits of the idea, is there a way I could do with s/and
and a conformed value?
hey all, I'm trying to figure out if I can use spec for parsing strings (sentences) into structured data. I'm struggling with whether or not I will need to split
around spaces, and how to handle things like phrases (sequences of words) if I use s/cat
. is this kind of use case intended at all, or am I better off with something else?
(maps of integers from 0 to 5, increasing in value as the index increases, where one of the values must be 1.0 and all of the values must be within [0,1])
I can generate these things with this function:
(defn generate-random-map
"Return a random structured map"
[]
(let [values (sort (repeatedly 6 rand))
pivot-value (last values)]
(into {} (map-indexed vector (map #(/ % pivot-value) values)))))
I'd rather not rope in test.check. Too bad that clojure.spec doesn't let you register generators separately from specs
Because I wouldn't mind associating a generator with my specs in my unit tests where I do use test.check
xcthulhu: I think clojure.spec is designed with the intention that you rope in test.check for non-trivial generator needs
is it just that you don't want test.check required in a non-test namespace?
But like I said, they do give you enough so you don't need test.check, it's just clumsy
where's fmap?
clojure.spec.gen/fmap?
you could probably rewrite my code using clojure.spec.gen if that's what you really want
looks like gen/shuffle isn't there
@xcthulhu: aren’t you already pulling in test.check
as a transitive dep if yr using something the gen
namespace?
not necessarily
since it's lazy loaded you can get away with not having test.check available if you never run the generators/tests
which I think is the whole point of the lazy loading
I wonder how they do lazy loading in cljs
https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/spec/impl/gen.cljc
oh man
a custom var deftype
Yeah, it's pretty hacks. Anyway, if I read this right they lazily load vector from test.check. Sadly, clojureScript doesn't give you double*, only double
Here's the generator I ended up going with BTW:
(gen/fmap
(fn [values]
(let [max-value (apply max values)
min-value (apply min values)]
(->> values
sort
(map #(-> % (- min-value) (/ (- max-value min-value))))
(drop 1)
(zipmap (range)))))
(gen/vector
(gen/such-that (complement #{infinity, (- infinity)}) (gen/double))
7))
infinity
is defined to be Double/INFINITY
and js/Number.INFINITY
in the jvm and js respectively
that should still give you nans though
gen/double generates nans
should be high enough to matter
unless I screwed something up
roughly 1% of the time
Here we go:
(defn nan?
"Tests if a value is a NaN or not"
[x]
#?(:clj (and (double? x) (.isNaN x))
:cljs (js/isNaN x)))
;;;;;;;;;;;;;;;;
(gen/fmap
(fn [values]
(let [max-value (apply max values)
min-value (apply min values)]
(->> values
sort
(map #(-> % (- min-value) (/ (- max-value min-value))))
(drop 1)
(zipmap (range)))))
(gen/vector
(gen/such-that (complement (some-fn #{infinity, (- infinity)} nan?))
(gen/double))
7))