我是elixir的新手,很难更新变量。需要一些帮助。我有两个地图如何在elixir中更新函数内部的布尔变量
firstMsg = %{msg: "Hello", vt: %{"p1" => 1, "p2" => 1, "p3" => 1}, from: "p3"}
state = %{ :name => "p2",
vector: %{"p1" => 0, "p2" => 0, "p3" => 0},
participants: ["p1","p3","p2"]
}
我传递这两张地图的功能,它应该返回我真或假,这取决于一些条件。
defmodule Testfunc do
def keep_in_pending(firstMsg, state) do
if (firstMsg.vt[firstMsg.from] == state.vector[firstMsg.from] + 1) do
#IO.puts("Origin proc clock is 1 step ahead from rcvd process Origin clk")
checking = false #initially set this to false
for n <- state.participants do
if n != firstMsg.from do #filter the origin processes
IO.puts("#{n}: #{inspect firstMsg.vt[n]} <= #{n}: #{inspect state.vector[n]} ")
checking = cond do
(firstMsg.vt[n] <= state.vector[n]) -> false
(firstMsg.vt[n] > state.vector[n]) -> true
end
end
end
end
checking
end
end
out = Testfunc.keep_in_pending(firstMsg, state)
IO.puts("#{inspect out}")
它总是给我虚假的(我最初分配给它的值),并没有更新。我认为变量的范围仅限于内部“如果”。任何人都可以给我建议如何重新安排这段代码,以便它返回适当的更新布尔值?
所以在这种情况下,它应该返回true,因为firstMsg.vt [“p1”]> state.vector [“p1”]。
但它总是让我在任何情况下都是虚假的。 –
奇怪的是,我得到'true',使用你提供的所有信息。 –