1

我正在学习 Clojure 与 Quil 一起使用它来制作生成艺术,我想尝试更多地了解 let 函数和 reader 宏。

我尝试从文档中阅读阅读器宏,但找不到明确的答案。

我目前有这段代码:

(let [dabs (take 10 (repeatedly #(make-watercolor 
                                   (mkpt 
                                     (randomNormal 0.1 0.9) 
                                     (randomNormal 0.1 0.9))
                                   (w (randomNormal 0.4 0.7)))))]
    (draw-multiple-watercolor dabs 3))

这很丑陋,而且不可读。我想精简重复的函数,将其拆分为更小的部分,但由于这些部分将被多次评估并且内部具有随机性,我无法将它们的结果存储在变量中并使用它,而是需要在需要时评估它们。

我的问题是:有没有办法做这样的事情

(let [randCoord (randomNormal 0.1 0.9) ;This..
      randPoint (mkpt randCoord randCoord) ;..and this doesn't should be evaluated here, but on the repeatedly function calls
      dabs (take 10 (repeatedly #(make-watercolor 
                                   randPoint ;Evaluation here
                                   (w (randomNormal 0.4 0.7)))))]
    (draw-multiple-watercolor dabs 3))
4

1 回答 1

3

一种选择是使用与传递给的函数相同的方法repeatedly:只需将表达式包装在不带参数(或只带您想要更改的参数)的函数中,然后每次调用时都会对它们进行评估功能。

(let [rand-coord (fn [] (randomNormal 0.1 0.9)) ;; or #(randomNormal 0.1 0.9)
      make-dab #(make-watercolor (mkpt (rand-coord) (rand-coord))
                                 (w (randomNormal 0.4 0.7)))
      dabs (take 10 (repeatedly make-dab))]
  (draw-multiple-watercolor dabs 3))

还要看看在letfn非顶级/命名空间范围内定义函数。

您可能还会发现, , , ,等线程宏可以使一些代码更具可读性。例如,如果您更改为最后使用它的 dab 序列(这对于在 Clojure 中对序列进行操作的函数来说是相当常见的做法),那么您可以执行以下操作:->->>as->some->cond->draw-multiple-watercolor

(let [rand-coord (fn [] (randomNormal 0.1 0.9)) ;; or #(randomNormal 0.1 0.9)
      make-dab #(make-watercolor (mkpt (rand-coord) (rand-coord))
                                 (w (randomNormal 0.4 0.7)))]
  (->> make-dab
       (repeatedly 10) ;; note: repeatedly can take a # of iterations
       (draw-multiple-watercolor 3)))
于 2019-01-03T20:47:43.473 回答