IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Some iterators do provide indexes
Here's my cut at it

| sequence ranges buckets |
sequence := Random seed: 5. "yes this is lame - pick a better seed"
ranges := #( 0.0 0.1 0.4 0.8 ).
buckets := Array new: (ranges size).
buckets atAllPut: 0.
500 timesRepeat: [ "or however many times you want"
\t| n index |
\tn := sequence next.
\tindex := ranges findLast: [:value | n >= value].
\tbuckets at: index put: ((buckets at: index) + 1)].

"This just prints out the results and could likely be done better with a stream"
ranges := ranges copyWith: 1.0. "stick a 1.0 on the end for completeness"
1 to: (ranges size-1) do: [:i |
\t| lo hi |
\tlo := ranges at: i.
\thi := ranges at: i+1.
\tTranscript show: ((lo asString), ' - ', (hi asString), ' : ', ((buckets at: i) asString)); cr]

There's also an iterator - pairsDo: which is handy for this sort of thing. The ranges array would have to look like

ranges := #(
0.0 0.1
0.1 0.4
0.4 0.8
0.8 1.0
).

But you'll still need to run your own counter to index into the buckets array.

ie:

index := 1.
ranges pairsDo: [:lo :hi |
(n isBetween: lo and: hi)
ifTrue: [buckets at: index put: (buckets at: index) + 1]
ifFalse: [index := index +1]]

The problem with this is you don't get a loop exit once you've found what you're looking for. So I use findLast which searches until found.




"I believe that many of the systems we build today in Java would be better built in Smalltalk and Gemstone."

     -- Martin Fowler, JAOO 2003
New Yay!
Exactly what I hoped to see.

I knew Smalltalk would have something nice, even without a bucket class. Thanks.
--

"There's nothing more nervous than a million dollars. It does not speak French, it does not speak English, it does not speak German and it moves very fast."

-- Jean Chretien
New Why worry about that factor of 2?
The problem with this is you don't get a loop exit once you've found what you're looking for. So I use findLast which searches until found.

Sure, on average you will cut your search time by a factor of 2. But if you have enough buckets to care, then you really should use a binary search strategy instead.

So either I wouldn't care, or I would care and do a lot better.

So why worry about the factor of 2?

Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
New Habit, I suppose
From working on powerflow simulators that ran simulations over long periods of time. A factor of 2 might be the difference between getting an answer Friday or late next week.

:-P

But I do see your point on the BSearch. I don't know if there's a handy one built in though. Don't have squeak handy on the work machine. I'll look later.



"I believe that many of the systems we build today in Java would be better built in Smalltalk and Gemstone."

     -- Martin Fowler, JAOO 2003
New It isn't just you though
I've seen a lot of people from Smalltalk environments who are perfectly happy to use an array with a detect method where something like a hash would make a lot more sense.

I have been left to wonder if they got in the habit of abstracting so far that they forgot the algorithm implications of their choices?

Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
New I know what you mean
But you can't really just say something like SortedCollection should implement #detect: as binary search. Its a general purpose method and the aspect you are searching on may not have anything at all to do with the sort ordering.

I've found some find: methods that return the index. But again, because of the nature of the evaluation block, they do a linear search. I've spent a few minutes looking for a #binarySearch: or similar and haven't found one. Of course, you can add one without much effort.

Smalltalk culture is pretty much about efficiency of coding - with efficiency of implementation being seldom considered until its crucial.

Actually, there appear to be 2 cults - one dedicated to elegant simplicity of coding - which is nearly everybody - and the other dedicated to really clever (and transparent) optimizations - like the people working on croquet that are transparently offloading graphics operations to hardware as much as possible - or the people who work on the VMs.



"I believe that many of the systems we build today in Java would be better built in Smalltalk and Gemstone."

     -- Martin Fowler, JAOO 2003
     Smalltalk question - (Arkadiy) - (19)
         What's the unsolved part? -NT - (tuberculosis) - (1)
             The original problem - (Arkadiy)
         Why not solve it like you would in any language? - (ben_tilly) - (16)
             That is the problem I solved - (Arkadiy) - (15)
                 2 ways - (Simon_Jester) - (11)
                     Re: 2 ways - (Arkadiy) - (10)
                         The iterator doesn't change the algorithm (with assumption) - (Simon_Jester) - (3)
                             Smalltalk iterators are different - (Arkadiy) - (2)
                                 Okay...so your 'cheat' is the method - (Simon_Jester) - (1)
                                     see my answer to Ben below... -NT - (Arkadiy)
                         Some iterators do provide indexes - (tuberculosis) - (5)
                             Yay! - (Arkadiy)
                             Why worry about that factor of 2? - (ben_tilly) - (3)
                                 Habit, I suppose - (tuberculosis) - (2)
                                     It isn't just you though - (ben_tilly) - (1)
                                         I know what you mean - (tuberculosis)
                 Here is my second strategy in Ruby - (ben_tilly) - (2)
                     I realized that introducing a bucket class makes it easy - (Arkadiy) - (1)
                         Elegance is in the eye of the beholder - (ben_tilly)

I know members of the legislature who would disagree with you under oath.
149 ms