CharlesO

CharlesO

Shouldn't we have a List.nth in Elixir?

Erlang :list.nth simple, but 1 - based

nth(1, [H|_]) -> H;
nth(N, [_|T]) when N > 1 ->
    nth(N - 1, T).

Elixir Enum.atcool, but not focused on Lists

  def at(enumerable, index, default \\ nil) do
    case fetch(enumerable, index) do
      {:ok, h} -> h
      :error   -> default
    end
  end
 ...
 # other code to check if _is a list_ or not, before redirtecting to do_fetch...

Elixir could (should?) have List.nth, a 0-based equivalent of Erlang’s :list.nth that expects to work only on lists (nth could even make it into the kernel :slight_smile: )

Most Liked

michalmuskala

michalmuskala

I have no idea why this discussion continues, and where is the confusion. I have no idea where do you expect it to lead by arguing about 0-based and 1-based indexation.

Elixir uses 0-based indexation everywhere. That’s it. There is no confusion what Enum.at(4) returns - the item with index 4, which is the fifth element of the collection.

As I stated in the first post, there’s entirely no reason to introduce List.nth/2 since this is completely covered by Enum.at/2. The performance is also exactly the same for both, as both use exactly the same code when executed on lists. It makes no point to introduce a 1-based list access when access everywhere in elixir, to all indexed structures is 0-based.

Please let’s stop this discussion if there’s nothing constructive to add.

michalmuskala

michalmuskala

The Enum,fetch/2 that Enum.at/2 uses has a clause that is specialised for lists, so it should be as efficient as the Erlang’s :lists.nth/2 without introducing extra functions.

https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/enum.ex#L2773-L2778

NobbZ

NobbZ

Also nomenclature forbids zero basing nth. There is no element before the first.

Where Next?

Popular in Discussions Top

Jayshua
I recently came across the javascript library htmx. It reminded me a lot of liveview so I thought the community here might be interested....
New
owaisqayum
I have a sample string sentence = "Hello, world ... 123 *** ^%&*())^% %%:>" From this string, I want to only keep the integers, ...
New
laiboonh
Hi all, I am trying to convince my team to use liveview over the current react. What are some of the points where one should consider us...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14027 124
New
Crowdhailer
I’ve been hearing much about the new formatter and it’s something I have been keen to try. I find examples buy far the most illuminating...
248 19327 150
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
fireproofsocks
This is more of a general question, but I’m wondering how other people in the community think about the pattern matching in function sign...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement