Forum: Ruby New Array Methods idea

F555402e879ef6ad937e41bbaedb465b?d=identicon&s=25 Brandon W. (brandon_w)
on 2013-11-09 07:35
(Received via mailing list)
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?
6e366eb5a71be2bad7f383d42aeb4788?d=identicon&s=25 Justin Collins (Guest)
on 2013-11-09 08:30
(Received via mailing list)
On 11/08/2013 10:34 PM, Brandon Weaver wrote:
> # ...and/or array.tail
>
> Any thoughts on this idea?

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: https://bugs.ruby-lang.org/issues/6727

-Justin
Da57cffaec80533bee0d0446b3ea6fcd?d=identicon&s=25 Michel Demazure (badal)
on 2013-11-09 17:27
Justin Collins wrote in post #1126807:
> On 11/08/2013 10:34 PM, Brandon Weaver wrote:
>> # ...and/or array.tail
Why not

array.drop(1)

_md
F555402e879ef6ad937e41bbaedb465b?d=identicon&s=25 Brandon W. (brandon_w)
on 2013-11-09 17:47
(Received via mailing list)
It doesn't fit the feel of Ruby, and isn't nearly as expressive or
succinct
as what I mentioned above.
797ef431a5e1295b56c08e1db4c8d2df?d=identicon&s=25 botp (Guest)
on 2013-11-09 18:14
(Received via mailing list)
On Sun, Nov 10, 2013 at 12:46 AM, Brandon Weaver
<keystonelemur@gmail.com> wrote:
> It doesn't fit the feel of Ruby, and isn't nearly as expressive or succinct
> as what I mentioned above.

ruby is not lisp.

try eg,

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

kind regards -botp
F555402e879ef6ad937e41bbaedb465b?d=identicon&s=25 Brandon W. (brandon_w)
on 2013-11-09 18:27
(Received via mailing list)
"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...
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-11-10 00:46
(Received via mailing list)
On Nov 9, 2013, at 11:26 AM, Brandon Weaver <keystonelemur@gmail.com>
wrote:

>   * add methods found in Smalltalk.
>
>
> array.shift #=> 1
> array #=> [2, 3, 4, 5]
>
> kind regards -botp
>


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, Im a Lisper, too!), but nothing we can really
do over here. Even better, fork ruby, add the methods, and submit a PR.
Id 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
F555402e879ef6ad937e41bbaedb465b?d=identicon&s=25 Brandon W. (brandon_w)
on 2013-11-10 00:51
(Received via mailing list)
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.
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-11-10 02:11
(Received via mailing list)
On Nov 9, 2013, at 5:50 PM, Brandon Weaver <keystonelemur@gmail.com>
wrote:

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

Cool beans :)
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-11-12 11:33
I use Arrays with "headers" quite a lot, so I think this is an excellent
idea :)
1d57c32d7712e7222dbbfdc363473837?d=identicon&s=25 Avdi Grimm (Guest)
on 2013-11-12 12:58
(Received via mailing list)
Also:

a = [1,2,3,4,5]
first, *rest = a
first                           # => 1
rest                            # => [2, 3, 4, 5]
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.