Ettu_R

Ettu_R

Accessing/updating Struct error during Enum.map

I have a struct define as follows

defmodule Employee do
 defstruct [:fname, :lname, :id, salary: 0, job: "none"]

then I made a list, consisting of Employee structs and added one in there.
At some point I want to search the list for employee based on id number, this part also I’ve gotten to work.
But then I got a function promote, where I pick an employee based on id and then change job: field. This is where im running into problem. I want function return updated list of employees so i do Enum.map in the end and try to update job in there. Somethings wrong and Im not sure how to accomplish this?
in the following code

def promote(list) do
        empId = IO.gets("Enter employee ID to promote: ")
        empIdC = String.trim(empId)
        getEmp = Enum.filter(list, fn x -> x.id == String.to_integer(empIdC) end) |> List.first()
        IO.inspect(getEmp)
        if getEmp != nil do
            cond do
                getEmp.job == "none" -> Enum.map(list, fn x -> if x.id == String.to_integer(empIdC) do x.job = "coder" end end)
            end
        end
    end

When I inspect getEmp I see it has correct Struct inside

%Employee{fname: "m", id: 1, job: "none", lname: "a", salary: 0}

job is “none”, I want to promote it to “coder” and return new list

Error im getting with this code

** (CompileError) kt6.exs:71: cannot invoke remote function x.job/0 inside a match
    (stdlib 3.8) lists.erl:1354: :lists.mapfoldl/3
    (stdlib 3.8) lists.erl:1355: :lists.mapfoldl/3
    (elixir 1.10.4) expanding macro: Kernel.if/2

Marked As Solved

LostKobrakai

LostKobrakai

This is not valid elixir. If you want to update the map use %{x | job: "coder"}. Also you should be aware that if without an else will return nil, so your function will return nil if no employee was found.

But more generally this is not really ideomatic in the big picture as well. If you select employees by id then it makes more sense to store them in a map with the employee id as a key, which makes selecting employees quite a bit simpler.

def promote(map) do
  emp_id = 
    "Enter employee ID to promote: "
    |> IO.gets() 
    |> String.trim()
    |> String.to_integer()

  if Map.has_key?(map, emp_id) do
    Map.update!(map, emp_id, fn current -> %{current | job: "coder"} end)
  else
    map
  end
end

Where Next?

Popular in Questions Top

jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

We're in Beta

About us Mission Statement