This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-09-27
Channels
- # beginners (34)
- # boot (15)
- # cider (7)
- # cljs-dev (7)
- # cljsjs (2)
- # cljsrn (46)
- # clojure (130)
- # clojure-argentina (1)
- # clojure-colombia (2)
- # clojure-greece (1)
- # clojure-italy (53)
- # clojure-losangeles (1)
- # clojure-russia (15)
- # clojure-spec (8)
- # clojure-uk (100)
- # clojurescript (117)
- # core-matrix (1)
- # cursive (24)
- # datomic (41)
- # duct (1)
- # emacs (11)
- # fulcro (22)
- # graphql (4)
- # hoplon (3)
- # jobs (1)
- # lein-figwheel (3)
- # luminus (18)
- # lumo (52)
- # off-topic (57)
- # pedestal (2)
- # planck (12)
- # re-frame (22)
- # remote-jobs (1)
- # ring-swagger (6)
- # rum (7)
- # shadow-cljs (13)
- # yada (19)
Is there a better way to parameterize generators than to use macros? In this case, I’d like to reuse a generator in different specs with different parameters:
(defmacro limited-string-m [lim]
`(gen/such-that (fn [s#]
(not (blank? s#)))
(gen/sized
(fn [size#]
(gen/resize (dec ~lim) gen/string-alphanumeric)))))
@waffletower at a glance that looks like a macro that could trivially be a function you don't generally need to write any macros to build generators
@mrchance maybe you could do it with https://github.com/stathissideris/spec-provider
@stathissideris oh nice, that looks like a helpful recommendation in any case!
@gfredericks Thanks! works as a function as well
(defn limited-string-fn [lim]
(gen/such-that (fn [s]
(not (blank? s)))
(gen/sized
(fn [size]
(gen/resize (dec lim) gen/string-alphanumeric)))))
@waffletower btw, if you want to retain the gradual-growth property of the builtin generator, you could replace (gen/resize (dec lim) ...)
with (gen/scale #(min % (dec lim)) ...)
thanks, I had noticed that side effect