Project

General

Profile

Actions

Feature #9023

open

Array#tail

Added by fuadksd (Fuad Saud) about 11 years ago. Updated almost 3 years ago.

Status:
Assigned
Target version:
-
[ruby-core:57862]

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.

Updated by nobu (Nobuyoshi Nakada) about 11 years ago

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

Updated by mame (Yusuke Endoh) about 11 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

FYI:

I made the same proposal in Japanese [ruby-dev:32198]. 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
  • second_or_later

In the end, knu suggested a simple workaround:

ary.drop(1)

--
Yusuke Endoh

Updated by Anonymous about 11 years 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 ).

Updated by funny_falcon (Yura Sokolov) about 11 years ago

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

2013/10/18 Fuad Saud

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

Updated by bozhidar (Bozhidar Batsov) almost 11 years 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.

Updated by Anonymous almost 11 years 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) <
> 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/

Updated by Nondv (Dmitry Non) over 8 years ago

Bozhidar Batsov wrote:

I also dislike the name Array#tail, since it's commonly associated with linked lists, not arrays.

+1 on that.

Bozhidar Batsov wrote:

I think a much better name would be Array#butfirst (and I've actually renamed Array#tail in powerpack to Array#butfirst)

Why do you and Matz not like #but_first ? Doesn't that make more sense to split words? #is_a?
I knew not much of methods that doesn't split words (actually I can remember only #upto and #downto)

Actions #8

Updated by hsbt (Hiroshi SHIBATA) almost 3 years ago

  • Project changed from 14 to Ruby master
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0