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

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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement