CharlesO

CharlesO

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: )

Showing Posts 1 to 10

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.

CharlesO

CharlesO OP

I don’t follow your argument

NobbZ

NobbZ

Well by writing List.nth(list, 0), you do request the element right before the first one.

There is never a zeroth item. You always start with the first. When one calls the function nth, he has to start counting with 1.

CharlesO

CharlesO OP

Doesn’t Enum.at/2 contradict what you are saying?

Enum.at list, 0

sztosz

sztosz

It’s a language (any language english, polish, german, whatever) trait. There can not 0th element, how would you call it? Zeroth? You have to start count from 1 (one), so → First, Second, Third, Fourth … Nth element. There is simply no Zeroth element :wink: It would be illogical.

On the other hand something can be at position marked as 0, zero. Something before it is at position 1 relative to position 0, and something behind is at position -1 (also relative to that 0 position). Or the other way round, based on do you want position number before or after that position. And element on position 1 is first from from, but that does not mean you’re “zeroth” from you.

Of course first element from the beginning of the list would be that at position 0, but then we would meant that we have to remember that nth element is on position n - 1. At that can lead to subtle off by 1 errors I would rather avoid.

In Erlang Unless otherwise stated, all functions assume that position numbering starts at 1 but in elixir we start at 0, hence having List.nth does not make, sadly, too much sense :frowning:

minhajuddin

minhajuddin

I disagree :slight_smile: 0th seems very logical if you think of it as a position. If that doesn’t make sense then how does nth make sense? You could even say Enum.at shows the nth element in a list where n is a position.

sztosz

sztosz

Can use a word to describe that position?

Onor.io

Onor.io

Why add a new function which can be easily done in just the fashion that @michalmuskala outlines above?

peerreynders

peerreynders

“0” is logical as an item offset when applied to a number of data type instances (of equal size), stored in a contiguous area of memory - what is typically thought of as an array.

In C this goes back to pointer arithmetic: arr[i] == *(arr + i) - this “index” syntax has been basically grandfathered into it’s various descendants and derivatives.

  • So item 1 is at offset 0 - or more generally
  • item n is at offset n-1 for n > 0

In functional programming languages “List” typically refers to a “Linked List”. In a linked list the items aren’t (usually) adjacent in memory so an offset isn’t as useful for “Lists”. Now Enum.at and Enum.fetch adopted the familiar 0-based index - in a sense that “index” is describing the “distance” into the enumeration starting at item 1 - i.e. item 1 is found at “distance” 0 from item 1 (rather than 0 being a “position”).

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
maennchen
:warning: Security advisory: Decimal DoS vulnerability A vulnerability has been published for decimal where very large exponents can cau...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New

Other Trending Topics Top

marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews