laurazard
Hi all,
This is my first post in this forum, and although I did look, please forgive me if it’s a duplicate (or something very simple, I’m quite new at elixir).
Onto the question: I have two streams which I create with File.Stream!. I want to create the Cartesian product of the two streams, but only until I have 10000 items (the full Cartesian product will be over double that). What I would like to really do is do this lazily, but I can’t seem to come up with an easy way to do that.
Currently, what I’m doing is this
for a <- File.stream!("a.txt"),
b <- File.stream!("b.txt"),
do: (
a = String.trim(a)
b = String.trim(b)
{a, b}
)
However, this seems to work eagerly. Of note is that I also need to apply some transformations to the elements (as exemplified by the String.trim).
Any idea as to how I could accomplish this in a lazy way, maybe with better use of Streams, or zipping them, or some such?
Thank you so much!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 2 of 2 Posts
josevalim
Welcome @laurazard!
A regular for-comprehension is equivalent to a chain of
flat_mapwith amapat the end. So this comprehension:Is equivalent to:
Which means you can make it a stream with:
You can use a similar approach in your code.
laurazard
Hi @josevalim,
Thank you for the comprehensive answer! This is exactly what I need