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

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
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
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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

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
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 45766 226
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement