Smalltalk

Un article de MonWiki.

"My move to Smalltalk wasn’t motivated by any particular language feature, but by what I saw as a generally richer environment and set of libraries and a deeper, more experienced community."
"There’s a wealth of knowledge and experience in the communities around “dead” languages like Smalltalk, Lisp, and Forth, and it always saddens me to see so many mainstream developers taking years to make mistakes and discoveries they could have read about in an afternoon of browsing old papers or a few mailing list archives."
-Avi Bryant, creator of Seaside.

Voir aussi : Seaside, Lisp.

Sommaire

Bouts de Code

Factory

Utilisation du message subclassResponsibility.

Object subclass: #Application

    clientMethod
        document := self documentClass new.
   
    documentClass
        self subclassResponsibility

Application subclass: #MyApplication

    documentClass
        ^ MyDocument

Proxy Pattern

Utilisation du message doesNotUnderstand.

on: anObject
   ^super new target: anObject

doesNotUnderstand: aMessage
   ^target
      perform: aMessage selector 
      withArguments: aMessage arguments

| wrapper |
wrapper := Proxy on: Transcript.
wrapper open.
wrapper show: 'Hi mom'.

Combinaisons

Voir aussi : Lua#Combinaisons

Exemple (Squeak) :

Object subclass: #Combi
    instanceVariableNames: 'set depth current'

Combi>>combine: aSet withDepth: aDepth
    depth := aDepth.
    set := aSet.
    current := Array new: aDepth.
    self startRecursion.

Combi>>startRecursion
   self recurseAtLevel: 1.

Combi>>recurseAtLevel: i
    depth = i
        ifTrue:
            [self endRecursion.
            ^nil].
    set do:
        [:currentElement | 
            current at: i put: currentElement.
            self recurseAtLevel: i+1].

Combi>>endRecursion
    set do:
        [:last |
            current at: depth put: last.
            current do:
                [:each |
                    Transcript show: each].
            Transcript cr].
Combi new combine: #( $x 7 '+' ) withDepth: 2.

xx
x7
x+
7x
77
7+
+x
+7
++

Permutations

Object subclass: #Permutation
    instanceVariableNames: 'set'

permute: aSet
    set := aSet.
    self recurseAtLevel: 1.

recurseAtLevel: level
    self print.
    level <= set size
        ifTrue:
            [set size-1 to: level by: -1 do:
                [:i |
                    i+1 to: set size do:
                        [:j |
                            set swap: i with: j.
                            self recurseAtLevel: i+1].
                    self rotateLeftStartingAt: i]].

rotateLeftStartingAt: start
    | tmp |
    tmp := set at: start.
    start to: set size-1 do: [:i | set at:i put: (set at: i+1)].
    set at: set size put: tmp.

print
    set do:
        [:each |
            Transcript show: each]
    separatedBy: [Transcript show: ' '].
    Transcript cr.
Permutation new permute: #( $x 7 '+').

x + 7
7 x +
7 + x
+ x 7
+ 7 x

Sauvegardes automatiques

Squeak.

À faire : ranger dans une classe, refactoriser, ajoûter un widget Morphic.

[[| newName |
    (Delay forMilliseconds: 1 * 60 * 1000) wait.
    newName _ 'SqueakAutoBackup'.
    newName isNil ifTrue: [^ self].
    Mutex new
        critical:
            [(SourceFiles at: 2) ifNotNil:
                [SmalltalkImage current closeSourceFiles; "so copying the changes file will always work"
                saveChangesInFileNamed: (SmalltalkImage current fullNameForChangesNamed: newName)].
            SmalltalkImage current saveImageInFileNamed: (SmalltalkImage current fullNameForImageNamed: newName)]]
repeat] fork.

Références


Warning: main() [function.main]: open_basedir restriction in effect. File(/mnt/145/sdb/9/8/sroccaserra/wiki/skins/common/images/icons/Images-functions.txt) is not within the allowed path(s): (/mnt/109/sdb/9/8/sroccaserra) in /mnt/109/sdb/9/8/sroccaserra/wiki/includes/OutputPage.php on line 2

Warning: main(/mnt/145/sdb/9/8/sroccaserra/wiki/skins/common/images/icons/Images-functions.txt) [function.main]: failed to open stream: Operation not permitted in /mnt/109/sdb/9/8/sroccaserra/wiki/includes/OutputPage.php on line 2

Warning: main() [function.include]: Failed opening '/mnt/145/sdb/9/8/sroccaserra/wiki/skins/common/images/icons/Images-functions.txt' for inclusion (include_path='/mnt/109/sdb/9/8/sroccaserra/include:.:/usr/php4/lib/php') in /mnt/109/sdb/9/8/sroccaserra/wiki/includes/OutputPage.php on line 2