Any reason Enum.first and Enum.rest are not implemented?

101 views
Skip to first unread message

Sam Vervaeck

unread,
Feb 16, 2017, 6:02:24 AM2/16/17
to elixir-lang-core
Hi,

I've found List.first and List.rest, but I couldn't find Enum.first and Enum.rest. Is there any reason this is only implemented for lists? I can think of a few cases were this would be useful. Any recursive algorithm that runs following the prescription "take one element out the bunch, process it, and take care of the rest later" would, I think, benefit from it.

Kind regards,
Sam

Sam Vervaeck

unread,
Feb 16, 2017, 6:12:46 AM2/16/17
to elixir-lang-core
Correction: there is no `List.rest`. Using the same reasoning as below, I think this would be useful, too.

Op donderdag 16 februari 2017 12:02:24 UTC+1 schreef Sam Vervaeck:

José Valim

unread,
Feb 16, 2017, 6:20:05 AM2/16/17
to elixir-l...@googlegroups.com
Because enumerables have no guarantee of ordering, so the notion of first can be misleading. You can use Enum.at(..., 0) but also with the disclaimer that it can be anything within a data-structure that does not provide order.



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/5fa50f90-72b2-4c73-a012-de052f4c036b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Hamilton

unread,
Feb 17, 2017, 7:20:31 PM2/17/17
to elixir-l...@googlegroups.com
I'd support Enum.pop, which would return an element and an new enumerable without the element.

On Thu, Feb 16, 2017 at 3:20 AM José Valim <jose....@plataformatec.com.br> wrote:
Because enumerables have no guarantee of ordering, so the notion of first can be misleading. You can use Enum.at(..., 0) but also with the disclaimer that it can be anything within a data-structure that does not provide order.



José Valim
Skype: jv.ptec
Founder and Director of R&D
On Thu, Feb 16, 2017 at 12:02 PM, Sam Vervaeck <vervae...@skynet.be> wrote:
Hi,

I've found List.first and List.rest, but I couldn't find Enum.first and Enum.rest. Is there any reason this is only implemented for lists? I can think of a few cases were this would be useful. Any recursive algorithm that runs following the prescription "take one element out the bunch, process it, and take care of the rest later" would, I think, benefit from it.

Kind regards,
Sam

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4KG59KQbmZmhp7pTzkrO%2BF0T2M7_cZhbUT5tKfLgoczyg%40mail.gmail.com.

Tallak Tveide

unread,
Feb 18, 2017, 12:14:03 PM2/18/17
to elixir-lang-core
stream_split would do something like that. The tail is stored in a continuation function, so i'm not sure if this is 'good code', and i'd not vouch for putting something like that in stdlib without a thorough analysis (eg covering issues related to cleaning up the enumerable if the tail is not iterated after)

https://hex.pm/packages/stream_split

For simple cases though it works well :)

Sam Vervaeck

unread,
Feb 20, 2017, 6:47:22 AM2/20/17
to elixir-l...@googlegroups.com
Seems like my mail got lost in my outbox. Attempt #2.

I agree with Peter Hamilton, a Enum.pop method would be quite nice, though I understand the difficulties.

Sam

You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/-fFtCPJPV1o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAOMhEnyJD5%2BCeGxPU_WuJXRT10MJWUk7x7zNHKagQFBZY%3DG%2BTQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages