在julia中覆盖`>`的错误

时间:2018-01-13 10:42:13

标签: pipe julia

我试图用以下

覆盖julia中的julia> (>)(a, f::Function) = (|>)(a, f) > (generic function with 1 method) julia> (>)(f::Function, g::Function) = (x...)->g(f(x...)) > (generic function with 2 methods) julia> -2 > abs 2 julia> -2 > (abs > sqrt) 1.4142135623730951 julia> (-2 > abs) > sqrt 1.4142135623730951
>

到目前为止,它的工作正常。然而,我无法弄清楚如何覆盖julia> -2 > abs > sqrt ERROR: TypeError: non-boolean (Int64) used in boolean context julia> @which -2 > abs > sqrt ERROR: expression is not a function call, or is too complex for @which to analyze; break it down to simpler parts if possible Stacktrace: [1] error(::String) at ./error.jl:21 以下情况:

np.array([[0,0,0],[0,0,0]])

它是一个buit-in硬语法还是三元函数,以便我们可以覆盖?

1 个答案:

答案 0 :(得分:3)

https://docs.julialang.org/en/latest/manual/mathematical-operations/#Chaining-comparisons-1开始,问题是-2 > absInt,它作为&&的LHS传递,会引发错误。

一般情况下,>应返回Bool,并且链接比较依赖于此事实。您必须重新定义&&的工作方式。我建议您使用>以外的其他符号作为您的目的,例如:

julia>  ⊗(a, f::Function) = (|>)(a, f)
⊗ (generic function with 1 method)

julia>  ⊗(f::Function, g::Function) = (x...)->g(f(x...))
⊗ (generic function with 2 methods)

julia> -2 ⊗ abs ⊗ sqrt
1.4142135623730951