如何评估AST具有更好的性能?目前我们创建AST作为树,其中叶节点(终端)是一个参数的函数 - 关键字及其值的映射.终端用关键字表示,而功能(非终端)可以是用户(或clojure)定义的功能.完全增长方法从非终端和终端创建树:
(defn full-growth
"Creates individual by full growth method: root and intermediate nodes are
randomly selected from non-terminals Ns,
leaves at depth depth are randomly selected from terminals Ts"
[Ns Ts arity-fn depth]
(if (<= depth 0)
(rand-nth Ts)
(let [n (rand-nth Ns)]
(cons n (repeatedly (arity-fn n) #(full-growth Ns Ts arity-fn(dec depth)))))))
Run Code Online (Sandbox Code Playgroud)
生成AST的示例:
=> (def ast (full-growth [+ *] [:x] {+ 2, * 2} 3))
#'gpr.symb-reg/ast
=> ast
(#object[clojure.core$_STAR_ 0x6fc90beb "clojure.core$_STAR_@6fc90beb"]
(#object[clojure.core$_STAR_ 0x6fc90beb "clojure.core$_STAR_@6fc90beb"]
(#object[clojure.core$_STAR_ 0x6fc90beb "clojure.core$_STAR_@6fc90beb"] …Run Code Online (Sandbox Code Playgroud)