Smalltalk idiom

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

Smalltalk idiom

Ramiro Diaz Trepat-2
I have a simple question regarding the use of a proper idiom.
I had to create a matrix (n X m) of random values and I used a line similar to this one:

NMatrix withRows: ((1 to: n) collect: [ :i | random next: m ]).

Since Smalltalk is so neat for its collection handling, I obviously did not like to write that line.
I thought that I did not want to explicitly create the interval, nor use a block that requires an
argument that I also don't use.
The question, finally, is if there an elegant way of replicating the behaviour of #collect: but 
without the argument?
In my case, I thought it would be great for Integer to have something like #timesCollect:
that would allow me to rewrite the line above as:

NMatrix withRows: (n timesCollect: [ random next: m ])

Does anyone else think that this would be an useful method to have?

Cheers


r.

_______________________________________________
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: Smalltalk idiom

Igor Stasenko
2009/5/20 Ramiro Diaz Trepat <[hidden email]>:
> I have a simple question regarding the use of a proper idiom.
> I had to create a matrix (n X m) of random values and I used a line similar
> to this one:
> NMatrix withRows: ((1 to: n) collect: [ :i | random next: m ]).

> Since Smalltalk is so neat for its collection handling, I obviously did not
> like to write that line.
> I thought that I did not want to explicitly create the interval, nor use a
> block that requires an
> argument that I also don't use.
> The question, finally, is if there an elegant way of replicating the
> behaviour of #collect: but
> without the argument?
> In my case, I thought it would be great for Integer to have something like
> #timesCollect:
> that would allow me to rewrite the line above as:
> NMatrix withRows: (n timesCollect: [ random next: m ])
> Does anyone else think that this would be an useful method to have?

depends on how you represent a matrix.
It seems that you using a collection of N rows.
IMO its more efficient to use a flat array, while you can declare the matrix as:

Array subclass: #NMatrix
instanceVariables: 'numRows'

then, an instance size \\ numRows - gives number of columns

and you can simply add/override some methods , while reuse most of
Collection protocol when you don't need to care about item row/col
position - like in regular flat Array.

Override a #species method, so NMatrix collect: will create NMatrix
when using #collect: and other related methods.
Then, a multiplication can be implemented as:

NMatrix >>* numberOrMatrix
  ^ numberOrMatrix isNumber ifTrue: [ self collect: [: each | each * number ] ]
  ifFalse: [ self mulMatrix: numberOrMatrix ]

as you can see, a #collect: method can be used for scalar multiplication.

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



--
Best regards,
Igor Stasenko AKA sig.

_______________________________________________
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: Smalltalk idiom

ramiro.diaz.trepat
Hi Igor,
Thank you very much for your thorough reply.
May be I did not express myself properly, but I am not really interested in discussing the implementation of a Matrix.  It was only an example.  
I'm interested in discussing wether or not people in the community find it neat and useful to have a method that behaves like #collect: but without arguments, just like #timesRepeat: for the iterations.
Sorry for the misunderstanding.
Cheers

r.




-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Igor Stasenko
Sent: 20 May 2009 09:40
To: [hidden email]
Subject: Re: [Pharo-project] Smalltalk idiom

2009/5/20 Ramiro Diaz Trepat <[hidden email]>:
> I have a simple question regarding the use of a proper idiom.
> I had to create a matrix (n X m) of random values and I used a line similar
> to this one:
> NMatrix withRows: ((1 to: n) collect: [ :i | random next: m ]).

> Since Smalltalk is so neat for its collection handling, I obviously did not
> like to write that line.
> I thought that I did not want to explicitly create the interval, nor use a
> block that requires an
> argument that I also don't use.
> The question, finally, is if there an elegant way of replicating the
> behaviour of #collect: but
> without the argument?
> In my case, I thought it would be great for Integer to have something like
> #timesCollect:
> that would allow me to rewrite the line above as:
> NMatrix withRows: (n timesCollect: [ random next: m ])
> Does anyone else think that this would be an useful method to have?

depends on how you represent a matrix.
It seems that you using a collection of N rows.
IMO its more efficient to use a flat array, while you can declare the matrix as:

Array subclass: #NMatrix
instanceVariables: 'numRows'

then, an instance size \\ numRows - gives number of columns

and you can simply add/override some methods , while reuse most of
Collection protocol when you don't need to care about item row/col
position - like in regular flat Array.

Override a #species method, so NMatrix collect: will create NMatrix
when using #collect: and other related methods.
Then, a multiplication can be implemented as:

NMatrix >>* numberOrMatrix
  ^ numberOrMatrix isNumber ifTrue: [ self collect: [: each | each * number ] ]
  ifFalse: [ self mulMatrix: numberOrMatrix ]

as you can see, a #collect: method can be used for scalar multiplication.

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



--
Best regards,
Igor Stasenko AKA sig.

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

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