如何评估条件案例和 return 函数对象?

how to eval a cond case and return function object?

得到 TypeError: 不知道如何包装 : . at 0x000001B879FD3D08>

当运行

;a fn object
(setv a_fn (fn [x] (+ 1 x)))
;a mock predicator
(setv predicator True)
;inject predicator and a_fn into a (cond ..)
(setv cond_expr `(cond [(~predicator) [~a_fn]]))
;eval at another place 
(eval cond_expr)

如何创建 "cond_expr" 以便得到结果 [a_fn]?

为了eval一个HyExpression它必须首先编译成Pythonast。虽然您可以将任意对象放入 HyExpression,但这并不意味着您可以编译它。 (有一点说simulating this feature,但目前没有。)

Hy 编译器只能对称为 Hy 模型类型的一组特定数据类型或可以自动转换为这些 Hy 模型的其他一些类型执行此操作。

在 Python ast 中没有明显的方法来表示函数 object,因此没有适用于它的 Hy 模型。但是你可以编译一个函数definition.

=> (setv a-fn '(fn [x] (+ 1 x)))
=> (setv cond-expr `(cond [True ~a-fn]))
=> (eval cond-expr)
<function <lambda> at 0x0000020635231598>

或函数的符号

=> (defn b-fn [x] (- x 1))
=> (setv cond-expr2 `(cond [True b-fn]))
=> (eval cond-expr)
<function <lambda> at 0x0000020635208378>