Elixir way to conditionally update a map

I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a loss how to do this without an if statement. I also don’t know if rebinding the same variable is a ‘code smell’

data = %{
  requireInteraction: true,
  title: title,
  icon: icon,
  click_action: click_action
}

if body do
  data = Map.put(data, :body, body)
end

It also warns me of this. So confusing :s

Note variables defined inside case, cond, fn, if and similar do not leak. If you want to conditionally override an existing variable “data”, you will have to explicitly return the variable. For example:

So you want to update the body key value pair to the data map when the body key exists?

I just want to add the :body key/value pair into the json blob if the body variable isnt nil. Im’ sending a message, and body is optional, but I can’t send NULL in the json or it literally puts a blank space.

You can use Map.has_key?(data, :body) with a case statement. A more Elixir way might be to pattern match on the presence or absence of the map.

def process(%{body: _} = data), do: data
def process(%{} = data), do: %{data | body: nil}
3 Likes

Is there a way that doesnt require creating methods to do it? I suppose I could do that, i feel like my module will get cluttered very fast if i have so many methods to do something like that

How would the case statement look?

1 Like

This has got to be the most frustrating day coding since i learned ruby like 10 years ago JESUS CHRIST.

I have spent 3 hours just trying to get a stupid map to update

Here is what i had so far

data =
  Enum.reject(
    %{
      requireInteraction: true,
      title: title,
      body: body,
      icon: icon,
      click_action: click_action
    },
    &(!elem(&1, 1))
  )

damnit it converts it to a list… this gives me so much of a headache. i have never been stuck on such easy stuff in so long, this is by far the hardest coding day in LONG TIME

Imo this is a elixir way as well to do it:

data = %{
  requireInteraction: true,
  title: title,
  icon: icon,
  click_action: click_action
}

data =
    if body do
      Map.put(data, :body, body)
    else
      data
    end

personally I’d prefer to create more functions like MrDoops pointed out, you can do anonymous functions as well

4 Likes

Ok… I guess skip all my preconceptions and just use the method way. The only problem is it doesnt remove the key it only sets it to NULL (I need to remove the key).

I don’t like to have two variables with same name in scope. For me such code is less readable.

If you have only one variable like that then use if or && with || instead like:

data = %{…}
updated_data = if body, do: Map.put(data, :body, body), else: data
# or
updated_data = body && Map.put(data, :body, body) || data

In case you need to do it multiple times then I recommend to do something like:

data = %{…}
data2 = %{example: 5, body: body, sample: 10}
Enum.reduce(data2, data, fn {key, value}, acc -> value && Map.put(acc, key, value) || acc)

This is equivalent for maps:

:maps.filter(fn _key, value -> not is_nil(value) end, data)
5 Likes

Statement mindset:

Expression mindset:

Statements don’t work in an expression based language - that’s why.

4 Likes

I’m more referring to all the errors for everything I try to do, and weird unexpected things (which is normal of course). Not complaining, just saying… brutal

Thats awesome, I like that 1 line if, and good point about the variable name.

I take it :maps is straight erlang?

Yes this comes from Erlang. Enum module always returns List. Of course you can do Enum.into(updated_data, %{}), but this makes no sense if you can directly work on Map instead.

1 Like

This is a dumb question, but why don’t they have a filter function which returns a map? I feel like that would be a common task?

Enum is protocol which Map (of course not only) implements, but this means it would return List in (unified) result unless you would not turn it into Map again.

So no matter if you pass List or Map it would always return List. Also when you are returning Tuple (i.e. {key, value}) and collect results then you got List of Tuple (like [{key, value}]) i.e. Keyword (which needs atom as key for example: [a: 5] is [{:a, 5}] nicely printed) which is just List. It’s why typical Map manipulation returns List.

Not sure how Erlang solves it - not even sure which one is faster or slower, but Erlang way is definitely easier to read for me since we have less code.

The only different thing is Enum.reduce/3 which acc (2nd argument is initial value) could be any term and we work directly on it, so instead return Tuple (again {key, value}) we are returning updated version of acc which does not chances its type unless we decide to do so.

1 Like

Pretty basic implementation:

filter(Pred,Map) when is_function(Pred,2), is_map(Map) ->
    maps:from_list([{K,V}||{K,V}<-maps:to_list(Map),Pred(K,V)]);
filter(Pred,Map) ->
    erlang:error(error_type(Map),[Pred,Map]).
1 Like

@cmkarlsson Ah … then it’s basically as same as Enum.filter/2 and Enum.into/2, but just in one function.

There really is no iteration functionality in Map - probably because that is represented in Enum.

iex(1)> data = %{
...(1)>   requireInteraction: true,
...(1)>   title: "title",
...(1)>   body: "body",
...(1)>   icon: nil,
...(1)>   click_action: :click_action
...(1)> }
%{
  body: "body",
  click_action: :click_action,
  icon: nil,
  requireInteraction: true,
  title: "title"
}
iex(2)> new_data = data |> Enum.reject(fn {_key, value} -> is_nil(value) end) |> Map.new()
%{
  body: "body",
  click_action: :click_action,
  requireInteraction: true,
  title: "title"
}
iex(3)> other_data = for kv <- data, (fn {_k,v} -> not is_nil(v) end).(kv), into: %{}, do: kv
%{
  body: "body",
  click_action: :click_action,
  requireInteraction: true,
  title: "title"
}
iex(4)>

Whichever way you look at it, it is filtered as a list.

1 Like