DevHeads.net

New Array Methods idea

So granted I've ventured over to Lisp land a bit, but these ideas still
seem like they could be useful:

array = [1, 2, 3, 4, 5]

# Exists
array.first # => 1

# Proposed
array.rest # => [2, 3, 4, 5]
# ...and/or array.tail

Granted it would be as simple as array[1..-1] but that lacks the distinct
eloquence of most ruby syntax and it seems to be far more of a language
feature possibility.

Monkey patching it on would be trivial, yes, but I would rather avoid that
in favor of discussing this as a possible feature.

This of course inspired by Lisp's car/cdr or first/rest and head/tail (of
which I don't remember the origin.)

Any thoughts on this idea?

Comments

Re: New Array Methods idea

By Andreas S. at 11/12/2013 - 15:56

Someone already did, and I did actually go directly to that record. The
demand is there and is about a season old so I was mixing it up a bit
after I discovered the report to add it.

Re: New Array Methods idea

By Andreas S. at 11/12/2013 - 06:33

I use Arrays with "headers" quite a lot, so I think this is an excellent
idea :)

Re: New Array Methods idea

By Justin Collins at 11/09/2013 - 03:29

On 11/08/2013 10:34 PM, Brandon Weaver wrote:
Yes, please. I use `array[1..-1]` all the time and it feels cumbersome.

Looks like there is a proposal, although it has not been touched in just
over a year: <a href="https://bugs.ruby-lang.org/issues/6727" title="https://bugs.ruby-lang.org/issues/6727">https://bugs.ruby-lang.org/issues/6727</a>

-Justin

Re: New Array Methods idea

By Andreas S. at 11/09/2013 - 12:27

Justin Collins wrote in post #1126807:
array.drop(1)

_md

Re: New Array Methods idea

By Avdi Grimm at 11/12/2013 - 07:57

a = [1,2,3,4,5]
first, *rest = a
first # => 1
rest # => [2, 3, 4, 5]

On Sat, Nov 9, 2013 at 11:27 AM, Michel Demazure <lists@ruby-forum.com>wrote:

Re: New Array Methods idea

By Brandon Weaver at 11/09/2013 - 12:46

It doesn't fit the feel of Ruby, and isn't nearly as expressive or succinct
as what I mentioned above.

Re: New Array Methods idea

By botp at 11/09/2013 - 13:13

On Sun, Nov 10, 2013 at 12:46 AM, Brandon Weaver
< ... at gmail dot com> wrote:
ruby is not lisp.

try eg,

array.shift #=> 1
array #=> [2, 3, 4, 5]

kind regards -botp

Re: New Array Methods idea

By Brandon Weaver at 11/09/2013 - 13:26

"Some may say Ruby is a bad rip-off of Lisp or Smalltalk, and I admit that.
But it is nicer to ordinary people."
- Matz, LL2

Ruby is a language designed in the following steps:

* take a simple lisp language (like one prior to CL).
* remove macros, s-expression.
* add simple object system (much simpler than CLOS).
* add blocks, inspired by higher order functions.
* add methods found in Smalltalk.
* add functionality found in Perl (in OO way).

So, Ruby was a Lisp originally, in theory.
Let's call it MatzLisp from now on. ;-)

- Matz

About that...

Re: New Array Methods idea

By tamouse (Tamara... at 11/09/2013 - 19:45

All interesting and such, but moot given core language change discussions happens over on ruby-core mailing list. I think your suggestion has merit (Hey, I’m a Lisper, too!), but nothing we can really do over here. Even better, fork ruby, add the methods, and submit a PR. I’d say it should be in Enumerable, as well. The implementation should be dead-easy:

class Array
def rest(n=1)
self.drop(n)
end
end

Re: New Array Methods idea

By Brandon Weaver at 11/09/2013 - 19:50

Someone already did, and I did actually go straight to that list. The
request is there and is about a year old so I was stirring it up a bit
after I found the report to add it.

On Sat, Nov 9, 2013 at 5:45 PM, Tamara Temple <tamouse. ... at gmail dot com>wrote:

Re: New Array Methods idea

By tamouse (Tamara... at 11/09/2013 - 21:10

Cool beans :)