ddombrow

ddombrow

Missing something with Stream to Enum

Missing something when going through the Little Elixir book. Made my own function to grab a csv and make weather requests.

The result is the result of the first operation (a correct temperature response from a service, followed by the cities list. I expected a list of outputs only. Any pointers?

def get_cities_weather(csv_path) do
  alias NimbleCSV.RFC4180, as: CSV

  cities = csv_path
  |> File.stream!
  |> CSV.parse_stream
  |> Stream.map(fn [city, city_ascii, lat, lng, pop, country, iso2, iso3, province] ->
      %{name: city_ascii, lat: lat, lng: lng}
    end)

  cities
  |> Enum.map_every(500, fn city ->
    Metex.Worker.temperature_of(city)
  end)

end

Produces:

[{:ok, 40.8}, %{lat: "34.5167011", lng: "65.25000063", name: "Chaghcharan"},
 %{lat: "31.58299802", lng: "64.35999955", name: "Lashkar Gah"},
 %{lat: "31.11200108", lng: "61.88699752", name: "Zaranj"},
 %{lat: "32.63329815", lng: "65.86669865", name: "Tarin Kowt"},
 %{lat: "32.85000016", lng: "68.41670453", name: "Zareh Sharan"}...]

Marked As Solved

NobbZ

NobbZ

If you want that behaviour, you probably want Enum.take_every/2 or Stream.take_every/2.

It could look like this then:

def get_cities_weather(csv_path) do
  alias NimbleCSV.RFC4180, as: CSV

  csv_path
  |> File.stream!
  |> CSV.parse_stream
  |> Stream.map(fn [city, city_ascii, lat, lng, pop, country, iso2, iso3, province] ->
      %{name: city_ascii, lat: lat, lng: lng}
    end)
  |> Stream.take_every(500)
  |> Enum.map(fn city ->
    Metex.Worker.temperature_of(city)
  end)
end

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

or use it as an Enumerable

Minor nitpick: Enumerables are neither lazy nor eager. Stream deals with Enumerables lazily, and Enum deals with them eagerly.

So a stream does nothing until it is consumed. It can be consumed by Stream.run or any of the Enum functions that eagerly consume their inputs.

NobbZ

NobbZ

But an important one! I thought if enumerable is the right word here quite a moment. Names are hard especially if you have to deal an remember the relations between similar names.

NobbZ

NobbZ

map_every(enumerable, nth, fun)

map_every(t, non_neg_integer, (element -> any)) :: list

Returns a list of results of invoking fun on every nth item of enumerable, starting with the first element.

So you have to wait another 495 elements after the ellipsis, before you will get a processed item again.

Perhaps you want plain old Enum.map/2 or Stream.map/2 instead?

Where Next?

Popular in Questions Top

New
Tee
can someone please explain to me how Enum.reduce works with maps
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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 53690 245
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
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
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
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement