Hi! Very new to elixir, thought I might try my hand at asking here, as I’m in a bit of a bind.
Suppose I have a list of maps as follows:
ball_prop_list =
[
%{"id" => "cue", "is_idle" => true, "velocity_x" => 0.0, "velocity_z" => 0.0, "x" => -15.0, "z" => 0.0},
%{"id" => "ball_1", "is_idle" => true, "velocity_x" => 0.0, "velocity_z" => 0.0, "x" => 15.0, "z" => 0.0},
%{"id" => "ball_2", "is_idle" => true, "velocity_x" => 0.0, "velocity_z" => 0.0, "x" => 17.0, "z" => 1.1},
%{"id" => "ball_3", "is_idle" => true, "velocity_x" => 0.0, "velocity_z" => 0.0, "x" => 17.0, "z" => -1.1}
]
How do I go about iterating through each item, and then comparing that item to the entire list (save for itself, of course)? This is based on C# code for checking ball collision, and I’m trying to roughly translate it to elixir.
foreach (bodyA in objectList) {
foreach (bodyB in objectList) {
if (bodyA == bodyB) {
continue;
}
// Do other stuff here
}
}
I tried this:
Enum.map(ball_prop_list , fn
body_a ->
Enum.map(ball_prop_list , fn body_b ->
if body_a["id"] != body_b["id"] do
Sys.Log.debug("#{body_a["id"]} (A) vs #{body_a["id"]} (B)")
# Do other stuff here
end
end)
end)
And enum.each
in place of enum.map
but it doesn’t seem to work as intended, as I end up with this:
cue (A) vs cue (B)
cue (A) vs cue (B)
cue (A) vs cue (B)
ball_1 (A) vs ball_1 (B)
ball_1 (A) vs ball_1 (B)
ball_1 (A) vs ball_1 (B)
ball_2 (A) vs ball_2 (B)
ball_2 (A) vs ball_2 (B)
ball_2 (A) vs ball_2 (B)
ball_3 (A) vs ball_3 (B)
ball_3 (A) vs ball_3 (B)
ball_3 (A) vs ball_3 (B)
So not only did it not check if it’s comparing the same item, it doesn’t seem to have checked against anything else; I was expecting something like:
cue (A) vs ball_1 (B)
cue (A) vs ball_2 (B)
cue (A) vs ball_3 (B)
ball_1 (A) vs cue (A)
ball_1 (A) vs ball_2 (B)
ball_1 (A) vs ball_3 (B)
etc
Is there anything else I can try?