CastLouis

CastLouis

Diference betwen Enum.map and Stream.map

Can someone explain me the diference betwen Enum.map and Stream.map, because i have this piece of code:
File.stream!(path)
|> Stream.map(&parse_line_to_string/1)
|> Stream.filter(fn {status,_} -> status == “SP” or status == “RA” end)
|> Enum.map(&build_claim/1)

and it build the claims but if i replace Enum to Stream it doesn’t work.

i dont know how it’s work

Most Liked

ericmj

ericmj

Elixir Core Team

Enum.map immediately maps over a collection. Stream.map on the other hand returns a value that describes how to map over a stream. This way we can lazy build a stream from operations such as map and filter and later decide to run it.

If you replace your Enum.map with Stream.map the stream will never be executed, instead the value describing how to map will just be returned.

bbense

bbense

One way to think about it is that each Stream call is essentially writing a mini program on that step of the pipeline; those programs don’t get “compiled and run” until there is an Enum in the pipeline to start the execution.

If that way of thinking doesn’t help, then just ignore it.

CastLouis

CastLouis

File.stream!(path)
|> Stream.map(&parse_line_to_string/1)
|> Stream.filter(fn {status,_} -> status == “SP” or status == “RA” end)
|> Stream.map(&build_claim/1)
|> Enum.each(&store_claim/1)

Thanks, for helping me, i was doing something after the stream, and i get the conclusion that you can use Stream intermedialy but at the finally you need to que all the collection for finish it

Please correct me if i am wrong.

law

law

This is my understanding. A stream ‘waits’ until items are called by streaming it into an enumerable or calling Stream.run

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
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
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
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
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

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
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
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
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement