是否可以将 erlang 的 :math 函数重新创建为 elixir 宏?
Is it possible to recreate erlang's :math functions as elixir macros?
我正在创建一个宏来计算两组经纬度值之间的距离。
iex()> calc_distance(posA, posB)
2 # distance is in km
目前这与常规函数的工作方式类似。我希望它成为宏的原因是我可以在保护子句中使用它。
例如
fn(posA, posB) when calc_distance(posA, posB) < 10 -> "close enough" end
但是,对于要在保护子句中使用的宏,它必须是“follow the rules”。这意味着很多函数和运算符是不允许使用的。
我的初始宏看起来是这样的...
defmacro calc_distance(ll1, ll2) do
quote do
lat1 = elem(unquote(ll1), 0)
long1 = elem(unquote(ll1), 1)
lat2 = elem(unquote(ll2), 0)
long2 = elem(unquote(ll2), 1)
v = :math.pi / 180
r = 6372.8
dlat = :math.sin((lat2 - lat1) * v / 2)
dlong = :math.sin((long2 - long1) * v / 2)
a = dlat * dlat + dlong * dlong * :math.cos(lat1 * v) * :math.cos(lat2 * v)
res = r * 2 * :math.asin(:math.sqrt(a))
res
end
end
我已经开始 "guard clause friendly" 通过删除宏中定义的所有变量。
defmacro calc_distance(ll1, ll2) do
quote do
:math.sin((elem(unquote(ll2), 1) - elem(unquote(ll1), 1)) * (3.141592653589793 / 180) / 2)
|> square()
|> Kernel.*(:math.cos(elem(unquote(ll1), 0) * (3.141592653589793 / 180)))
|> Kernel.*(:math.cos(elem(unquote(ll2), 0) * (3.141592653589793 / 180)))
|> Kernel.+(square(:math.sin((elem(unquote(ll2), 0) - elem(unquote(ll1), 0)) * (3.141592653589793 / 180) / 2)))
|> :math.sqrt()
|> :math.asin()
|> Kernel.*(2)
|> Kernel.*(6372.8)
end
end
这仍然可以作为宏使用,但由于使用了 :math
函数,当我尝试将其用作保护子句时仍然出错。
如果我可以将此函数的我自己的版本编写为宏,这将解决问题。
有人知道这是否可行吗?如果是这样,我该如何处理?
不,不可能将此作为保护测试来实施。
或者好吧,如果你允许准确性损失,这是可能的:this approximation of the sine function 可以仅使用守卫中允许的操作来实现。
但最有可能的是,在您的程序中,准确性比节省几行代码更重要。在这种情况下,我可能会调用我的函数 call_distance
并将结果作为参数传递给另一个函数,该函数可以对结果使用保护测试:
def my_function(ll1, ll2) do
my_function(ll1, ll2, calc_distance(ll1, ll2))
end
defp my_function(ll1, ll2, distance) when distance < 10 do
"close enough"
end
defp my_function(ll1, ll2, distance) do
"too far"
end
我正在创建一个宏来计算两组经纬度值之间的距离。
iex()> calc_distance(posA, posB)
2 # distance is in km
目前这与常规函数的工作方式类似。我希望它成为宏的原因是我可以在保护子句中使用它。 例如
fn(posA, posB) when calc_distance(posA, posB) < 10 -> "close enough" end
但是,对于要在保护子句中使用的宏,它必须是“follow the rules”。这意味着很多函数和运算符是不允许使用的。
我的初始宏看起来是这样的...
defmacro calc_distance(ll1, ll2) do
quote do
lat1 = elem(unquote(ll1), 0)
long1 = elem(unquote(ll1), 1)
lat2 = elem(unquote(ll2), 0)
long2 = elem(unquote(ll2), 1)
v = :math.pi / 180
r = 6372.8
dlat = :math.sin((lat2 - lat1) * v / 2)
dlong = :math.sin((long2 - long1) * v / 2)
a = dlat * dlat + dlong * dlong * :math.cos(lat1 * v) * :math.cos(lat2 * v)
res = r * 2 * :math.asin(:math.sqrt(a))
res
end
end
我已经开始 "guard clause friendly" 通过删除宏中定义的所有变量。
defmacro calc_distance(ll1, ll2) do
quote do
:math.sin((elem(unquote(ll2), 1) - elem(unquote(ll1), 1)) * (3.141592653589793 / 180) / 2)
|> square()
|> Kernel.*(:math.cos(elem(unquote(ll1), 0) * (3.141592653589793 / 180)))
|> Kernel.*(:math.cos(elem(unquote(ll2), 0) * (3.141592653589793 / 180)))
|> Kernel.+(square(:math.sin((elem(unquote(ll2), 0) - elem(unquote(ll1), 0)) * (3.141592653589793 / 180) / 2)))
|> :math.sqrt()
|> :math.asin()
|> Kernel.*(2)
|> Kernel.*(6372.8)
end
end
这仍然可以作为宏使用,但由于使用了 :math
函数,当我尝试将其用作保护子句时仍然出错。
如果我可以将此函数的我自己的版本编写为宏,这将解决问题。
有人知道这是否可行吗?如果是这样,我该如何处理?
不,不可能将此作为保护测试来实施。
或者好吧,如果你允许准确性损失,这是可能的:this approximation of the sine function 可以仅使用守卫中允许的操作来实现。
但最有可能的是,在您的程序中,准确性比节省几行代码更重要。在这种情况下,我可能会调用我的函数 call_distance
并将结果作为参数传递给另一个函数,该函数可以对结果使用保护测试:
def my_function(ll1, ll2) do
my_function(ll1, ll2, calc_distance(ll1, ll2))
end
defp my_function(ll1, ll2, distance) when distance < 10 do
"close enough"
end
defp my_function(ll1, ll2, distance) do
"too far"
end