Jayshua
How to end for comprehension early
I’m trying to figure out how to use Elixir’s version of for comprehensions.
The Phoenix live view generator creates this code in the live/page_live.ex file to get the search results:
for {app, desc, vsn} <- Application.started_applications(),
app = to_string(app),
String.starts_with?(app, query) and not List.starts_with?(desc, ~c"ERTS"),
into: %{},
do: {app, vsn}
How could I go about making this return at most 5 results? I tried using Enum.with_index on the first line and adding an index < 5 filter, but that just limited the search space to the first 5 dependencies.
Marked As Solved
mudasobwa
Creator of Cure
for comprehension is greedy, so it would pass through the whole list no matter what.
Some time ago I have implemented lazy comprehension with LazyFor having nearly the same syntax as for (but it’s somewhat limited yet.)
iex|1 ▶ import LazyFor
iex|2 ▶ stream {app, desc, vsn} <- Application.started_applications(),
...|2 ▶ app = to_string(app),
...|2 ▶ String.starts_with?(app, "") and not List.starts_with?(desc, ~c"ERTS"),
...|2 ▶ take: 5, # THIS
...|2 ▶ do: {app, vsn}
[
{"lazy_for", '0.3.0'},
{"benchfella", '0.3.5'},
{"hex", '0.20.5'},
{"inets", '7.1.2'},
{"ssl", '9.6'}
]```
Popular in Questions
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
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
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
Hello,
I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these
buyer = %{
id: ...
New
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
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
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
Background
Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
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
Other popular topics
can someone please explain to me how Enum.reduce works with maps
New
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
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
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
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New







