My understanding is that the Access module is meant to contain functions that may be used to generate a func that can be used for filtering with nested dictionary structure interrogation, however, I ran into some issues when I was trying to pass my own anonymous function in place of an Access.all/at.
This only occurs with the (dynamic) get_and_update_in rather than get_in. Below is the code (modified from Programming Elixir):
authors = [
%{ name: "José", language: "Elixir" },
%{ name: "Matz", language: "Ruby" },
%{ name: "Larry", language: "Perl" }
]
languages_with_an_r = fn (:get, collection, next_fn) ->
for row <- collection do
if String.contains?(row.language, "r") do
next_fn.(row)
end
end
end
get_and_update_in(authors, [Access.all(), :name], &({&1, &1})
get_and_update_in(authors, [languages_with_an_r, :name], &({&1, &1}))
The first get_and_update_in works, while the second fails citing:
**(FunctionClauseError) no function clause matching in :erl_eval."-inside-an-interpreted-fun-"/3
The following arguments were given to :erl_eval."-inside-an-interpreted-fun-"/3:
# 1
:get_and_update
# 2
[
%{name: "José", language: "Elixir"},
%{name: "Matz", language: "Rubyr"},
%{name: "Larry", language: "Perl"}
]
# 3
#Function<6.66015480/1 in Kernel.get_and_update_in/3>
(stdlib 5.2) :erl_eval."-inside-an-interpreted-fun-"/3
(stdlib 5.2) erl_eval.erl:963: :erl_eval.eval_fun/8
iex:6: (file)
iex:14: (file)
Seems to suggest that the functions output by the Access module functions are of a specific pattern that is acceptable to the get_and_update_in function while other anonymous functions aren’t. At least that’s what I’m understanding.
Can I write my own functions to work with get_and_update_in ?
My second question regards Access.key/1. What is the usecase for this function, is it to make sure a map/struct is being interrogated? I ask because get_in and get_and_update_in allow for direct expression of values as keys in the path list, so what does Access.key/1 do differently here?