understanding selectors

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

understanding selectors

Jonathan Wright-2

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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: understanding selectors

Jonathan Wright-2
On Sat, 30 Jul 2011 08:52:53 -0500
Jonathan Wright <[hidden email]> 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
> [hidden email]
> 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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: understanding selectors

Jonathan Wright-2
On Sat, 30 Jul 2011 09:34:02 -0500
Jonathan Wright <[hidden email]> wrote:

> On Sat, 30 Jul 2011 08:52:53 -0500
> Jonathan Wright <[hidden email]> 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
> > [hidden email]
> > 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
> [hidden email]
> 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?

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: understanding selectors

Jonathan Wright-2
On Sat, 30 Jul 2011 10:41:52 -0500
Jonathan Wright <[hidden email]> wrote:

> On Sat, 30 Jul 2011 09:34:02 -0500
> Jonathan Wright <[hidden email]> wrote:
>
> > On Sat, 30 Jul 2011 08:52:53 -0500
> > Jonathan Wright <[hidden email]> 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
> > > [hidden email]
> > > 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
> > [hidden email]
> > 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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: understanding selectors

Jerome Peace
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 <[hidden email]> wrote:

> From: Jonathan Wright <[hidden email]>
> Subject: Re: [Newbies] understanding selectors
> To: [hidden email]
> Date: Saturday, July 30, 2011, 11:50 AM
> On Sat, 30 Jul 2011 10:41:52 -0500
> Jonathan Wright <[hidden email]>
> wrote:
>
> > On Sat, 30 Jul 2011 09:34:02 -0500
> > Jonathan Wright <[hidden email]>
> wrote:
> >
> > > On Sat, 30 Jul 2011 08:52:53 -0500
> > > Jonathan Wright <[hidden email]>
> 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
> > > > [hidden email]
> > > > 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
> > > [hidden email]
> > > 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
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: understanding selectors

Levente Uzonyi-2
In reply to this post by Jonathan Wright-2
Hi Jonathan,

On Sat, 30 Jul 2011, Jonathan Wright wrote:

snip

> 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.

It's not such a good idea, to override #perform:with:, because your Matrix
(and all other Matrix instances) will behave differently.

But you can use #with:collect: to achieve what you'd like to. A few
examples:

In methods:

+ aMatrix

  ^self with: aMatrix collect: [ :each :other | each + other ]

- aMatrix

  ^self with: aMatrix collect: [ :each :other | each - other ]

Directly:

m1 with: m2 collect: [ :each :other | each + other ]

m1 with: m2 collect: [ :each :other | each - other ]

Or the tricky way: (which relies on Symbol >> #value:value:)

m1 with: m2 collect: #+

m1 with: m2 collect: #-


Levente

>
>
> where the subtraction/addition looks like this:
> subtractMatrix := matrixOne perform: #- with: matrixTwo.
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners