def update_2d(array, inds, val ) do
[first_coord, second_coord] = inds
new_arr = List.update_at(array, second_coord, fn(y) ->
List.update_at(Enum.at(array , second_coord),
first_coord, fn(x) -> val end) end)
end
This function will contain a list of lists, a list of two indexes, and the index to be specified The position of the inserted value in the list of lists.
As the first step to make more Elixir-ey, I started laying the pipeline:
array < br /> |> Enum.at(second_coord)
|> List.update_at(first_coord, fn(x) -> val end)
This makes me mostly there, but how Pass the output to the last anonymous function called by List.update_at? I can embed it in the original call, but this seems to give up:
List.update_at(array, second_coord, fn(y) -> array
|> Enum.at(second_coord)
|> List.update_at(first_coord, fn(x) -> val end) end)
p>
def update_2d(array, inds = [first_coord, second_coord], val) do
updated =
array
|> Enum.at(second_coord)
|> List.update_at(first_coord, fn(x) -> val end)
List.update_at(array, second_coord, fn(x) -> updated end)
end
< p>You can also use the capture operator to do this:
def update_2d(array, inds = [first_coord, second_coord], val), do:
array
|> Enum.at(second_coord)
|> List.update_at(first_coord, fn(x) -> val end)
|> (&(List.update_at(array, second_coord, fn(y) -> &1 end))).()
I found using a more readable variable, but the options are there.
Consider the (smelly, non-idiomatic) function as follows:
def update_2d(array, inds, val) do
[first_coord, second_coord] = inds
new_arr = List.update_at(array, second_coord, fn(y) ->
List .update_at(Enum.at(array, second_coord),
first_coord, fn(x) -> val end) end)
end
This function will contain a list of lists, two The index list and the value to be inserted in the list list at the position specified by the index.
As the first step to make more Elixir-ey, I started laying the pipeline:
< /p>
array
|> Enum.at(second_coord)
|> List.update_at(first_coord, fn(x) -> val end)
This makes me Most of it is there, but how do I pass the output to the last anonymous function called by List.update_at? I can embed it in the original call, but this seems to give up:
List.update_at(array, second_coord, fn(y) -> array
|> Enum.at(second_coord)
|> List.update_at(first_coord, fn(x) -> val end) end)
You can simply Bind to a variable to capture the first result, and then replace it in the second List.update_at/3 call
def update_2d(array, inds = [first_coord, second_coord], val) do
updated =
array
|> Enum.at(second_coord)
|> List.update_at(first_coord, fn(x) -> val end)
List.update_at(array, second_coord, fn(x) -> updated end)
end
You can also use the capture operator to do this :
def update_2d(array, inds = [first_coord, second_coord], val), do:
array
|> Enum.at(second_coord)
|> List.update_at(first_coord, fn(x) -> val end)
|> (&(List.update_at(array, second_coord, fn(y) -> &1 end))). ()
I found using a more readable variable, but the options are there.