assorted beginner questions

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

assorted beginner questions

Unconditional
This is a mixed bag. These are the questions that I
would ask an
informed friend. They might be obvious, but they are
not obvious
to me (yet, I hope).
-----------------------------
As I read code in Squeak, I discover method names that
I do not
know. How do you find these methods if you don't know
the type of
the receiver? For example,

    self subclassResponsibility.
-----------------------------
In the floor method from Number,

    truncation _ self truncated.

What does the underscore mean?
-----------------------------
I want to apply smalltalk to fibonacci numbers. I add
this method
to Integer,

fib:
    (self = 0) ifTrue: (^0)
    (self = 1) ifTrue: (^1)
    ^ (self - 1 fib) + (self - 2 fib).

Next, I would like to memoize this method, (because of
the
enormous performance gains).  I do not see how to
memo-ize things
in smalltalk. Can somebody help me see the necessary
shift in
thinking?

and

Would a smalltalker even take this approach?
-----------------------------

I am still waiting for things to >click< w/rt
smalltalk.  I know
that I don't know and that's all that I know :)

Thanks to all the people on this list for letting this
list be a
place where you can ask questions in this fashion w/o
the
certainty of abuse and dismissal.

Mark.


      ____________________________________________________________________________________
Don't let your dream ride pass you by. Make it a reality with Yahoo! Autos.
http://autos.yahoo.com/index.html
 


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

Re: assorted beginner questions

Blake-5
On Tue, 02 Oct 2007 20:22:01 -0700, Mark Smithfield  
<[hidden email]> wrote:

> In the floor method from Number,
>
>     truncation _ self truncated.
>
> What does the underscore mean?

:=

In some images it shows up as a left-pointing arrow.

I'm not sure if that's coming back in later iterations or not.

> I want to apply smalltalk to fibonacci numbers. I add
> this method
> to Integer,
>
> fib:
>     (self = 0) ifTrue: (^0)
>     (self = 1) ifTrue: (^1)
>     ^ (self - 1 fib) + (self - 2 fib).
>
> Next, I would like to memoize this method, (because of
> the enormous performance gains).  I do not see how to
> memo-ize things in smalltalk. Can somebody help me see the necessary
> shift in thinking?

I don't see why memoization would be different in Smalltalk. Or why it  
would specifically have to be, rather. You might create a Fibonacci class  
that contained an array or somesuch and cached the numbers that had  
already been called. (Slowly taking up more and more space over time.)
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: assorted beginner questions

Ron Teitelbaum
In reply to this post by Unconditional
Hi Mark,

Welcome to the list, and Smalltalk.

> From: Mark Smithfield
>
> This is a mixed bag. These are the questions that I
> would ask an
> informed friend. They might be obvious, but they are
> not obvious
> to me (yet, I hope).
> -----------------------------
> As I read code in Squeak, I discover method names that
> I do not
> know. How do you find these methods if you don't know
> the type of
> the receiver? For example,
>
>     self subclassResponsibility.

This is an error that a programmer uses to call attention to something that
another developer should have done.  In Smalltalk we have something called
inheritance.  This basically means that if you subclass an object the
subclass can be considered to have, or implement, all the methods of its
parent object.  There are times when you want the subclass to provide its
own content in a method but you want to make sure that all subclasses
provide a valid answer to the message.  In that case you implement the
method and have it call self subclassResponsibility.  Which would raise and
error that says, your class should have overridden this method, or something
like that.  This tells you that you made a mistake and should implement a
method with the same name.

For example:

A superclass named: StoreProduct
        Might implement a  
        StoreProduct class >> isTaxible
                "return a Boolean describing if product requires collecting
State Sales tax"
                ^self subclassResponsiblity

Then a subclass named: StoreProductFood
        Would override this method with
        StoreProductFood class >> isTaxible
                "return a Boolean describing if product requires collecting
State Sales tax"
                ^false

Another subclass named: StoreProductNonFood
        Would override this method with
        StoreProductFood class >> isTaxible
                "return a Boolean describing if product requires collecting
State Sales tax"
                ^self manufacturer hasStoreLocationIn: Store
currentLocationState.

Or something like that.

> -----------------------------
> In the floor method from Number,
>
>     truncation _ self truncated.
>
> What does the underscore mean?

 It is an assignment operator, it is much better to use :=

> -----------------------------
> I want to apply smalltalk to fibonacci numbers. I add
> this method
> to Integer,
>
> fib:
>     (self = 0) ifTrue: (^0)
>     (self = 1) ifTrue: (^1)
>     ^ (self - 1 fib) + (self - 2 fib).
>
> Next, I would like to memoize this method, (because of
> the
> enormous performance gains).  I do not see how to
> memo-ize things
> in smalltalk. Can somebody help me see the necessary
> shift in
> thinking?

You need to create the method.  

The method can not have a : after it unless you are providing a argument to
your method

So you would leave that out, then find the template by selecting the
protocol in your integer browser (mathematical functions), add the following
and accept it.  This will save the method in the class Integer.

Integer >> fib
        "return the Fibonacci sum for the receiver"
        self > 0 ifFalse: [^0].
        self = 1 ifTrue: [^1].
        ^(self - 1) fib + (self - 2) fib

Then you can collect the sequence by doing something like:

        (1 to: 10) collect: [:i | i fib].

Highlight this and print it and get:

        #(0 1 1 2 3 5 8 13 21 34 55)

>
> and
>
> Would a smalltalker even take this approach?

Sure, it makes sense.

> -----------------------------
>
> I am still waiting for things to >click< w/rt
> smalltalk.  I know
> that I don't know and that's all that I know :)
>
> Thanks to all the people on this list for letting this
> list be a
> place where you can ask questions in this fashion w/o
> the
> certainty of abuse and dismissal.
>
> Mark.
>
>

You are welcome.  

Ron Teitelbaum
President / Principal Software Engineer
US Medical Record Specialists
www.USMedRec.com

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

Re: assorted beginner questions

cbc
In reply to this post by Unconditional
Hi.

On 10/2/07, Mark Smithfield <[hidden email]> wrote:
As I read code in Squeak, I discover method names that
I do not
know. How do you find these methods if you don't know
the type of
the receiver?
 
If you are asking how to find out what a method does from the code, the easiest way is to place the cursor over the method (just a part of it) and press alt+m (on windows, or maybe ctl+m, or on a Mac, cmd+m).  This should bring up the implementors of the method, and you can browse through all of the implementors and look at the code.  If that still doesn't make sense, you can ask about the method here and others will nicely explain it to you (like Ron did).
 
>> In the floor method from Number,
>>
>>     truncation _ self truncated.
> >
>> What does the underscore mean?
>
>:=
>In some images it shows up as a left-pointing arrow.
>I'm not sure if that's coming back in later iterations or not.
 
The underscore is an assignment.  If you have picked the right font, you won't see underscores in the code, but rather a left-pointing arrow.  I believe these fonts are still in the current versions of Squeak - but they just arent the default in some (most?) of the distributions.  So, if you change the font (or modify the font yourself - I haven't done that, but it is doable), then you can currently get back the left-pointing arrow.
 
-Chris
 

 

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners