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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
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
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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

We're in Beta

About us Mission Statement