[Pharo-project] A more concise array access (using negative indices)
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
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 Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
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 Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
On Mon, Jun 7, 2010 at 3:23 PM, Stéphane Ducasse <stephane.ducasse@inria.fr> 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
On Jun 7, 2010, at 3:36 PM, Niko Schwarz wrote:
On Mon, Jun 7, 2010 at 3:23 PM, Stéphane Ducasse <stephane.ducasse@inria.fr> 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.
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 Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
On Mon, Jun 7, 2010 at 15:23, Stéphane Ducasse <stephane.ducasse@inria.fr> 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
On Jun 7, 2010, at 3:38 PM, Damien Pollet wrote:
On Mon, Jun 7, 2010 at 15:23, Stéphane Ducasse <stephane.ducasse@inria.fr> 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 Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
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
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
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 <ramon.leon@allresnet.com> 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 Pharo-project@lists.gforge.inria.fr 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
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
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.
On Mon, Jun 7, 2010 at 10:29 PM, Ramon Leon <ramon.leon@allresnet.com> 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
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 Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
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 <hilaire.fernandes@gmail.com> 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 Pharo-project@lists.gforge.inria.fr 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
participants (7)
-
Adrian Lienhard -
Damien Pollet -
Hilaire Fernandes -
Lukas Renggli -
Niko Schwarz -
Ramon Leon -
Stéphane Ducasse