This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-01-03
Channels
- # adventofcode (1)
- # beginners (76)
- # boot (88)
- # cider (63)
- # clojure (357)
- # clojure-austin (2)
- # clojure-berlin (8)
- # clojure-brasil (8)
- # clojure-nl (1)
- # clojure-russia (22)
- # clojure-spec (17)
- # clojure-uk (47)
- # clojurescript (67)
- # cursive (45)
- # datascript (3)
- # datomic (45)
- # dirac (7)
- # emacs (3)
- # funcool (2)
- # hoplon (26)
- # jobs (2)
- # jobs-discuss (11)
- # luminus (6)
- # off-topic (243)
- # om (40)
- # om-next (7)
- # onyx (23)
- # overtone (1)
- # portland-or (2)
- # protorepl (11)
- # re-frame (55)
- # reagent (58)
- # rum (12)
- # sql (4)
- # test-check (12)
- # untangled (25)
What is the best way to spec a string that contains a floating point number? Using a regex or some other method?
So what would the spec look like? In particular, would I have to write a custom generator?
It might look like: (s/spec #(Double/parseDouble %) :gen (gen/fmap str (gen/double)))
Possibly using gen/double*
with some constraints
@donaldball Cool, thanks! Unfortunately I'm in cljs-land, so I think I might have to go the regex path anyway
Just one more thing:
When you make a spec like (s/def ::double-string #(Double/parseDouble %))
we rely on parseDouble
throwing an exception rather than matching a certain predicate, right?
Ended up with:
(def decimal-regex #"-?\d+\.\d+")
(s/def ::double-string (s/spec (s/and string? #(re-matches decimal-regex %)) :gen #(gen/fmap str (gen/double))))
Anything that could be written in a nicer way?if you end up doing that a lot (specing via regex) you might want to check test.chuck for the gen part
@curlyfry not saying a nicer way doesn’t exist, but that way is straightforward and works well, and it’s the way i’d do it
@mpenet @joshjones Sweet, thanks guys!
@seancorfield I know that you answered it with I think a namespace and a function ( artObjects-id:response
) but when I used it in a sdef I did not work