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

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
aalberti333
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: ...
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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

We're in Beta

About us Mission Statement