[Newbies] Teaching Smalltalk
Hannes Hirzel
hannes.hirzel at gmail.com
Tue May 4 16:05:29 UTC 2010
No I do not think it is silly.
It is a simple example and illustrates the OO paradigm.
This would be a nice case for a video. That brings out the usefulness
of the tools.
--Hannes
On 5/4/10, John McKeon <p3anoman at gmail.com> 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
>
More information about the Beginners
mailing list