[squeak-dev] Smalltalk idiom

Jerome Peace peace_the_dreamer at yahoo.com
Thu May 21 00:11:31 UTC 2009


Hi Ramiro,


>Ramiro Diaz Trepat ramiro at diaztrepat.name
>Wed May 20 08:01:11 UTC 2009 asks:
>
>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?

In a workspace evaluate:

picker := Random new .

(Matrix rows: 2 columns: 3 ) collect: [ :each | picker next ] .

result:

 a Matrix
(0.2668126189461036 0.319686627164337 0.973142751014392
 0.610216298890401 0.905335450966533 0.97292439452043)

You may wish to study the Matrix class and see what else it can do.
The class is a recent contribution by someone who had a need for it.

Hth,

Yours in curiosity and service, --Jerome Peace


      



More information about the Squeak-dev mailing list