Clojure有没有类似于Mathematica的母猪/收获的功能?

sun*_*ots 1 clojure clojure-contrib

Clojure中是否有函数,它模拟Mathemaica函数播种/收获?母猪和母猪的主要用途是收集在评估过程中产生的表达.

例)

在Mathematica中输入:收获[Sow [w = 2]; w + = Sow [w ^ 3]; w = Sqrt [w + w ^ 3]]

输出:{Sqrt [1010],{{2,8}}}

给出中间结果2和8.

noi*_*ith 10

像clojure这样的homoiconic语言的精彩之处在于,您可以根据需要定义新的语法.

(defmacro reap
  [& body]
  `(let [state# (atom [])
         ~'sow (fn sow [v#] (swap! state# conj v#) v#)]
     [(do ~@body) @state#]))
Run Code Online (Sandbox Code Playgroud)

为简单起见,我使用jvm interop进行数学运算,而不是使用clojure.numeric-tower,因此我们得到浮点而不是精确输出:

user> (reap (let [w (sow 2)
                  w (+ w (sow (Math/pow w 3)))
                  w (Math/sqrt (+ w (Math/pow w 3)))]
              w))

[31.78049716414141 [2 8.0]]
user> (= 31.78049716414141 (Math/sqrt 1010))
true
Run Code Online (Sandbox Code Playgroud)

编辑:既然我看到母猪的文档,它也支持标记和按标记选择

因为这是clojure抓取东西的关键是微不足道的,所以我将只显示一个变量,使标签:

(defmacro reap
  [& body]
  `(let [state# (atom {})
         ~'sew (fn sew [& [v# tag#]]
                 (swap! state# #(update-in % [tag#] conj v#)) v#)]
     [(do ~@body) @state#]))

user> (reap (let [w (sew 2 :a)
            w (+ w (sew (Math/pow w 3)))
            w (Math/sqrt (+ w (Math/pow w 3)))]
        w))

[31.78049716414141 {nil (8.0), :a (2)}]
Run Code Online (Sandbox Code Playgroud)