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.

Where Next?

Popular in Questions Top

sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
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
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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