Array#rest

C

Charles Comstock

I find myself frequently using array slice of 1..-1, to pass the rest of an
array onto something. While this is a reasonably concise syntax it is not the
most readable. I was curious if there had ever been interest in a rest operator
on an array?

For example

a = [1,2,3,4]
a[1..-1] # -> [2,3,4]
a.rest # -> [2,3,4]

I realize this might cause a few people to write programs in a more list
processing stance, for which the current arrays are probably not best suited.
However, the majority of the times I use this operator is for say reading in a
command as a the first arg, and passing the rest of the elements as the
parameters, and thus it would not be a performance issue. Rest should also have
the same copy symantics as the 1..-1 slice.

Any particular objections to this? Should I make this an RCR?

Charles Comstock
 
R

Robert Klemme

Charles Comstock said:
I find myself frequently using array slice of 1..-1, to pass the rest of an
array onto something. While this is a reasonably concise syntax it is not the
most readable. I was curious if there had ever been interest in a rest operator
on an array?

For example

a = [1,2,3,4]
a[1..-1] # -> [2,3,4]
a.rest # -> [2,3,4]

I realize this might cause a few people to write programs in a more list
processing stance, for which the current arrays are probably not best suited.
However, the majority of the times I use this operator is for say reading in a
command as a the first arg, and passing the rest of the elements as the
parameters, and thus it would not be a performance issue. Rest should also have
the same copy symantics as the 1..-1 slice.

Any particular objections to this? Should I make this an RCR?

If you want to do something about this I'd prefer a symmetric solution like
Array#head and Array#tail or Array#car and Array#cdr.

Btw: is "rest" in English the same as in German? I always thought "rest"
rather means "pause" than "reminder"...

Kind regards

robert
 
A

Alexander Kellett

If you want to do something about this I'd prefer a symmetric solution like
Array#head and Array#tail or Array#car and Array#cdr.

first / rest is symmetric...
head / tail unix commands don't default to 1 "line"
car + cdr have crappy names.
i'm sure there is something better for this :)

Alex
 
D

David A. Black

Hi --

Charles Comstock said:
I find myself frequently using array slice of 1..-1, to pass the rest of an
array onto something. While this is a reasonably concise syntax it is not the
most readable. I was curious if there had ever been interest in a rest operator
on an array?

For example

a = [1,2,3,4]
a[1..-1] # -> [2,3,4]
a.rest # -> [2,3,4]

I realize this might cause a few people to write programs in a more list
processing stance, for which the current arrays are probably not best suited.
However, the majority of the times I use this operator is for say reading in a
command as a the first arg, and passing the rest of the elements as the
parameters, and thus it would not be a performance issue. Rest should also have
the same copy symantics as the 1..-1 slice.

Any particular objections to this? Should I make this an RCR?

If you want to do something about this I'd prefer a symmetric solution like
Array#head and Array#tail or Array#car and Array#cdr.

I agree, though my first choice would be just to keep it as is. I'm
not entirely sure why... it could just be jitters about creeping
method multiplication, though I do try not to confuse RCRs in my mind
with RCs :)
Btw: is "rest" in English the same as in German? I always thought "rest"
rather means "pause" than "reminder"...

s/reminder/remainder/ :) It has several meanings (it can be
equivalent to "Ruhe"), including "remainder": "While he stayed, the
rest of us went home." But when you want to emphasize that everything
except what remains has been destroyed, you would say, e.g., "the
remains of the building". (As opposed to: "They painted the second
floor but not the rest of the building.")


David
 
S

Simon Strandgaard

first / rest is symmetric...
head / tail unix commands don't default to 1 "line"
car + cdr have crappy names.
i'm sure there is something better for this :)

What about

#begin == slice(0..-2)
#end == slice(1..-1)
 
L

Linus Sellberg

Btw: is "rest" in English the same as in German? I always thought "rest"
rather means "pause" than "reminder"...

It has both meanings.

(in swedish we have 'rast' for pause, and 'rest' for remainder)
 
P

Patrick Gundlach

Alexander Kellett said:
first / rest is symmetric...
head / tail unix commands don't default to 1 "line"
car + cdr have crappy names.

Array#car and Array#cdr were the first one that came into my
mind... And the name is only crappy when not used to it...

then we can even define Array#cadr and so on ;-)


Patrick
 
R

Robert Klemme

David A. Black said:
Hi --

news:p[email protected]...
I find myself frequently using array slice of 1..-1, to pass the rest
of
an
array onto something. While this is a reasonably concise syntax it is
not
the
most readable. I was curious if there had ever been interest in a
rest
operator
on an array?

For example

a = [1,2,3,4]
a[1..-1] # -> [2,3,4]
a.rest # -> [2,3,4]

I realize this might cause a few people to write programs in a more list
processing stance, for which the current arrays are probably not best suited.
However, the majority of the times I use this operator is for say
reading
in a
command as a the first arg, and passing the rest of the elements as the
parameters, and thus it would not be a performance issue. Rest should also have
the same copy symantics as the 1..-1 slice.

Any particular objections to this? Should I make this an RCR?

If you want to do something about this I'd prefer a symmetric solution like
Array#head and Array#tail or Array#car and Array#cdr.

I agree, though my first choice would be just to keep it as is. I'm
not entirely sure why... it could just be jitters about creeping
method multiplication, though I do try not to confuse RCRs in my mind
with RCs :)
Btw: is "rest" in English the same as in German? I always thought "rest"
rather means "pause" than "reminder"...

s/reminder/remainder/ :)

Darn! Yes, of course I meant "remainder".
It has several meanings (it can be
equivalent to "Ruhe"), including "remainder": "While he stayed, the
rest of us went home."

Ah, yes. Forgot that one. It sounded odd at first sight but this example
feels perfectly ok.
But when you want to emphasize that everything
except what remains has been destroyed, you would say, e.g., "the
remains of the building". (As opposed to: "They painted the second
floor but not the rest of the building.")

Thanks for the lengthy explanation (or was it "expiration"? :))))

Regards

robert
 
R

Robert Klemme

Linus Sellberg said:
It has both meanings.

(in swedish we have 'rast' for pause, and 'rest' for remainder)

Ha, in German, too! :) There's so nice words like "Raststätte",
"Rastplatz" etc. :))

robert
 
D

David A. Black

Hi --

Array#car and Array#cdr were the first one that came into my
mind... And the name is only crappy when not used to it...

then we can even define Array#cadr and so on ;-)

What problem would this solve?


David
 
P

Patrick Gundlach

Hello David,
What problem would this solve?

$USERDEFINED problem. In Lisp there are car, cdr and often(?) cddr
cadr,... which access members of lists (cadr acesses the second
element, cddr the third, fourth .... element). Absolutely no need to
put this into ruby itself though. Its more a mnemonic and shorter to
type. Example (setq mylist '((1 a))) then (car (cdr 'mylist)) is the
same as (caar 'mylist) which is 1.

Patrick
 
F

Florian Gross

Charles said:
a = [1,2,3,4]
a[1..-1] # -> [2,3,4]
a.rest # -> [2,3,4]
Any particular objections to this? Should I make this an RCR?

I already tried to introduce this into Ruby as a.last(-1), but matz
didn't seem to like it. I wish you good luck with a new RCR. :)
 
T

Tom Counsell

Charles Comstock said:
a = [1,2,3,4]
a[1..-1] # -> [2,3,4]
a.rest # -> [2,3,4]

In what way are you after something different from Array.shift?

a = [1,2,3,4]
wanted = a.shift # 1
passed = a # [2,3,4]

Tom
 
C

Charles Comstock

If you want to do something about this I'd prefer a symmetric solution like
Array#head and Array#tail or Array#car and Array#cdr.

Btw: is "rest" in English the same as in German? I always thought "rest"
rather means "pause" than "reminder"...

You can refer to the first and rest of the list, in fact I had those
bound when I first started using scheme, as opposed to car & cdr until
I realized the value of cadr, cadar and the like. We already have
first and last, so I assumed rest would be reasonably symmetric with
this. a.last(a.size-1) doesn't look very good either, though it might
be clearer then [1..-1].

With regards to english use, rest does mean pause, but it is also quite
commonly used to refer to the remainder of something. I'd almost
hazard to guess they are equally used. Certainly rest is used more
often then remainder in common speech I think.

Charles Comstock
 
C

Charles Comstock

Charles Comstock said:
a = [1,2,3,4]
a[1..-1] # -> [2,3,4]
a.rest # -> [2,3,4]

In what way are you after something different from Array.shift?

a = [1,2,3,4]
wanted = a.shift # 1
passed = a # [2,3,4]

Tom

I don't want a destructive function. A number of times I have used the shift
and the remainder, but there are a number of times you want a non destructive
copy of the rest of the list. Hence rest ;p

Charles Comstock
 
D

David A. Black

Hi --

Hello David,


$USERDEFINED problem. In Lisp there are car, cdr and often(?) cddr
cadr,... which access members of lists (cadr acesses the second
element, cddr the third, fourth .... element). Absolutely no need to
put this into ruby itself though. Its more a mnemonic and shorter to
type. Example (setq mylist '((1 a))) then (car (cdr 'mylist)) is the
same as (caar 'mylist) which is 1.

I know about it from Lisp, just wondering how/whether it would enhance
Ruby. I can see 'rest' as a companion to 'first' and 'last', but
going down the road of car, cdr and friends seems like a language
pastiche.


David
 
R

Robert Klemme

Patrick Gundlach said:
Hello David,


$USERDEFINED problem. In Lisp there are car, cdr and often(?) cddr
cadr,... which access members of lists (cadr acesses the second
element, cddr the third, fourth .... element). Absolutely no need to
put this into ruby itself though. Its more a mnemonic and shorter to
type. Example (setq mylist '((1 a))) then (car (cdr 'mylist)) is the
same as (caar 'mylist) which is 1.

Huh? What am I missing here? It seems (caar x) is short for (car (car
x))...

[1]> (setq mylist '((1 a)))
((1 A))
[2]> (car (cdr 'mylist))

*** - CDR: MYLIST is not a LIST
Break 1 [3]>
[4]> (car (cdr mylist))
NIL
[5]> (caar 'mylist)

*** - CAAR: MYLIST is not a LIST
Break 1 [6]>
[7]> (caar mylist)
1
[8]> (car (car mylist))
1

GNU clisp

robert
 
F

Florian Frank

Hi,

I don't want a destructive function. A number of times I have used
the shift
and the remainder, but there are a number of times you want a non
destructive
copy of the rest of the list. Hence rest ;p

If you don't want to destroy the original list you can use the splat
operator
in an assignment:

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

Florian Frank
 
R

Robert Klemme

David A. Black said:
Hi --



I know about it from Lisp, just wondering how/whether it would enhance
Ruby. I can see 'rest' as a companion to 'first' and 'last', but
going down the road of car, cdr and friends seems like a language
pastiche.

Sounds like a good suggestion for an RAA project "rlisp" to make lispers
feel at home with Ruby. :)

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top