Fork me on GitHub
#clojure-spec
<
2017-01-30
>
mrg20:01:48

Is there a good way to generate an increasing number as id in a variable-length collection?

mrg20:01:51

i.e. i have this:

mrg20:01:44

now if i generate subsegments, how would I get the first one to have the ::id 1, the second one (if there is one) the ::id 2, and the third one (if there is one) the ::id 3 ?

alexmiller20:01:22

you can’t make that happen automatically, but you could build a generator that did that

alexmiller20:01:38

I would create a custom generator for ::subsegments using gen/fmap - use the default generator to create, then in the fmap function replace the ::id values to have the index

mrg21:01:01

I'll give that a shot @alexmiller .

mrg21:01:24

How would you keep the index state? There doesn't seem to be a fmap-with-index, nor a multi-arity version where I could pass in a range or something and zip them together

alexmiller22:01:31

Yeah, you can just map over the generated value and range

mrg22:01:10

Do you have an example or a pointer to somewhere that would explain that?

mrg22:01:29

I don't quite grok generators yet

jr22:01:02

Is there a way to spec a mapargs fn? for example:

(defn foo [& {:keys [bar]}] bar)
(foo :bar "value of bar")

joshjones22:01:20

(s/def ::bar string?)
(s/def ::baz int?)

(s/def ::mapargs (s/keys* :req-un [::bar ::baz]))

(defn foo [& {:keys [bar baz]}] bar)
(s/fdef foo :args ::mapargs)

(stest/instrument `foo)
(foo :bar "value of bar" :baz 42)
@jr

jr22:01:20

@joshjones wow that's great. thanks!

jr22:01:50

I didn't realize that keys* is different in that it transforms a sequence of values into a map

joshjones23:01:30

just modified the above example to remove cat which was not necessary fyi

jr23:01:29

no worries I got the gist of it. I was specing a macro with the following structure

(defui foo []
  :tracks [name [:path :to :value]]
  [:h1 name])

jr23:01:01

where the options like :tracks are optional