List – Can you choose to insert the pipe output to insert the Elixir function args?

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, 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)

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

< 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.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2299 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.