How To Talk To Yourself When Reading Smalltalk

This is obvious.. just talk about the weather, or sports, or other general small issues... Pretend you're at a bar, make conversation. Make small talk. No, really..

For tonight's reading, we have selected the following small piece:

select: aBlock

| newCollection |
newCollection := self species new.
self do: [ :each | (aBlock value: each) ifTrue: [newCollection add: each]].
^ newCollection

I like the idea of talking to myself when reading any language, for example, reading i = i + 1 as "i gets i plus one". But, even though I made a brief sojourn into Smalltalk a few weeks ago, this page gives me no hints. (I did learn that I don't like the terminology "Answers a ..." found in the definition of each method (at least in Dolphin Smalltalk).) -- RandyKramer


   Method select takes one argument, aBlock. 
   One local object, newCollection. 
   newCollection gets self species (answers a class) new. 
   self do a one-argument block. 
The argument is each. 
aBlock value each (answers a boolean)if true block 
        newCollection add each. end block. 
end block. 
   answer newCollection.


I'm not sure I'm entirely convinced by the above. I think I would read this as :

This is how I select things, given a Block. To select things, I'll work on a newCollection, and return it at the end. The newCollection should be an instance of my species, i.e. idiomatically "my class or a close approximation thereof". For each thing I myself have, I will do the following - ask the Block I was given what its value is for that thing, and ask the value to do the following for me if it happens to be a true value : add the thing to the newCollection.


Here's another reading. --RonJeffries

I'd read much of it as written, actually:

select: aBlock

select colon aBlock (thinking, "It's a method named select taking a block.")

| newCollection |
temp newCollection

newCollection := self species new.
newCollection gets self species new. (thinking, "Species means 'class' except that collections convert to their species. this is a rather tricky bit that many smalltalkers never find out about.")

self do: [ :each | (aBlock value: each) ifTrue: [newCollection add: each]].
This one I don't read word for word. I see: "loop over the receiver (collection) and if the block value is true, add the element to newCollection".

^ newCollection
"Answer the new collection" (you get used to saying answer. it's just a word.)

Strictly I don't think those thoughts. I see that code and for most lines I just know what it means. I don't translate it to English to understand it, just as I used to not translate German to understand it, and as most of us don't translate our "native" programming language.


Ron notes at the end, "Strictly I don't think those thoughts." This made me ask: ShouldWeTalkToOurselvesWhenReadingCode?


Another way is this:

select: a1blockDiscriminator

| selectedElements |                                   " a temp "
selectedElements := self species new.                  " selectedElements is initially an empty copy of this collection. "
self do:                                               " do
            [ :eachElement |                                   "   eachElement suchThat
             (a1blockDiscriminator value: eachElement)         "   where the value of discriminator is true for said element "
                 ifTrue: [selectedElements add: eachElement]   "   we add said element to selectedElements "
            ].
^ selectedElements                                     " answer the selectedElements "

JimSawyer?

CategorySmalltalk


EditText of this page (last edited April 21, 2012) or FindPage with title or text search