links to this page:    
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
Code Snippets
Last updated at 9:00 pm UTC on 31 March 2017

A Great Squeak Demo

I have never failed to get an "Ah-ha" out of the factorial part of my Squeak demo. It's a bottom-up approach that seems to work well for people who are competent in some other programming language and wondering what Smalltalk is all about. Here's how it goes...

First I show the code for Integer factorial, and explain how it works in terms of sending messages, and run it for 6 factorial, which anyone can understand.

Then I run 100 factorial, which is always fun. We joke about whether it's correct or not, so I print

100 factorial // 99 factorial

(if you have Squeak plugin installed, try this).

and we joke again about how it probably is correct, after all. Moreover, it's clear by now that Squeak is doing serious arithmetic (i.e. division) on monster numbers, quickly and correctly.

THEN I go back to factorial and explain how the fact that the code is messages means that it will work for any kind of numbers, so long as they support those messages. That's how it continues to work when the results get to be too big for normal integers. If someone isn't getting it, then I show them max: in Magnitude and point out that this one tiny proceedure is constantly servicing Integers, Floats, LargeIntegers, Fractions, Times and Dates (show the hierarchy). This is the power of polymorphism.

Finally I talk about how Squeak defines a virtual machine in which this metaphor of message sending IS the actual instruction set. At that point I show the compiled bytecodes of factorial [that's why we bother to have show-bytcodes in a menu], responsible for the foregoing numeric pyrotechnics. It never fails to blow people's minds that you can have this kind of power encoded in 21 (count 'em) bytes. (max:, by the way, is 7 bytes). - Dan Ingalls (2 Jul 98)

From a post to the Squeak mailing list by Dan Ingalls 01 February 1999
        | strm |
        strm := WriteStream new on: (String new: 10).
        tempNames do: [:n | strm nextPutAll: n; space].
        tempStr := strm contents.


        tempStr := String streamContents:
            [:strm | tempNames do: [:n | strm nextPutAll: n; space]].

- submitted by John-Reed Maffeo

Is this the best page for powerful pieces of short, compact code? If so, here's one: Squeak has facilities for downloading arbitrary URLs, and for filing in from arbitrary streams. Putting it together, one can do things like:

'http://some.server/dir/file' asUrl retrieveContents contentStream fileIn

HTTPSocket httpFileInNewChangeSet: 'http://some.server/dir/file'

To extract the title of this page

 ((('http://wiki.squeak.org/squeak/5' asUrl retrieveContents contents) 
    splitBy: '<div style="font-weight: bold; font-size: x-large; padding-top: 1ex">') second 
    splitBy: '</div>'
 ) first

to get a list of Swiki page titles

 1 to:  50 do: [:pageNo | 
	Transcript show: '- ', pageNo printString, '. *',
	(((('http://wiki.squeak.org/squeak/', pageNo printString) asUrl retrieveContents contents) 
    splitBy: '<div style="font-weight: bold; font-size: x-large; padding-top: 1ex">') second 
    splitBy: '</div>'
 ) first, '*'; cr


Or what about this (found at the very bottom on the SARInstaller page,
no.3324 on this Swiki), slightly modified here :
SARInstaller installSAR: 'SomeSqueakArchive-1.sar'

See also
How to create new text fonts
Browse all implementors of a message selector
Göran's Code Snippets Of The Day
Mouse button tester