I just wrote this code:
(defn parameters [transform-factory state] (lazy-seq (let [[r1 state] (uniform state) [r2 state] (uniform state) [t state] (transform-factory state)] (cons [t [r1 r2]] (parameters transform-factory state))))) (defn repeated-transform [mosaic n transform-factory state] (reduce transform-square mosaic (take n (parameters transform-factory state))))
The parameters function generates a lazy sequence of values generated from state , which are used to parameterize the re-conversion of something (in this case, a "mosaic").
it seems to me that parameters shows a fairly common pattern that occurs when you have some state that needs to be transferred (in this case, to generate random values). is there a name for this?
is there a better way to write the first function? related problems can often be solved with reduce , which “transfers” the state, but here I have nothing to reduce. likewise, reductions don't seem to fit. is this a good example for a monad? (from the theoretical pov I don’t see how you determine the way of combining several copies into one, but maybe this does not change the practical application - it seems that such problems of the monad are solved in another place where some state needs to be carried around).
(ps mentions random numbers, but I can't replace this with a solution that uses a volatile state behind the scenes - like "normal" random procedures - for reasons not related to the question).
andrew cooke
source share