MethodFinder is so cool!

Tim Olson tim at jumpnet.com
Thu Jun 17 15:33:39 UTC 1999


There was a recent posting on rec.puzzles, asking how many different 
16-bit binary numbers had exactly 4 '1' bits.  I was playing around in 
Squeak at the time, so I thought I'd whip something up:

((0 to: (2 raisedTo: 16) - 1) select:
     [:n | ((n printStringBase: 2) <<<need method here>>> $1) = 4]) size

I was pretty sure that there already existed a method to count the number 
of a particular element in a collection (in this case, the character 1 in 
a string of characters), but couldn't remember what it was called.

Then I remembered the new "selector finder" panel.  It not only has the 
ability to type a fragment of a method name to find all the implementers, 
it also has this great feature: you type in some examples of what you 
want to do, and it will exhaustively search for any method which does 
that.  So I typed in:

	MethodFinder methodFor:
     #( ('1101' $1) 3 ('abcda' $a) 2).

which consists of 2 different examples of what I wanted:

     the string '1101' with the operand $1 (the Character 1) gives the 
result 3
     the string 'abcda' with the operand $a gives the result 2.

It immediately responded with:

     '(data1 occurrencesOf: data2) '

How cool is that!

So I did it, and:

((0 to: (2 raisedTo: 16) - 1) select:
     [:n | ((n printStringBase: 2) occurrencesOf: $1) = 4]) size

and got: 1820



     -- tim





More information about the Squeak-dev mailing list