Retrieving information from a List of maps?

Honestly though if you’re this new to the language… don’t. ChatGPT is as often wrong as it is right with Elixir and if you’re at the phase where you’re not sure how to pass a function to Enum.map, how are you supposed to tell the difference?

6 Likes

I hate how people immediately decided that ChatGPT is credible. :sweat:

It hallucinates, and it does it well for the areas where it had trillion+ trainings / tokens. For everything else you might as well ask the local grocery store seller lady, she’ll give you a better answer. :grin:

But apart from that, I don’t get it how actually onboarding yourself in the language is viewed as this monumental task that should never be done. Boggles my mind. We’re not talking about Rust and OCaml here, which might easily take you months to properly onboard, we’re talking Elixir where one weekend is enough to start writing apps and parallel code and make a Phoenix project even.

4 Likes

I asked
‘How do I transform a list of maps in elixir?’

In Elixir, you can transform a list of maps using functions like Enum.map/2 or Enum.reduce/3. Here’s an example of how you can use Enum.map/2 to transform a list of maps:

Suppose you have a list of maps like this:

list_of_maps = [
  %{name: "Alice", age: 30},
  %{name: "Bob", age: 25},
  %{name: "Charlie", age: 35}
]

And you want to double the age in each map. You can do it like this:

transformed_list = Enum.map(list_of_maps, fn %{name: name, age: age} ->
  %{name: name, age: age * 2}
end)

After this transformation, transformed_list will contain the updated maps with doubled ages.

You can adjust the function inside Enum.map/2 to perform the specific transformation you need on the maps in your list.

I also asked how to do it with reduce. Did do not hallucinate and def knew better than grocery lady lol.
I agree, onboarding yourself to the language by going through getting started and docs is a must but @roganjoshua is solving a problem and is stuck. You tried to help, maybe chat gpt can also help - my point.

@benwilson512 You could tell by running the code in iex. A beginner will not tackle a complex problem, nor will you ask chat gpt to solve a complex problem but learning to break down a problem and asking simple questions, don’t see nothing wrong with that. I feel chat gpt can be a great learning companion if used thoughtfully of course, but I’m actually interested if @roganjoshua tried and what does he think?

OK, so somebody in OpenAI noticed ChatGPT is doing badly with Elixir and gave it better Elixir inputs. Cool.

I didn’t really try to help with OP’s problem, I tried to guide them to arrive at the solution themselves. IMO this forum is super helpful but there’s also a balance to be struck – we want people to get better, not for us to become unpaid support group solving other people’s homework (or urgent work tasks).

I formulated the code for OP’s solution literally during reading their original post and could have just posted it – but they have to learn, otherwise next time they’ll come here again with the next problem and so on to infinity, which obviously doesn’t scale. We had posters in the past that tried this and we quickly stopped responding altogether to them. We do not want for that to happen so we try guiding people instead. We want people to learn. That’s why @benwilson512 very often starts off a thread with: “What have you tried?”. It’s a gentle reminder that the people here are willing to help if you tried and failed and got confused (while ACTUALLY KNOWING ELIXIR!), but not if you haven’t even tried (“not trying” includes “not really knowing the basics of Elixir”).

And btw most beginners HUGELY MISS OUT on the official guide because it also teaches them how to experiment in iex, something that not that many programming languages actually have – and is a hugely empowering tool! Helps you iterate quickly.

No machine can replace humans at that yet. ChatGPT will just give you the answer with some little explanation. Correct me if I am wrong, the approach I took felt better to me: “don’t think about the list of records, let’s do the job for one record first” for example.

I am not saying that ChatGPT won’t evolv… – errr, will not have an army of engineers to manually enrich its DB, excuse me! (:stuck_out_tongue_winking_eye:) – to do what I and others do. I’d be glad if we get machine pedagogues at one point, seriously. But so far I am not seeing any signs of that happening.

2 Likes

You’re almost there @roganjoshua . Keep up :slight_smile:

Mapping, reducing etc. can be hard at the beginning if you are more used to loops and mutable state. And honetly those operations will always be a bit more convoluted and verbose in a functional language. But soon enough you will feel it natural if you practice.

So, with this:

 |> Enum.map( fn item -> {"Customer", item["value"]} end)

You get this:

[
  {"Customer", "EOG.Acme.ManualXML"},
  {"Customer",
   "{\"variables\":{\"CUSTOMER\":\"EOG.Acme.ManualXML\",\"VERSION\":\"Version-8.6.2\"},\"id\":\"5112\",\"ref\":\"main\"}"},
  {"Customer", "Version-8.6.2"}
]

The data looks like this:

[
  %{
    "key" => "CUSTOMER",
    "raw" => false,
    "value" => "EOG.Acme.ManualXML",
    "variable_type" => "env_var"
  },
  %{
    "key" => "TRIGGER_PAYLOAD",
    "raw" => false,
    "value" => "{\"variables\":{\"CUSTOMER\":\"EOG.Acme.ManualXML\",\"VERSION\":\"Version-8.6.2\"},\"id\":\"5112\",\"ref\":\"main\"}",
    "variable_type" => "file"
  },
  %{
    "key" => "VERSION",
    "raw" => false,
    "value" => "Version-8.6.2",
    "variable_type" => "env_var"
  }
]

What single change can you make to your code to get this:

[
  {"CUSTOMER", "EOG.Acme.ManualXML"},
  {"TRIGGER_PAYLOAD",
   "{\"variables\":{\"CUSTOMER\":\"EOG.Acme.ManualXML\",\"VERSION\":\"Version-8.6.2\"},\"id\":\"5112\",\"ref\":\"main\"}"},
  {"VERSION", "Version-8.6.2"}
]

?

1 Like

So I think after a bit more reading
I can get from

[
  %{
    "key" => "CUSTOMER",
    "raw" => false,
    "value" => "EOG.Acme.ManualXML",
    "variable_type" => "env_var"
  },
  %{
    "key" => "TRIGGER_PAYLOAD",
    "raw" => false,
    "value" => "{\"variables\":{\"CUSTOMER\":\"EOG.Acme.ManualXML\",\"VERSION\":\"Version-8.6.2\"},\"id\":\"5112\",\"ref\":\"main\"}",
    "variable_type" => "file"
  },
  %{
    "key" => "VERSION",
    "raw" => false,
    "value" => "Version-8.6.2",
    "variable_type" => "env_var"
  }
]

Because I only want the value for CUSTOME and VERSION

|> Enum.filter(fn %{key: key} -> key == "CUSTOMER" or key == "VERSION" end) 
|> Enum.map(fn item -> item[:value] end)

["EOG.Acme.ManualXML", "Version-8.6.2"]

Which is quite close but I think if “key” was key: and “value” was value:

The main takeaway so far is

  • Far more interest than I thought I would get
  • iex is great but iex -S mix phx.server is fantastic
  • dbg/2 (why this is dbg and not debug?)
  • RTFM
3 Likes