dogweather

dogweather

A nice way to combine captured functions? (Credo.Check.Refactor.RejectReject)

This code generates a couple of Credo warnings:

    filtered_paragraphs =
      paragraphs
      |> Floki.filter_out("[align=center]")
      |> Enum.reject(&repealed?/1)
      |> Enum.reject(&subchapter_heading?/1)
      |> Enum.reject(&subsubchapter_heading?/1)

One Enum.reject/2 is more efficient than Enum.reject/2 |> Enum.reject/2 (refactor:Credo.Check.Refactor.RejectReject)

Is there some expressive way to combine these three rejects with a logical or?

Is refactoring the best answer, extracting them into a single new named function that I use with reject? Are there any function-level tools for working with captured functions?

Maybe a reject_any? function would be good here.

Marked As Solved

Marcus

Marcus

You could do something like this:

    filtered_paragraphs =
      paragraphs
      |> Floki.filter_out("[align=center]")
      |> Enum.reject(&(repealed?(&1) or subchapter_heading?(&1) or subsubchapter_heading(&1)))

Also Liked

trisolaran

trisolaran

Floki keeps things in memory as a list - when dealing with stuff that is already stored in memory, the Stream API only brings a performance penalty (it’s slower) with no additional memory savings (stuff is already in memory, so there’s no advantage in consuming it lazily).

If you want to use Stream effectively in this use case you will need to generate your paragraphs lazily.

tcoopman

tcoopman

Is this code on a performance critical path? If not, I would keep the code like it is now. It’s very readable.

sodapopcan

sodapopcan

If you change it to Stream.reject does it complain? If you know it’s going to be a small input then what you have is clearly no big deal. Personally I would go either the named function route or a string of ors in the fn body or I would just disable linting for that line if I know the input is always going to be small (but guh, that does suck!)

dimitarvp

dimitarvp

Normally I’d tell you to ignore this – including instruct credo to ignore it – but since it seems to be on a performance-critical path then @Marcus’s suggestion is the best one IMO.

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
Tee
can someone please explain to me how Enum.reduce works with maps
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
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
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
lk-geimfari
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
fireproofsocks
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
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
josevalim
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement