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

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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
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
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
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 52774 488
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New

We're in Beta

About us Mission Statement