elixirdev

elixirdev

100k values in list, need to filter even, odd and prime numbers in three lists

i have a list/array of 100k values. Filter even, odd and prime numbers in separate three lists.
i am new on elixir can anyone help me about it.

Thanks

Marked As Solved

sfusato

sfusato

Have a look at Enum.reduce, Map.update, guard clauses.

something along the lines below should do it, I suppose. You will also need to implement is_prime?. is_odd and is_even are Kernel functions that can be used as guard clauses.

Enum.reduce([1, 2, 3, 4], %{}, fn 
    2, acc -> Map.update(acc, :prime, [x], &([x | &1]))
    x, acc when is_even(x) -> Map.update(acc, :even, [x], &([x | &1]))
    x, acc -> if is_prime?(x), do: Map.update(acc, :prime, [x], &([x | &1])), else: Map.update(acc, :odd, [x], &([x | &1]))
end)

uhh, wait, I made some assumptions I shouldn’t have made. 2 should appear in both even and prime lists? 7 should appear in both odd and prime? Depending on the answer to this, the above example should be adapted a bit.

Also Liked

LostKobrakai

LostKobrakai

I’m not sure “filter into lists” should be translated to Enum.filter though.

I think it was meant to be split into lists (clauses by @sfusato):

Enum.group_by(list, fn 
  2 -> :prime
  x when is_even(x) -> :even
  x -> if is_prime?(x), do: :prime, else: :odd
end)

… but could as well be have lists filtered by type of data:

%{
  prime: Enum.filter(list, &prime?/1),
  even: Enum.filter(list, &is_even/1),
  odd: Enum.filter(list, &is_odd/1)
}
# or for just one list traversal
Enum.reduce(list, %{prime: [], even: [], odd: []}, fn x, acc -> 
  [prime: prime?(x), even: is_even(x), odd: is_odd(x)]
  |> Enum.filter(fn {_, val} -> val end)
  |> Enum.reduce(acc, fn {key, _} -> 
    Map.update!(acc, key, &([x | &1]))
  end)
end)
axelson

axelson

Scenic Core Team

Hmm, if you wanted to avoid extra iteration then you could use Enum.group_by to split into multiple lists :even, :odd, :prime, [:odd, :prime], then for every number that is both odd and prime you would add them to both the :odd and :prime lists (technically you would also need [:even, :prime] for just the number 2

Where Next?

Popular in Questions Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
dokuzbir
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
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
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
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
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
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

We're in Beta

About us Mission Statement