Clojurians
#test-check
<
2017-08-02
>

This page is not created by, affiliated with, or supported by Slack Technologies, Inc.

cddr20:08:37

How might you go about writing a generator of sequences of numbers which add up to n?

gfredericks20:08:25

@cddr depends what kind of distribution you want

gfredericks20:08:59

the easy dumb way to do it is to generate a sequence of numbers and then append (- n sum) to them

gfredericks20:08:38

also depends on what kind of numbers you mean

cddr20:08:09

This is related to my question over in #clojure-spec about a having relations between a top-level amount, and children that should add up to that amount https://clojurians.slack.com/archives/C1B1BB2Q3/p1501686285791508

gfredericks20:08:54

would it be weird to not generate the top-level amount?

gfredericks20:08:09

generate just the children, then set the top-level to the sum?

cddr20:08:38

Yeah that was my first thought too. But then I thought I needed to solve this type of problem anyway but looking back at my code I can't remember why now :slightly_smiling_face:

cddr20:08:35

In the actual example there are a few more relationships between the top-level and the children

gfredericks21:08:14

a very useful tactic as generating something related to what you want, and using gen/fmap to transform it

gfredericks21:08:42

i.e., generate the information, then use fmap to get it into the right format