building39

building39

I’m trying to update a deeply nested map (actually, a struct) and am not having any luck. Here’s the scenario:

key2 = :"some.key"
Kernel.update_in(state.key1."some.key".key3, &Map.delete(&1, "keyval"))    # seems to work fine
Kernel.update_in(state.key1.key2.key3, &Map.delete(&1, "keyval"))   # fails, since key2 is a data item, not an atom.
Kernel.update_in(state.key1[key2].key3, &Map.delete(&1, "keyval"))  # fails atrociously - beam uses up all available ram, then crashes.

I’m sure that I’m doing this entirely wrong - I can’t believe that this would be a bug - same behavior on 1.4.5, 1.5.1 and 1.6.0-dev.

What am I doing wrong?

Showing Posts 1 to 6

NobbZ

NobbZ

Small experiment on the REPL, just use [] consistently:

iex(1)> state = %{foo: %{bar: %{baz: 1}}}
%{foo: %{bar: %{baz: 1}}}
iex(2)> mid = :bar
:bar
iex(3)> update_in(state[:foo][mid][:baz], &(&1+1))
%{foo: %{bar: %{baz: 2}}}
iex(4)> update_in(state, [:foo, mid, :baz], &(&1+1))
%{foo: %{bar: %{baz: 2}}}
bglusman

bglusman

Maybe I’m misunderstanding, but I beleive/thought update_in relies on Access protocol, and Struct’s don’t implement that, only maps do… I just saw @NobbZ’s post, and I did my own simpler REPL experiment to confrm, and it seems to partly support that, but maybe he understood your issue better? But since I already finished before I saw, I’ll share:

iex> defmodule Temp do
(2)>   defstruct a: 1
(2)> end
iex> x = %{a: %{b: 1} }
(5)> %{a: %{b: 1}}
iex> y = %Temp{a: %{b: 1} }
(6)>  %Temp{a: %{b: 1}}
iex> update_in(x, [:a, :b], &(&1 + 1))
(7)> %{a: %{b: 2}}
iex> update_in(y, [:a, :b], &(&1 + 1))
(8)>** (UndefinedFunctionError) function Temp.get_and_update/3 is undefined (Temp does not implement the Access behaviour)
(8)> Temp.get_and_update(%Temp{a: %{b: 1}}, :a, #Function<3.67985749/1 in Kernel.get_and_update_in/3>)
(8)> (elixir) lib/access.ex:356: Access.get_and_update/3
(8)> (elixir) lib/kernel.ex:1902: Kernel.update_in/3
building39

building39 OP

Yes, that seems to do the trick. Thanks!

peerreynders

peerreynders

iex(1)> defmodule Temp do
...(1)>   defstruct a: 1
...(1)> end
iex(2)> y = %Temp{a: %{b: 1} }
%Temp{a: %{b: 1}}
iex(3)> update_in(y, [Access.key(:a,%{}), Access.key(:b,0)], &(&1 + 1))
%Temp{a: %{b: 2}}
iex(4)> update_in(%Temp{a: %{}}, [Access.key(:a,%{}), Access.key(:b,0)], &(&1 + 1))
%Temp{a: %{b: 1}}
iex(5)>

Access.key/2

bglusman

bglusman

Thanks @peerreynders hadn’t seend Access.key before, though looks used under the hood by access protocol… but I mentioned the map/struct difference because @building39 said

though to be fair I’m not totally clear whether it’s a struct-nested-in-a-struct, a struct-nested-in-a-map, or something else, but I assume each level needs the Access protocol to use the syntax he was trying to use, without your Access.key addition?

peerreynders

peerreynders

As stated Access exists to provide

Key-based access to data structures using the data[key] syntax.

The OP also contained attempts to use the static access operator which will work on structs that do not implement the Access callbacks. But you are correct that the structs would have to implement Access callbacks if the supplied list of keys contained “regular keys”.

However get_and_update_in/3 and get_in/2 (and cousins) will work with non-Access structs just fine if they are provided with a list of custom functions - an approach that seems appropriate when interacting with a struct or map “in a programmatic fashion”. Now in most cases the functions generated by Access.key/2 will be sufficient but it can be instructive to DIY just to see what is going on:

# file: info.ex
#
# Some code trying to demonstrate the mechanics of using "functions instead of keys"
# with Kernel.get_and_update_in/3 and Kernel.get_in/2, attempting to expand on the
# somewhat terse examples found in
# https://hexdocs.pm/elixir/Kernel.html#get_and_update_in/3-examples
# https://hexdocs.pm/elixir/Kernel.html#get_in/2-examples
#
# fn(:get_and_update, data, next) - Clause used for Kernel.get_and_update_in/3
# 1. Obtain "value" of implied key from "data"
# 2. Hand "next" that "value" i.e. {replaced_info, updated_value} = next.(value)
# 3. Update the "data" with the "updated_value" under the implied key to get "updated_data"
# 4. Return {replaced_info, updated_data}
#
# fn(:get, data, next) - Clause used for Kernel.get_in/3
# 1. Obtain "value" of implied key from "data"
# 2. Hand "next" that "value" i.e. info = next.(value)
# 3. Return "info"
#
# Note that Access.key/2 will generate these type of functions automatically - however
# that source accommodates all possible scenarios which can easily obscure
# the fundamental operation. This code focuses on navigating a nested struct.
#
# Essentially both "get_and_update_in" and "get_in" process the keys list recursively
# via the "next" callback handed to each "key function" - which is expected to extract
# the value for the key it is responsible for - invoking "next" with that value
# so that (recursive) navigation on the next level "key function" can continue.
#

defmodule Info do
  defstruct [:name, :age]
  @age_default 0

  def age_access(:get_and_update, info, next) do
    age = Map.get(info, :age, @age_default)
    {old, new} = next.(age) # i.e. age_and_update function
    {old, (Map.put info, :age, new)}
  end
  def age_access(:get, info, next) do
    age = Map.get(info, :age, @age_default)
    next.(age) # i.e. identity function
  end

  def all_list(:get_and_update, info_list, next) when is_list(info_list) do
    old_new_list = (Enum.map info_list, next) # e.g. (get_and_update_in &1, [age_access], age_and_update) on each item
                                              #      resulting in {original_age, updated_info} for each item in the list
    :lists.unzip old_new_list                 # need a list of original ages and a list of updated infos
  end
  def all_list(:get, info_list, next) when is_list(info_list) do
    Enum.map info_list, next                  # e.g. (get_in &1, [age_access]) on each item
  end
end

defmodule Role do
  defstruct [:name, :info]
  @info_default %Info{name: "unknown", age: 0}

  def info_access(:get_and_update, role, next) do
    info = Map.get(role, :info, @info_default)
    {old, new} = next.(info) # e.g. (get_and_update_in &1, [age_access], age_and_update)
    {old, (Map.put role, :info, new)}
  end
  def info_access(:get, role, next) do
    info = Map.get(role, :info, @info_default)
    next.(info) # e.g. (get_in &1, [age_access])
  end
end

defmodule Trial do
  def run do
    john = %Info{name: "john", age: 27}
    meg = %Info{name: "meg", age: 23}
    info_list = [john, meg]
    owner = %Role{name: "owner", info: john}

    # NOTE: these take the value NOT the key as stated in
    # https://hexdocs.pm/elixir/Kernel.html#get_and_update_in/3
    # This code is consistent with the examples shown where the
    # passed parameter IS a value NOT a key
    age_and_update = fn age -> {age, age + 1} end
    owner_and_replacement = fn owner -> {owner, meg} end # i.e. meg replaces owner

    age_access = &Info.age_access/3
    info_access = &Role.info_access/3
    all_list = &Info.all_list/3

    # IO.inspect (Kernel.get_and_update_in john, [:age], age_and_update)
    IO.inspect (Kernel.get_and_update_in john, [age_access], age_and_update)
    IO.inspect (Kernel.get_in john, [age_access])
    IO.inspect (Kernel.get_and_update_in owner, [info_access, age_access], age_and_update)
    IO.inspect (Kernel.get_and_update_in owner, [info_access], owner_and_replacement)
    IO.inspect (Kernel.get_in owner, [info_access, age_access])
    IO.inspect (Kernel.get_in owner, [info_access])
    IO.inspect info_list
    IO.inspect (Kernel.get_and_update_in info_list, [all_list, age_access], age_and_update)
    IO.inspect (Kernel.get_in info_list, [all_list, age_access])
  end
end

Trial.run()

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews