aalberti333

aalberti333

Enum.map over list of key/value pairs with a map as the value

As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:

[
        {"2015-02-23",
        %{
          "1. open" => "94.6226",
          "2. high" => "95.0000",
          "3. low" => "94.1250",
          "4. close" => "94.3130",
          "5. volume" => "19943800"
        }},
        ...
]

and I’ve tried the following with no success:

def datestr_to_datetime(ticker_map) do
    ticker_map
    |> Enum.map(fn {k, {nk, nv}} -> {Date.from_iso8601!(k), {nk, Float.parse(nv)}} end)
    |> Enum.sort_by(fn {d, v} -> {{d.year, d.month, d.day}, v} end)
end

this fails at Enum.map, and I’m not sure why. Any help is greatly appreciated!

Marked As Solved

al2o3cr

al2o3cr

A good general practice when writing data transformations is “align the shape of code with the shape of the data it processes”. In this case, your requirement starts with “a list of key-value pairs”, so write the corresponding code:

def convert_from_strings(data) do
  Enum.map(data, &convert_one_element/1)
end

def convert_one_element({key_string, stats_map}) do
  # TODO: return {new_key, new_stats_map}
  {key_string, stats_map}
end

Your next requirement: the incoming key should be converted from a string to a date with Date.from_iso8601!/1. convert_from_strings will stay the same for a while, since we’ve focused attention down to one element.

def convert_one_element({key_string, stats_map}) do
  {
    Date.from_iso8601!(key_string),
    stats_map
  }
end

Your next requirement: each value in stats_map should be converted with Float.parse/1. We can write that function first:

def convert_stats_map(stats_map) do
  stats_map
  |> Enum.map(fn {k, v} -> {k, convert_float(v)} end)
  |> Map.new()
end

def convert_float(string_value) do
  string_value
  |> Float.parse()
  |> elem(0)
end

and then hook it up:

def convert_one_element({key_string, stats_map}) do
  {
    Date.from_iso8601!(key_string),
    convert_stats_map(stats_map)
  }
end

Last requirement: the list should be sorted by year/month/day. This changes convert_from_strings, giving the final code:

def convert_from_strings(data) do
  data
  |> Enum.map(&convert_one_element/1)
  |> Enum.sort_by(fn {d, v} -> {{d.year, d.month, d.day}, v} end)
end

def convert_one_element({key_string, stats_map}) do
  {
    Date.from_iso8601!(key_string),
    convert_stats_map(stats_map)
  }
end

def convert_stats_map(stats_map) do
  stats_map
  |> Enum.map(fn {k, v} -> {k, convert_float(v)} end)
  |> Map.new()
end

def convert_float(string_value) do
  string_value
  |> Float.parse()
  |> elem(0)
end

Some notes:

  • to completely match the structure, there should be a convert_key_string function called from convert_one_element. All it would do is call Date.from_iso8601!, so I wrote it inline.

  • consider making most of these convert_* functions private

  • the Access protocol and the associated functions in Kernel can DRY up some of this quite a bit:

def convert_from_strings_with_access(data) do
  import Access

  data
  |> update_in([all(), elem(0)], &Date.from_iso8601!/1)
  |> update_in([all(), elem(1)], &convert_stats_map/1)
  |> Enum.sort_by(fn {d, v} -> {{d.year, d.month, d.day}, v} end)
end

Sadly there’s no equivalent of Access.all() for “every value in a Map”, or this wouldn’t need convert_stats_map even.

Also Liked

hauleth

hauleth

Just to let you know:

enumerable
|> Enum.map(&fun/1)
|> Map.new()

Is less idiomatic than:

enumarable
|> Map.new(&fun/1)
hauleth

hauleth

How do you think it would work? It try to match:

        {"2015-02-23",
        %{
          "1. open" => "94.6226",
          "2. high" => "95.0000",
          "3. low" => "94.1250",
          "4. close" => "94.3130",
          "5. volume" => "19943800"
        }}

to

{k, {nk, nv}}

But 2nd value in tuple is map() but you try to match it to 2-ary tuple.

Where Next?

Popular in Questions 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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

9mm
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 l...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
Tee
can someone please explain to me how Enum.reduce works with maps
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
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement