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

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
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
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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

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
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
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement