Rewriting the "rewriting a script into a callable method" method

Comments

Hi Randall
why not using a single loop:

(String new: desiredLength) collect: [:each | sourceCharacterSet atRandom]

(String new: desiredLength) collect: [:each | sourceCharacterSet atRandom]

That seems a bit wasteful to me, since the source string is being used only for its type and length. Although, you could argue the same about my intermediate OrderedCollection. Hmm. :)
Oh yes, what a waste of space!
And it can confuse the newbie (will the String be modified in place?).
Take a look at: Matrix class>>#new:tabulate:

Second argument is a block (take two arguments, row and column indices).
We could have this selector in SequenceableCollection class too (with a single index).

#(1 4 9 16) is for example (Array new: 4 tabulate: [:index | index squared]).

And here:

String new: desiredLength tabulate: [:index | sourceCharacterSet atRandom].

Of course you would still prefer a zero argument block... What you eventually can test in #new:tabulate:

Ah, SequenceableCollection allSelectors size -> 749, too much is never enough, we always want to add some new!
Hmm tabulate is for a table (presumably row and column)

Better call it
(String new: desiredLength generator: [sourceCharacterSet atRandom])
[참 좋습니다]

Speaking of misleading :each of #collect: , I have made #timesCollect: and used quite a lot.


Your last code will look like this if you use it.


(desiredLength timesCollect: [ sourceCharacterSet atRandom]) as: String

[this is good]
[this is good]
How about
(1 to: desiredLength) inject: (String new: desiredLength) into: [:s :i | s at: i put: sourceCharacterSet atRandom ; yourself].

Post a comment

Already a Vox member? Sign in

Randal Schwartz

About Me

Randal Schwartz
United States
Just another [insert technology here] hacker!
Jaiku:
merlyn

My Groups

Neighborhood

Explore friends, family, friends & family, or entire neighborhood.

Archives