[Newbies] understanding selectors

Jerome Peace peace_the_dreamer at yahoo.com
Sun Jul 31 23:32:55 UTC 2011


Hi Jonathan,

Squeak makes learning really easy.

If you want to go deeper. It would seem to me that writing a boolean selector for 
#hasSameDimemsionsAs: aMatrix would be overall useful. It would return true when the argument is a matrix with the same row and column count.

Also once you have established that then for linear operations on the matrix you could make use of the matrix contents.

newContents := 
self contents with: aMatrix contents collect: [:each :eachOther | each perform: operation  with: other.

Then
self contents: newContents.

Look around at Matrix and the hierarchy it is a part of there are probably things to do stuff like this already. Matrix is a relatively recent addition to squeak so maybe there are pieces missing but usually the common stuff someone has already invented. 

If you are really adventurous I would suggest finding out how the protocol browser works. It is great for finding out ALL of the selectors that work with a particular class not just the ones defined in the class itself.

Course if your satisfied with how you've got it working now you can put this off in favor of getting on with the application you are writing.

Hth, 

Yours in curiosity and service, --Jerome Peace

--- On Sat, 7/30/11, Jonathan Wright <j.c at jondw.com> wrote:

> From: Jonathan Wright <j.c at jondw.com>
> Subject: Re: [Newbies] understanding selectors
> To: Beginners at lists.squeakfoundation.org
> Date: Saturday, July 30, 2011, 11:50 AM
> On Sat, 30 Jul 2011 10:41:52 -0500
> Jonathan Wright <j.c at jondw.com>
> wrote:
> 
> > On Sat, 30 Jul 2011 09:34:02 -0500
> > Jonathan Wright <j.c at jondw.com>
> wrote:
> > 
> > > On Sat, 30 Jul 2011 08:52:53 -0500
> > > Jonathan Wright <j.c at jondw.com>
> wrote:
> > > 
> > > > 
> > > > Hello,
> > > > 
> > > > I'm trying to extend the Matrix class by
> making it able to add and
> > > > subtract matrices.  So far I've
> implemented the following code to
> > > > add:
> > > > 
> > > > + aMatrix
> > > >     | newMatrix rowCount
> columnCount sum |
> > > >     rowCount := self
> rowCount.
> > > >     columnCount := self
> columnCount.
> > > >     newMatrix := Matrix rows:
> rowCount columns: columnCount.
> > > >     
> > > >     1 to: rowCount do: [
> :selectRow | 
> > > >         1 to:
> columnCount do: [ :selectColumn |
> > > >         sum :=
> (self at: selectRow at: selectColumn) + 
> > > >        
>            
> (aMatrix at: selectRow at:
> > > >   selectColumn). newMatrix
> at: selectRow at: selectColumn put:
> > > > sum.]]. ^newMatrix.
> > > > 
> > > > Now I want to implement a method for
> subtracting matrices.
> > > > However, I'd like to use the same
> code.  I tried to implement an
> > > > operand selector, however, it errors out.
> > > > 
> > > > Something like this:
> > > > 
> > > > operand: operand matrix: aMatrix
> > > >     | newMatrix rowCount
> columnCount sum |
> > > >     rowCount := self
> rowCount.
> > > >     columnCount := self
> columnCount.
> > > >     newMatrix := Matrix rows:
> rowCount columns: columnCount.
> > > >     
> > > >     1 to: rowCount do: [
> :selectRow | 
> > > >         1 to:
> columnCount do: [ :selectColumn |
> > > >         sum :=
> (self at: selectRow at: selectColumn)
> > > > operand (aMatrix at: selectRow at:
> > > >   selectColumn). newMatrix
> at: selectRow at: selectColumn put:
> > > > sum.]]. ^newMatrix.
> > > > 
> > > > I know this is not SmallTalk convention, but
> how should I pursue
> > > > something like this?
> > > > 
> > > > Thank You,
> > > > Jonathan
> > > >
> _______________________________________________
> > > > Beginners mailing list
> > > > Beginners at lists.squeakfoundation.org
> > > > http://lists.squeakfoundation.org/mailman/listinfo/beginners
> > > > 
> > > 
> > > I answered my own question.  How I love
> being able to look at all
> > > the source code in this handy Browser in Squeak!
> > > 
> > > I implemented the following:
> > > 
> > > perform: anOpperator with: aMatrix
> > >     | newMatrix rowCount
> columnCount result |
> > >     rowCount := self rowCount.
> > >     columnCount := self
> columnCount.
> > >     newMatrix := Matrix rows:
> rowCount columns: columnCount.
> > >     
> > >     1 to: rowCount do: [
> :selectRow | 
> > >         1 to:
> columnCount do: [ :selectColumn |
> > >         result :=
> (self at: selectRow at: selectColumn)
> > > perform: anOpperator with: (aMatrix at: selectRow
> at: selectColumn).
> > >         newMatrix
> at: selectRow at: selectColumn put:
> > > result.]]. ^newMatrix.
> > > 
> > > 
> > > where the subtraction/addition looks like this:
> > > subtractMatrix := matrixOne perform: #- with:
> matrixTwo.
> > > _______________________________________________
> > > Beginners mailing list
> > > Beginners at lists.squeakfoundation.org
> > > http://lists.squeakfoundation.org/mailman/listinfo/beginners
> > > 
> > 
> > Something I don't understand about this however. 
> Why does the
> > "result := (self at: selectRow at: selectColumn)
> perform: anOpperator
> > with: (aMatrix at: selectRow at: selectColumn)"
> > statement use the Object class and not the Matrix
> class I extended?
> > 
> 
> Oh because "(self at: selectRow at: selectColumn)" returns
> an Integer
> not a Matrix, duh.
> 
> Sorry for so many comments.
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
> 


More information about the Beginners mailing list