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

blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
ejpcmac
I have discovered Nix last month and I am currently on my way to migrating to it—both on macOS at home and the full NixOS distrubution at...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
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
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
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 43622 214
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement