Feature #9023

Array#tail

Added by Fuad Saud 6 months ago. Updated 5 months ago.

[ruby-core:57862]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:-
Target version:-

Description

I propose the addition of a tail method to the Array class that returns all elements but the first. It is a pretty common pattern in functional programming, but not limited to - I use it extensively in all kinds of apps/gems. The implementation would be pretty trivial, I won't risk a patch to MRI because I'm uninitiated on ruby core matters, but powerpack gem (http://github.com/bbatsov/powerpack) implements it in ruby in terms of slices.

History

#1 Updated by Nobuyoshi Nakada 6 months ago

How about to extend Array#last (and Array#first too) to accept negative size?

#2 Updated by Yusuke Endoh 6 months ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto

FYI:

I made the same proposal in Japanese . But matz did not agree with me because "tail" did not look a good name to him. Indeed, it is unnatural that all but "head" is "tail", especially, for those who are not familiar with typed functional programming such as ML and Haskell.

Even for very functional people.
http://wadler.blogspot.jp/2009/11/list-is-odd-creature.html

Matz counter-proposed "butfirst" (which comes from LOGO) but received many negative comments, he said. The following names were suggested but matz did not like all of them.

  • cdr
  • rest
  • body
  • tail
  • subseq
  • headless
  • follower
  • but_first
  • subsequence
  • from_second
  • continuation
  • secondorlater

In the end, knu suggested a simple workaround:

ary.drop(1)

Yusuke Endoh mame@tsg.ne.jp

#3 Updated by Boris Stitnicky 6 months ago

+1 for #tail. I do not know why it seemed not a good name to Matz, but in case he reads this,
I experienced that the notion that "everything but head is tail" is easy to learn and remember.

PS: My second and third favorite is #cdr and #rest. In the meantime, like-minded can enjoy #cdr
along with #car, #cadr, #cddr and progeny in my (begging forgiveness for self-promotion) Pyper
gem ( https://github.com/boris-s/pyper ).

#4 Updated by Yura Sokolov 6 months ago

List (and Array) is a snake : everything but head is tail :)

2013/10/18 Fuad Saud fuadksd@gmail.com

I can understand maybe tail is not the best name to describe this concept,
nor any of the other options. But it is still a pretty common idiom among
many functional languages.

I like cdr, but it wouldn't make much sense to introduce it in ruby today
- it may confuse newcomers a lot, since that's not vey intuitive.

Rest doesn't feel right. What is rest? It's a remainder, but if you drop
the last element, then what remains is still a kind of rest :/

Butfirst is weird for me, but bbatsov (powerpack maintainer) prefered it
over tail, and it makes it very clear what it means (e.g.
people.but_first.each do ... means iterate over people but the first).

#5 Updated by Bozhidar Batsov 5 months ago

I also dislike the name Array#tail, since it's commonly associated with linked lists, not arrays. I think a much better name would be Array#butfirst (and I've actually renamed Array#tail in powerpack to Array#butfirst). On a related note it makes sense to have a method Array#butlast as well - it should obviously everything except the last element of the array.

#6 Updated by Anonymous 5 months ago

rest sounds definitely better. Clojure uses it and it feels very natural
and readable.

On Wed, Nov 13, 2013 at 7:59 AM, bozhidar (Bozhidar Batsov) <
bozhidar@batsov.com> wrote:

Issue #9023 has been updated by bozhidar (Bozhidar Batsov).

I also dislike the name Array#tail, since it's commonly associated with
linked lists, not arrays. I think a much better name would be
Array#butfirst (and I've actually renamed Array#tail in powerpack to
Array#butfirst). On a related note it makes sense to have a method
Array#butlast as well - it should obviously everything except the last

element of the array.

Feature #9023: Array#tail
https://bugs.ruby-lang.org/issues/9023#change-42901

Author: fuadksd (Fuad Saud)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category:
Target version:

I propose the addition of a tail method to the Array class that returns
all elements but the first. It is a pretty common pattern in functional
programming, but not limited to - I use it extensively in all kinds of
apps/gems. The implementation would be pretty trivial, I won't risk a patch
to MRI because I'm uninitiated on ruby core matters, but powerpack gem (
http://github.com/bbatsov/powerpack) implements it in ruby in terms of
slices.

http://bugs.ruby-lang.org/

Also available in: Atom PDF