A more concise array access (using negative indices)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

A more concise array access (using negative indices)

niko.schwarz
Hi guys,

Am I the only one who finds the ruby protocol for accessing
collections richer at times? In Smalltalk, if ary is an array, how do
I get ary without its last element?

In Ruby, it's dead easy: ary[0..-2]. I can do that by heart after not
having done any serious Ruby in a long time. In Smalltalk, I have to
search through a long list of method names, because there are just so
many possible names for the method.

The point is: would it be totally out of reach to try and get a more
concise and unified way to access sequenceable collections?

I know I'm sort of asking for the slaughter of a holy cow: Smalltalk
only has telling and easy message names all over. Or wait, does it?
There's of course Class >> #methodName. And 2 @ 3 for points.

Just to put something on the table, how about:

(ary at: 1, -2)

Or, without braces, if you don't mind the reversal:

1,-2 @ ary

Or, consistent with current naming conventions:

(ary from: 1 to: -2)

Just my 2 cents, what do you think?


Niko
--
http://scg.unibe.ch/staff/Schwarz
twitter.com/nes1983
Tel: +41 076 235 8683

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

Adrian Lienhard
Yes, I would welcome such an extension!

I wouldn't invent a new syntax, though, but just use the existing protocol

array copyFrom: 1 to: -2

Cheers,
Adrian


On Jun 7, 2010, at 13:47 , Niko Schwarz wrote:

> Hi guys,
>
> Am I the only one who finds the ruby protocol for accessing
> collections richer at times? In Smalltalk, if ary is an array, how do
> I get ary without its last element?
>
> In Ruby, it's dead easy: ary[0..-2]. I can do that by heart after not
> having done any serious Ruby in a long time. In Smalltalk, I have to
> search through a long list of method names, because there are just so
> many possible names for the method.
>
> The point is: would it be totally out of reach to try and get a more
> concise and unified way to access sequenceable collections?
>
> I know I'm sort of asking for the slaughter of a holy cow: Smalltalk
> only has telling and easy message names all over. Or wait, does it?
> There's of course Class >> #methodName. And 2 @ 3 for points.
>
> Just to put something on the table, how about:
>
> (ary at: 1, -2)
>
> Or, without braces, if you don't mind the reversal:
>
> 1,-2 @ ary
>
> Or, consistent with current naming conventions:
>
> (ary from: 1 to: -2)
>
> Just my 2 cents, what do you think?
>
>
> Niko
> --
> http://scg.unibe.ch/staff/Schwarz
> twitter.com/nes1983
> Tel: +41 076 235 8683
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

Lukas Renggli
In reply to this post by niko.schwarz
> Am I the only one who finds the ruby protocol for accessing
> collections richer at times? In Smalltalk, if ary is an array, how do
> I get ary without its last element?

anArray allButLast

> I know I'm sort of asking for the slaughter of a holy cow: Smalltalk
> only has telling and easy message names all over. Or wait, does it?
> There's of course Class >> #methodName. And 2 @ 3 for points.

There is the method finder, type

     #(1 2 3) . #(1 2)

and it should give you the answer.

> Just to put something on the table, how about:
>
> (ary at: 1, -2)
>
> Or, without braces, if you don't mind the reversal:
>
> 1,-2 @ ary
>
> Or, consistent with current naming conventions:
>
> (ary from: 1 to: -2)

See FScript (http://www.fscript.org/), it has quite cool syntax
extensions for array programming.

Lukas

--
Lukas Renggli
www.lukas-renggli.ch

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

Stéphane Ducasse
In reply to this post by Adrian Lienhard
yes this one looks good.
but

        copyFrom: 1 to: does not convey that you should pass the second argument starting reverse.

        copyFrom: 3 toFromEnd: -2
       
would be better

Stef



> Yes, I would welcome such an extension!
>
> I wouldn't invent a new syntax, though, but just use the existing protocol
>
> array copyFrom: 1 to: -2
>
> Cheers,
> Adrian
>
>
> On Jun 7, 2010, at 13:47 , Niko Schwarz wrote:
>
>> Hi guys,
>>
>> Am I the only one who finds the ruby protocol for accessing
>> collections richer at times? In Smalltalk, if ary is an array, how do
>> I get ary without its last element?
>>
>> In Ruby, it's dead easy: ary[0..-2]. I can do that by heart after not
>> having done any serious Ruby in a long time. In Smalltalk, I have to
>> search through a long list of method names, because there are just so
>> many possible names for the method.
>>
>> The point is: would it be totally out of reach to try and get a more
>> concise and unified way to access sequenceable collections?
>>
>> I know I'm sort of asking for the slaughter of a holy cow: Smalltalk
>> only has telling and easy message names all over. Or wait, does it?
>> There's of course Class >> #methodName. And 2 @ 3 for points.
>>
>> Just to put something on the table, how about:
>>
>> (ary at: 1, -2)
>>
>> Or, without braces, if you don't mind the reversal:
>>
>> 1,-2 @ ary
>>
>> Or, consistent with current naming conventions:
>>
>> (ary from: 1 to: -2)
>>
>> Just my 2 cents, what do you think?
>>
>>
>> Niko
>> --
>> http://scg.unibe.ch/staff/Schwarz
>> twitter.com/nes1983
>> Tel: +41 076 235 8683
>>
>> _______________________________________________
>> Pharo-project mailing list
>> [hidden email]
>> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

niko.schwarz
On Mon, Jun 7, 2010 at 3:23 PM, Stéphane Ducasse
<[hidden email]> wrote:
> yes this one looks good.
> but
>
>        copyFrom: 1 to: does not convey that you should pass the second argument starting reverse.

Well, in ruby it's a general convention that negative indices start
counting from the right. A reasonable implementation would change all
sequenceable collections such that they accept negative indices.

In Phexample syntax:

((1 to: 5) at: -2) should = 4.

The alternative is to always have two accessors, then of course non-negative:

(1 to: 5) atFromEnd: 2)  should = 4.

and:

('hello' from: 2 toFromEnd: 2) should = 'ell'

I clearly prefer the negative indices. Things you do all the time can
be much shorter than things you don't do so often. Huffman-encode your
programming :)

Cheers,

Niko


--
http://scg.unibe.ch/staff/Schwarz
twitter.com/nes1983
Tel: +41 076 235 8683

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

Damien Pollet
In reply to this post by Stéphane Ducasse
On Mon, Jun 7, 2010 at 15:23, Stéphane Ducasse
<[hidden email]> wrote:
>        copyFrom: 1 to: does not convey that you should pass the second argument starting reverse.

Of course, since it's the sign that does that…

>        copyFrom: 3 toFromEnd: -2

then, why -2 if it's explicitly from the end ???


--
Damien Pollet
type less, do more [ | ] http://people.untyped.org/damien.pollet

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

Ramon Leon-5
In reply to this post by Lukas Renggli
On 6/7/2010 5:03 AM, Lukas Renggli wrote:
>> Am I the only one who finds the ruby protocol for accessing
>> collections richer at times? In Smalltalk, if ary is an array, how do
>> I get ary without its last element?
>
> anArray allButLast

Or for other amounts, allButLast: x

There's also allButFirst and allButFirst: x.  A look at the protocol of
SequenceableCollection would have revealed this in seconds.

--
Ramon Leon
http://onsmalltalk.com

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

Stéphane Ducasse
In reply to this post by Damien Pollet

On Jun 7, 2010, at 3:38 PM, Damien Pollet wrote:

> On Mon, Jun 7, 2010 at 15:23, Stéphane Ducasse
> <[hidden email]> wrote:
>>        copyFrom: 1 to: does not convey that you should pass the second argument starting reverse.
>
> Of course, since it's the sign that does that…
>
>>        copyFrom: 3 toFromEnd: -2
>
> then, why -2 if it's explicitly from the end ???

ah why not.

>
>
> --
> Damien Pollet
> type less, do more [ | ] http://people.untyped.org/damien.pollet
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

niko.schwarz
In reply to this post by Ramon Leon-5
Well, I'm just arguing that in Ruby, the simple convention that
negative indices start from left to right map to a zoo of different
methods in Smalltalk.

You didn't comment on:

('hello' from: 2 toFromEnd: 2) should = 'ell'

Would that find your blessing?

niko


On Mon, Jun 7, 2010 at 6:59 PM, Ramon Leon <[hidden email]> wrote:

> On 6/7/2010 5:03 AM, Lukas Renggli wrote:
>>>
>>> Am I the only one who finds the ruby protocol for accessing
>>> collections richer at times? In Smalltalk, if ary is an array, how do
>>> I get ary without its last element?
>>
>> anArray allButLast
>
> Or for other amounts, allButLast: x
>
> There's also allButFirst and allButFirst: x.  A look at the protocol of
> SequenceableCollection would have revealed this in seconds.
>
> --
> Ramon Leon
> http://onsmalltalk.com
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>



--
http://scg.unibe.ch/staff/Schwarz
twitter.com/nes1983
Tel: +41 076 235 8683

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

hilaire
In reply to this post by niko.schwarz
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

Stéphane Ducasse
In reply to this post by niko.schwarz

On Jun 7, 2010, at 3:36 PM, Niko Schwarz wrote:

> On Mon, Jun 7, 2010 at 3:23 PM, Stéphane Ducasse
> <[hidden email]> wrote:
>> yes this one looks good.
>> but
>>
>>        copyFrom: 1 to: does not convey that you should pass the second argument starting reverse.
>
> Well, in ruby it's a general convention that negative indices start
> counting from the right. A reasonable implementation would change all
> sequenceable collections such that they accept negative indices.
>
> In Phexample syntax:
>
> ((1 to: 5) at: -2) should = 4.
>
> The alternative is to always have two accessors, then of course non-negative:
>
> (1 to: 5) atFromEnd: 2)  should = 4.
>
> and:
>
> ('hello' from: 2 toFromEnd: 2) should = 'ell'
>
> I clearly prefer the negative indices. Things you do all the time can
> be much shorter than things you don't do so often. Huffman-encode your
> programming :)


s

stef ?
sure ?
shit ?

huffman encding your programming is a stupid idea.
_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

Stéphane Ducasse
In reply to this post by niko.schwarz
If you want a chance to your idea propose some code and some tests

H     E    L   L    O
1     2    3   4    5
-5 -4   -3  -2   -1

hello from: 2 to: -2  -> ell   ok

'hello' from: 2 to: -2  -> ello
'hello' from: 2 to: 0 -> ???

This is strange because I thought intuitively and wrong that it would be means complete

from: 2 toFromEnd: 2
from: 2 to: -2

So do you have other usage of negative index in ruby?
Because I'm not sure I like that?



Stef

>>        copyFrom: 1 to: does not convey that you should pass the second argument starting reverse.
>
> Well, in ruby it's a general convention that negative indices start
> counting from the right. A reasonable implementation would change all
> sequenceable collections such that they accept negative indices.
>
> In Phexample syntax:
>
> ((1 to: 5) at: -2) should = 4.
>
> The alternative is to always have two accessors, then of course non-negative:
>
> (1 to: 5) atFromEnd: 2)  should = 4.
>
> and:
>
> ('hello' from: 2 toFromEnd: 2) should = 'ell'
>
> I clearly prefer the negative indices. Things you do all the time can
> be much shorter than things you don't do so often. Huffman-encode your
> programming :)
>
> Cheers,
>
> Niko
>
>
> --
> http://scg.unibe.ch/staff/Schwarz
> twitter.com/nes1983
> Tel: +41 076 235 8683
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

Stéphane Ducasse
In reply to this post by hilaire
nope this is a primitive and we do not have multiple dispatch

Stef

On Jun 7, 2010, at 9:30 PM, Hilaire Fernandes wrote:

> Niko Schwarz a écrit :
>
>> Just to put something on the table, how about:
>> (ary at: 1, -2)
>
> Or #at: could accept an interval as argument
>
>
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

niko.schwarz
In reply to this post by hilaire
That would be precisely what ruby does.

It's

ary at: (1 to: 3)

vs.

ary from: 1 to: 3.

Right there, the second one looks more useful.
But, the first thing leads to beautiful code in Matlab and Mathematica
when dealing with vectors and matrices. So it may well be worth it.

Cheers,

Niko


On Mon, Jun 7, 2010 at 9:30 PM, Hilaire Fernandes
<[hidden email]> wrote:

> Niko Schwarz a écrit :
>
>> Just to put something on the table, how about:
>>
>> (ary at: 1, -2)
>
> Or #at: could accept an interval as argument
>
>
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>



--
http://scg.unibe.ch/staff/Schwarz
twitter.com/nes1983
Tel: +41 076 235 8683

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

Ramon Leon-5
In reply to this post by niko.schwarz
On 6/7/2010 12:17 PM, Niko Schwarz wrote:

> Well, I'm just arguing that in Ruby, the simple convention that
> negative indices start from left to right map to a zoo of different
> methods in Smalltalk.
>
> You didn't comment on:
>
> ('hello' from: 2 toFromEnd: 2) should = 'ell'
>
> Would that find your blessing?
>
> niko

What's the goal, to be more like Ruby?  If that's the case I'm sure
there's a thousand things you could change, but it's not Ruby, it's
Smalltalk.  Ruby seems to have a style where you can send just about
anything to a method and it tries to figure out what to do based on the
type of the arg, they like magic and the smaller API; Smalltalk tends to
just have a dozen different but similarly named methods that let you
accomplish the same thing.  I prefer that latter style, less magic and
more smaller simpler well factored methods with a larger API and less
implicit assumptions.

Personally, I don't like a lot of unspoken assumptions like negative
indices meaning something that isn't stated in the method name, so yes I
like...

('hello' from: 2 toFromEnd: 2) should = 'ell'

better than using negative indices.  Not sure I like #from:toFromEnd: as
the selector name, but I like the spirit of it, it makes it explicit.
I'd probably like #copyFrom:fromEnd: better, but that's just personal
preference as it goes better with #copyFrom:to: which would seem to be a
related method in that protocol.

I really don't want to learn 10 different ways to use #at:, I'd rather
see 10 similarly named methods each doing it one way that look like
they're all variations of a theme.  When someone comes up with a new
idea like #copyFrom:fromEnd:, it's easier to add that than it is to go
back and patch #at: to do something new, it allows the system to grow
over time yet remain stable for existing code.

--
Ramon Leon
http://onsmalltalk.com

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

Stéphane Ducasse

I agree :)

> better than using negative indices.  Not sure I like #from:toFromEnd: as the selector name, but I like the spirit of it, it makes it explicit. I'd probably like #copyFrom:fromEnd: better, but that's just personal preference as it goes better with #copyFrom:to: which would seem to be a related method in that protocol.

me too.
Now I could sacrifice that for polymorphism sake :)
I think that polynorphic api are really cool and powerful.

> I really don't want to learn 10 different ways to use #at:, I'd rather see 10 similarly named methods each doing it one way that look like they're all variations of a theme.  When someone comes up with a new idea like #copyFrom:fromEnd:, it's easier to add that than it is to go back and patch #at: to do something new, it allows the system to grow over time yet remain stable for existing code.

Yes.


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: A more concise array access (using negative indices)

niko.schwarz
In reply to this post by Ramon Leon-5
On Mon, Jun 7, 2010 at 10:29 PM, Ramon Leon <[hidden email]> wrote:
>  Ruby
> seems to have a style where you can send just about anything to a method and
> it tries to figure out what to do based on the type of the arg, they like
> magic and the smaller API; Smalltalk tends to just have a dozen different
> but similarly named methods that let you accomplish the same thing.

I like that analysis.

>  so yes I like...
>
> ('hello' from: 2 toFromEnd: 2) should = 'ell'

:)

> I really don't want to learn 10 different ways to use #at:, I'd rather see
> 10 similarly named methods each doing it one way that look like they're all
> variations of a theme.

That's a valid stand point to take. I guess, at some point you just
need to choose what your use cases are / what your audience is.

You can get all the positions of c that differ from a, in Matlab:

c(a!=c).

In smalltalk, this could be something like:

c at: (a differencesVectorWith: c).

Cheers,

Niko

--
http://scg.unibe.ch/staff/Schwarz
twitter.com/nes1983
Tel: +41 076 235 8683

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project