elixirdev

elixirdev

How can I get immediate greater and lesser value of a number

value = 7
jobs = [1, 4, 9, 3, 8, 6, 2]
output should be 6,8
i have large and too many lists. please suggest me optimised solutions.

Marked As Solved

mudasobwa

mudasobwa

Creator of Cure
jobs = [1, 4, 9, 3, 8, 6, 2]
num = 7
Enum.reduce(jobs, {nil, nil}, fn
  e, {nil, max} when e < num -> {e, max}
  e, {min, nil} when e > num -> {min, e}
  e, {min, max} when e > min and e < num -> {e, max}
  e, {min, max} when e < max and e > num -> {min, e}
  _, min_max -> min_max
end)

The idea is you walk through the list narrowing down the difference. Maybe this looks shorter:

Enum.reduce(jobs, {nil, nil}, fn
  e, {min, max} when (is_nil(min) or e > min) and e < num -> {e, max}
  e, {min, max} when (is_nil(max) or e < max) and e > num -> {min, e}
  _, min_max -> min_max
end)

Also Liked

NobbZ

NobbZ

So for any given n, do you always want to check if n-1 and n+1 are in the list, or do you search those x for that holds that there is no y greather than x and lesser than n (and the other way round for the other side)?


edit

Perhaps my last sentence could be better phrased as “…or do you want to search for the greatest number smaller than n as well as the smallest number greater than n?”


another edit

If it is the first, I’d roughly do

def search(n, list) do
  Enum.filter(list, & &1 in [n-1, n+1])
end
mudasobwa

mudasobwa

Creator of Cure

Yeah, I know. I would even go with my first snippet because I value clarity way more than brevity.

Last Post!

NobbZ

NobbZ

So if you only want to see n-1 and n+1 my solution should suffice. You can microoptimise it though by using Enum.reduce or even List.fold. Also by precalculating n-1 and n+1 outside of the closure.

But I’m not sure if that will shave much…

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement