Hello, I am quite new to Elixir (I already start loving the language).
I am trying to write a function that swaps two elements in nested list.
For instance, I want to swap the third element at the first row with the fifth element at the third row I would write something like swap.(list, {0, 2}, {2, 4}).
My current version is
swap = fn
list, {i1, j1}, {i2, j2} ->
temp = list |> Enum.at(i1) |> Enum.at(j1)
row1 = list |> Enum.at(i1)
row2 = list |> Enum.at(i2)
new_row1 = row1 |> List.replace_at(j1, Enum.at(row2, j2))
new_row2 = row2 |> List.replace_at(j2, temp)
list |> List.replace_at(i1, new_row1) |> List.replace_at(i2, new_row2)
end
list = [
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]
]
swap.(list, {0, 2}, {2, 4})
I am wondering if there is better way of doing this.