Teaching Smalltalk

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

Re: Teaching Smalltalk

laurent laffont
Cool. I've just understood the 100 doors problem looking at your code. I was too lazy to try to understand the original one on http://programming.dojo.net.nz/languages/smalltalk/index :)

Laurent Laffont


On Tue, May 4, 2010 at 5:55 PM, John McKeon <[hidden email]> wrote:
Looking at rosettacode most languages look nearly identical (or, at the very least, ugly/crappy) when written out procedurely so that one might end up deciding to just stick with C.

I know I am going off the deep end a bit, but the whole answer to "why Smalltalk?" lies in the fact that problems should/would be approached in a completely different manner. As I am sure they would in most of the other languages if one were not restricted to 20 or so lines of code.

Since it is example code, at least in the case of Smalltalk, an object oriented solution would be in order - i.e. show how it would be solved using "everything is an object". Unfortunately, it doesn't really fit into "a few lines of code" to display on a page (even though the classes/objects and code required to implement them is little more than a few lines). And you also don't get any feel for the great tools. Perhaps, some insight into how one would run the code in Java/Eclipse, or Visual C <chuckle> with all the includes and project setup, etc might be a useful addition to the comparisons...

Object subclass: #Corridor
    instanceVariableNames: 'doors count'
    classVariableNames: ''
    poolDictionaries: ''
    category: '100Doors'

initialize: anInteger
    "initialize the receiver with the given number of doors"
    count := anInteger.
    doors := OrderedCollection new.
    anInteger timesRepeat: [ doors add: Door new ]

pass
    "iterate over the doors"
    1 to: count do: [ :i | self passBy: i ]

passBy: anInteger
    "if the nth door is open close it otherwise open it"
    doors by: anInteger do: [ :door | door toggle ]

printOn: aStream
    "print  the open doors"
    aStream cr.
    doors withIndexDo: [ :door :i | door isOpen ifTrue: [ aStream nextPutAll: i asString, ' is open'; cr ]]

Corridor class

pass: anInteger
    "return a new Corridor with the given number of doors that has been passed thru"
    ^self new
        initialize: anInteger;
        pass


==================================================================

Object subclass: #Door
    instanceVariableNames: 'isOpen'
    classVariableNames: ''
    poolDictionaries: ''
    category: '100Doors'

isOpen
    "Answer the value of isOpen"
    ^ isOpen

toggle
    "if the receiver is open close it else open it"
    isOpen := isOpen not

initialize
    "initialize the receiver to be closed"
    super initialize.
    isOpen := false

===========================================================
Patch to iterate over a collection by each nth item

OrderedCollection>>by: anInteger do: aBlock
    | index |
    index := anInteger.
    [index <= lastIndex]
        whileTrue:
            [aBlock value: (array at: index).
            index := index + anInteger]


Probably silly for the problem given but just my 2 cents
John


--
http://john-mckeon.us/seaside

_______________________________________________
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: Teaching Smalltalk

Hannes Hirzel
Yes, the documentation value of the OO solution is much higher
(assuming that you are aware of the concepts and know Smalltalk in
this case). It is actually a simple simulation.

--Hannes

On 5/4/10, laurent laffont <[hidden email]> wrote:

> Cool. I've just understood the 100 doors problem looking at your code. I was
> too lazy to try to understand the original one on
> http://programming.dojo.net.nz/languages/smalltalk/index :)
>
> Laurent Laffont
>
>
> On Tue, May 4, 2010 at 5:55 PM, John McKeon <[hidden email]> wrote:
>
>> Looking at rosettacode <http://rosettacode.org/wiki/100_doors> most
>> languages look nearly identical (or, at the very least, ugly/crappy) when
>> written out procedurely so that one might end up deciding to just stick
>> with
>> C.
>>
>> I know I am going off the deep end a bit, but the whole answer to "why
>> Smalltalk?" lies in the fact that problems should/would be approached in a
>> completely different manner. As I am sure they would in most of the other
>> languages if one were not restricted to 20 or so lines of code.
>>
>> Since it is example code, at least in the case of Smalltalk, an object
>> oriented solution would be in order - i.e. show how it would be solved
>> using
>> "everything is an object". Unfortunately, it doesn't really fit into "a
>> few
>> lines of code" to display on a page (even though the classes/objects and
>> code required to implement them is little more than a few lines). And you
>> also don't get any feel for the great tools. Perhaps, some insight into
>> how
>> one would run the code in Java/Eclipse, or Visual C <chuckle> with all the
>> includes and project setup, etc might be a useful addition to the
>> comparisons...
>>
>> Object subclass: #Corridor
>>     instanceVariableNames: 'doors count'
>>     classVariableNames: ''
>>     poolDictionaries: ''
>>     category: '100Doors'
>>
>> initialize: anInteger
>>     "initialize the receiver with the given number of doors"
>>     count := anInteger.
>>     doors := OrderedCollection new.
>>     anInteger timesRepeat: [ doors add: Door new ]
>>
>> pass
>>     "iterate over the doors"
>>     1 to: count do: [ :i | self passBy: i ]
>>
>> passBy: anInteger
>>     "if the nth door is open close it otherwise open it"
>>     doors by: anInteger do: [ :door | door toggle ]
>>
>> printOn: aStream
>>     "print  the open doors"
>>     aStream cr.
>>     doors withIndexDo: [ :door :i | door isOpen ifTrue: [ aStream
>> nextPutAll: i asString, ' is open'; cr ]]
>>
>> Corridor class
>>
>> pass: anInteger
>>     "return a new Corridor with the given number of doors that has been
>> passed thru"
>>     ^self new
>>         initialize: anInteger;
>>         pass
>>
>>
>> ==================================================================
>>
>> Object subclass: #Door
>>     instanceVariableNames: 'isOpen'
>>     classVariableNames: ''
>>     poolDictionaries: ''
>>     category: '100Doors'
>>
>> isOpen
>>     "Answer the value of isOpen"
>>     ^ isOpen
>>
>> toggle
>>     "if the receiver is open close it else open it"
>>     isOpen := isOpen not
>>
>> initialize
>>     "initialize the receiver to be closed"
>>     super initialize.
>>     isOpen := false
>>
>> ===========================================================
>> Patch to iterate over a collection by each nth item
>>
>> OrderedCollection>>by: anInteger do: aBlock
>>     | index |
>>     index := anInteger.
>>     [index <= lastIndex]
>>         whileTrue:
>>             [aBlock value: (array at: index).
>>             index := index + anInteger]
>>
>>
>> Probably silly for the problem given but just my 2 cents
>> John
>>
>>
>> --
>> http://john-mckeon.us/seaside
>>
>> _______________________________________________
>> 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: Teaching Smalltalk

Samuel Williams
Hi,

Thanks for all your feedback.

While it isn't that great, an object oriented solution "similar" to the Java example is fine too:
        http://programming.dojo.net.nz/languages/java/index

It doesn't really rely on object oriented features, but it shows how a class is instantiated and a method is called.

This could be useful in the Smalltalk example.

Kind regards,
Samuel

On 5/05/2010, at 4:58 AM, Hannes Hirzel wrote:

> Yes, the documentation value of the OO solution is much higher
> (assuming that you are aware of the concepts and know Smalltalk in
> this case). It is actually a simple simulation.
>
> --Hannes
>
> On 5/4/10, laurent laffont <[hidden email]> wrote:
>> Cool. I've just understood the 100 doors problem looking at your code. I was
>> too lazy to try to understand the original one on
>> http://programming.dojo.net.nz/languages/smalltalk/index :)
>>
>> Laurent Laffont
>>
>>
>> On Tue, May 4, 2010 at 5:55 PM, John McKeon <[hidden email]> wrote:
>>
>>> Looking at rosettacode <http://rosettacode.org/wiki/100_doors> most
>>> languages look nearly identical (or, at the very least, ugly/crappy) when
>>> written out procedurely so that one might end up deciding to just stick
>>> with
>>> C.
>>>
>>> I know I am going off the deep end a bit, but the whole answer to "why
>>> Smalltalk?" lies in the fact that problems should/would be approached in a
>>> completely different manner. As I am sure they would in most of the other
>>> languages if one were not restricted to 20 or so lines of code.
>>>
>>> Since it is example code, at least in the case of Smalltalk, an object
>>> oriented solution would be in order - i.e. show how it would be solved
>>> using
>>> "everything is an object". Unfortunately, it doesn't really fit into "a
>>> few
>>> lines of code" to display on a page (even though the classes/objects and
>>> code required to implement them is little more than a few lines). And you
>>> also don't get any feel for the great tools. Perhaps, some insight into
>>> how
>>> one would run the code in Java/Eclipse, or Visual C <chuckle> with all the
>>> includes and project setup, etc might be a useful addition to the
>>> comparisons...
>>>
>>> Object subclass: #Corridor
>>>    instanceVariableNames: 'doors count'
>>>    classVariableNames: ''
>>>    poolDictionaries: ''
>>>    category: '100Doors'
>>>
>>> initialize: anInteger
>>>    "initialize the receiver with the given number of doors"
>>>    count := anInteger.
>>>    doors := OrderedCollection new.
>>>    anInteger timesRepeat: [ doors add: Door new ]
>>>
>>> pass
>>>    "iterate over the doors"
>>>    1 to: count do: [ :i | self passBy: i ]
>>>
>>> passBy: anInteger
>>>    "if the nth door is open close it otherwise open it"
>>>    doors by: anInteger do: [ :door | door toggle ]
>>>
>>> printOn: aStream
>>>    "print  the open doors"
>>>    aStream cr.
>>>    doors withIndexDo: [ :door :i | door isOpen ifTrue: [ aStream
>>> nextPutAll: i asString, ' is open'; cr ]]
>>>
>>> Corridor class
>>>
>>> pass: anInteger
>>>    "return a new Corridor with the given number of doors that has been
>>> passed thru"
>>>    ^self new
>>>        initialize: anInteger;
>>>        pass
>>>
>>>
>>> ==================================================================
>>>
>>> Object subclass: #Door
>>>    instanceVariableNames: 'isOpen'
>>>    classVariableNames: ''
>>>    poolDictionaries: ''
>>>    category: '100Doors'
>>>
>>> isOpen
>>>    "Answer the value of isOpen"
>>>    ^ isOpen
>>>
>>> toggle
>>>    "if the receiver is open close it else open it"
>>>    isOpen := isOpen not
>>>
>>> initialize
>>>    "initialize the receiver to be closed"
>>>    super initialize.
>>>    isOpen := false
>>>
>>> ===========================================================
>>> Patch to iterate over a collection by each nth item
>>>
>>> OrderedCollection>>by: anInteger do: aBlock
>>>    | index |
>>>    index := anInteger.
>>>    [index <= lastIndex]
>>>        whileTrue:
>>>            [aBlock value: (array at: index).
>>>            index := index + anInteger]
>>>
>>>
>>> Probably silly for the problem given but just my 2 cents
>>> John
>>>
>>>
>>> --
>>> http://john-mckeon.us/seaside
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [hidden email]
>>> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>>>
>>>
>>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

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