0% found this document useful (0 votes)
346 views23 pages

Squeak - Smalltalk - Terse Guide To Squeak PDF

Uploaded by

NixonMR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
346 views23 pages

Squeak - Smalltalk - Terse Guide To Squeak PDF

Uploaded by

NixonMR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

Terse Guide to Squeak

Introduction
Allowable characters
a-z , A-Z , 0-9 , .+/\*~<>@%|&? , blank , tab , cr , ff , lf

Variables
Variables must be declared before use
Shared vars must begin with uppercase
Local vars must begin with lowercase
Reserved names: self , super , thisContext , true , false , and nil

Variable scope
Global: defined in current environment (that is usually Smalltalk ) and
accessible by all objects in system
Special (reserved): self , super , thisContext , true , false , and nil
Method Temporary: local to a method
Block Temporary: local to a block
Pool: variables in a Dictionary object
Method Parameters: automatic local vars created as a result of message call
with params
Block Parameters: automatic local vars created as a result of value: message
call
Class: shared with all instances of one class & its subclasses
Class Instance: unique to each instance of a class
Instance Variables: unique to each instance

Syntax
Comments are enclosed in quotes ( " )
Period ( . ) is the statement separator

Transcript
https://squeak.org/documentation/terse_guide/ 1/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

Transcript clear. "clear to transcript


Transcript show: 'Hello World'. "output string in tr
Transcript nextPutAll: 'Hello World'. "output string in tr
Transcript nextPut: $A. "output character in
Transcript space. "output space charac
Transcript tab. "output tab characte
Transcript cr. "carriage return / l
'Hello' printOn: Transcript. "append print string
'Hello' storeOn: Transcript. "append store string
Transcript endEntry. "flush the output bu

Assignment
| x y |
x := 5. "assignment"
x := y := z := 6. "compound assignment
x := (y := 6) + 1.
"x _ 4." "older assignment st
x := Object new. "bind to allocated i
x := 123 class. "discover the object
x := Integer superclass. "discover the superc
x := Object allInstances. "get an array of all
x := Integer allSuperclasses. "get all superclasse
x := 1.2 hash. "hash value for obje
y := x copy. "copy object"
y := x shallowCopy. "copy object (not ov
y := x deepCopy. "copy object and ins
y := x veryDeepCopy. "complete tree copy

Constants
| b x |
b := true. "true constant"
b := false. "false constant"
x := nil. "nil object constant
x := 1. "integer constants"
x := 3.14. "float constants"
x := 2e-2. "fractional constant
x := 16r0F. "hex constant"
x := -1. "negative constants"
x := 'Hello'. "string constant"
x := 'I''m here'. "single quote escape
x := $A. "character constant"
x := $ . "character constant
x := #aSymbol. "symbol constants"
x := #(3 2 1). "array constants"
x := #('abc' 2 $a). "mixing of types all

https://squeak.org/documentation/terse_guide/ 2/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

Booleans
| b x y |
x := 1. y := 2.
b := (x = y). "equals"
b := (x ~= y). "not equals"
b := (x == y). "identical"
b := (x ~~ y). "not identical"
b := (x > y). "greater than"
b := (x < y). "less than"
b := (x >= y). "greater than or equ
b := (x <= y). "less than or equal"
b := b not. "boolean not"
b := (x < 5) & (y > 1). "boolean and"
b := (x < 5) | (y > 1). "boolean or"
b := (x < 5) and: [y > 1]. "boolean and (short-
b := (x < 5) or: [y > 1]. "boolean or (short-c
b := (x < 5) eqv: (y > 1). "test if both true o
b := (x < 5) xor: (y > 1). "test if one true an
b := 5 between: 3 and: 12. "between (inclusive)
b := 123 isKindOf: Number. "test if object is c
b := 123 isMemberOf: SmallInteger. "test if object is t
b := 123 respondsTo: sqrt. "test if object resp
b := x isNil. "test if object is n
b := x isZero. "test if number is z
b := x positive. "test if number is p
b := x strictlyPositive. "test if number is g
b := x negative. "test if number is n
b := x even. "test if number is e
b := x odd. "test if number is o
b := x isLiteral. "test if literal con
b := x isInteger. "test if object is i
b := x isFloat. "test if object is f
b := x isNumber. "test if object is n
b := $A isUppercase. "test if upper case
b := $A isLowercase. "test if lower case

Arithmetic expressions

https://squeak.org/documentation/terse_guide/ 3/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| x |
x := 6 + 3. "addition"
x := 6 - 3. "subtraction"
x := 6 * 3. "multiplication"
x := 1 + 2 * 3. "evaluation always l
x := 5 / 3. "division with fract
x := 5.0 / 3.0. "division with float
x := 5.0 // 3.0. "integer divide"
x := 5.0 \\ 3.0. "integer remainder"
x := -5. "unary minus"
x := 5 sign. "numeric sign (1, -1
x := 5 negated. "negate receiver"
x := 1.2 integerPart. "integer part of num
x := 1.2 fractionPart. "fractional part of
x := 5 reciprocal. "reciprocal function
x := 6 * 3.1. "auto convert to flo
x := 5 squared. "square function"
x := 25 sqrt. "square root"
x := 5 raisedTo: 2. "power function"
x := 5 raisedToInteger: 2. "power function with
x := 5 exp. "exponential"
x := -5 abs. "absolute value"
x := 3.99 rounded. "round"
x := 3.99 truncated. "truncate"
x := 3.99 roundTo: 1. "round to specified
x := 3.99 truncateTo: 1. "truncate to specifi
x := 3.99 floor. "truncate"
x := 3.99 ceiling. "round up"
x := 5 factorial. "factorial"
x := -5 quo: 3. "integer divide roun
x := -5 rem: 3. "integer remainder r
x := 28 gcd: 12. "greatest common den
x := 28 lcm: 12. "least common multip
x := 100 ln. "natural logarithm"
x := 100 log. "base 10 logarithm"
x := 100 log: 10. "logarithm with spec
x := 100 floorLog: 10. "floor of the log"
x := 180 degreesToRadians. "convert degrees to
x := 3.14 radiansToDegrees. "convert radians to
x := 0.7 sin. "sine"
x := 0.7 cos. "cosine"
x := 0.7 tan. "tangent"
x := 0.7 arcSin. "arcsine"
x := 0.7 arcCos. "arccosine"
x := 0.7 arcTan. "arctangent"
x := 10 max: 20. "get maximum of two
x := 10 min: 20. "get minimum of two
x := Float pi. "pi"
x := Float e. "exp constant"
x := Float infinity. "infinity"
x := Float nan. "not-a-number"
x := Random new next; yourself. x next. "random number strea
x := 100 atRandom. "quick random number

https://squeak.org/documentation/terse_guide/ 4/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

Bitwise Manipulation
| b x |
x := 16rFF bitAnd: 16r0F. "and bits"
x := 16rF0 bitOr: 16r0F. "or bits"
x := 16rFF bitXor: 16r0F. "xor bits"
x := 16rFF bitInvert. "invert bits"
x := 16r01 bitShift: 4. "left shift"
x := 16r10 bitShift: -4. "right shift"
x := 16r80 bitAt: 7. "bit at position (0|
x := 16r80 highbit. "position of highest
b := 16rFF allMask: 16r0F. "test if all bits se
b := 16rFF anyMask: 16r0F. "test if any bits se
b := 16rFF noMask: 16r0F. "test if all bits se

Conversion
| x |
x := 3.99 asInteger. "convert number to i
x := 3.99 asFraction. "convert number to f
x := 3 asFloat. "convert number to f
x := 65 asCharacter. "convert integer to
x := $A asciiValue. "convert character t
x := 3.99 printString. "convert object to s
x := 3.99 storeString. "convert object to s
x := 15 radix: 16. "convert to string i
x := 15 printStringBase: 16.
x := 15 storeStringBase: 16.

Blocks
Blocks are objects and may be assigned to a variable
Value is last expression evaluated unless explicit return
Blocks may be nested
Specification [ arguments | | localvars | expressions ]
Squeak does not currently support localvars in blocks
Max of three arguments allowed
^ expression terminates block & method (exits all nested blocks)
Blocks intended for long term storage should not contain ^

https://squeak.org/documentation/terse_guide/ 5/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| x y z |
x := [ y := 1. z := 2. ]. x value. "simple block usage"
x := [ :argOne :argTwo | argOne, ' and ' , argTwo.]. "set up block with a
Transcript show: (x value: 'First' value: 'Second'); cr. "use block with argu
"x := [ | z | z := 1.]. localvars not avail

Method calls
Unary methods are messages with no arguments
Binary methods
Keyword methods are messages with selectors including colons

Standard categories/protocols
initialize-release (methods called for new instance)
accessing (get/set methods)
testing (boolean tests - is)
comparing (boolean tests with parameter
displaying (gui related methods)
printing (methods for printing)
updating (receive notification of changes)
private (methods private to class)
instance-creation (class methods for creating instance)

| x |
x := 2 sqrt. "unary message"
x := 2 raisedTo: 10. "keyword message"
x := 194 * 9. "binary message"
Transcript show: (194 * 9) printString; cr. "combination (chaini
x := 2 perform: #sqrt. "indirect method inv
Transcript "Cascading - send mu
show: 'hello ';
show: 'world';
cr.
x := 3 + 2; * 100. "result=300. Sends m

Conditional Statements

https://squeak.org/documentation/terse_guide/ 6/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| x
|
x >
10 ifTrue: [Transcript show: 'ifTrue'; cr]. "if then"
x >
10 ifFalse: [Transcript show: 'ifFalse'; cr]. "if else"
x >
10 "if then else"
ifTrue: [Transcript show: 'ifTrue'; cr]
ifFalse: [Transcript show: 'ifFalse'; cr].
x > 10 "if else then"
ifFalse: [Transcript show: 'ifFalse'; cr]
ifTrue: [Transcript show: 'ifTrue'; cr].
Transcript show:
(x > 10
ifTrue: ['ifTrue']
ifFalse: ['ifFalse']);
cr.
Transcript "nested if then else
show:
(x > 10
ifTrue: [x > 5
ifTrue: ['A']
ifFalse: ['B']]
ifFalse: ['C']);
cr.
switch := Dictionary new. "switch functionalit
switch at: $A put: [Transcript show: 'Case A'; cr].
switch at: $B put: [Transcript show: 'Case B'; cr].
switch at: $C put: [Transcript show: 'Case C'; cr].
result := (switch at: $B) value.

Iteration statements
| x y |
x := 4. y := 1.
[x > 0] whileTrue: [x := x - 1. y := y * 2]. "while true loop"
[x >= 4] whileFalse: [x := x + 1. y := y * 2]. "while false loop"
x timesRepeat: [y := y * 2]. "times repear loop (
1 to: x do: [:a | y := y * 2]. "for loop"
1 to: x by: 2 do: [:a | y := y / 2]. "for loop with speci
#(5 4 3) do: [:a | x := x + a]. "iterate over array

Character

https://squeak.org/documentation/terse_guide/ 7/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| b x y |
x := $A. "character assignmen
y := x isLowercase. "test if lower case"
y := x isUppercase. "test if upper case"
y := x isLetter. "test if letter"
y := x isDigit. "test if digit"
y := x isAlphaNumeric. "test if alphanumeri
y := x isSeparator. "test if separator c
y := x isVowel. "test if vowel"
y := x digitValue. "convert to numeric
y := x asLowercase. "convert to lower ca
y := x asUppercase. "convert to upper ca
y := x asciiValue. "convert to numeric
y := x asString. "convert to string"
b := $A <= $B. "comparison"
y := $A max: $B.

Symbol
| b x y |
x := #Hello. "symbol assignment"
y := #Symbol, 'Concatenation'. "symbol concatenatio
b := x isEmpty. "test if symbol is e
y := x size. "string size"
y := x at: 2. "char at location"
y := x copyFrom: 2 to: 4. "substring"
y := x indexOf: $e ifAbsent: [0]. "first position of c
x do: [:a | Transcript show: a printString; cr]. "iterate over the st
b := x allSatisfy: [:a | (a >= $a) & (a <= $z)]. "test if all element
y := x select: [:a | a > $a]. "return all elements
y := x asString. "convert symbol to s
y := x asText. "convert symbol to t
y := x asArray. "convert symbol to a
y := x asOrderedCollection. "convert symbol to o
y := x asSortedCollection. "convert symbol to s
y := x asBag. "convert symbol to b
y := x asSet. "convert symbol to s

String

https://squeak.org/documentation/terse_guide/ 8/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| b x y |
x := 'This is a string'. "string assignment"
x := 'String', 'Concatenation'. "string concatenatio
b := x isEmpty. "test if string is e
y := x size. "string size"
y := x at: 2. "char at location"
y := x copyFrom: 2 to: 4. "substring"
y := x indexOf: $a ifAbsent: [0]. "first position of c
x := String new: 4. "allocate string obj
x "set string elements
at: 1 put: $a;
at: 2 put: $b;
at: 3 put: $c;
at: 4 put: $e.
x := String with: $a with: $b with: $c with: $d. "set up to 4 element
x do: [:a | Transcript show: a printString; cr]. "iterate over the st
b := x conform: [:a | (a >= $a) & (a <= $z)]. "test if all element
y := x select: [:a | a > $a]. "return all elements
y := x asSymbol. "convert string to s
y := x asArray. "convert string to a
x := 'ABCD' asByteArray. "convert string to b
y := x asOrderedCollection. "convert string to o
y := x asSortedCollection. "convert string to s
y := x asBag. "convert string to b
y := x asSet. "convert string to s
y := x shuffled. "randomly shuffle st

Array
Array : Fixed length collection
ByteArray : Array limited to byte elements (0-255)
WordArray : Array limited to word elements (0-2^32)

https://squeak.org/documentation/terse_guide/ 9/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| b x y sum max |
x := #(4 3 2 1). "constant array"
x := Array with: 5 with: 4 with: 3 with: 2. "create array with u
x := Array new: 4. "allocate an array w
x "set array elements"
at: 1 put: 5;
at: 2 put: 4;
at: 3 put: 3;
at: 4 put: 2.
b := x isEmpty. "test if array is em
y := x size. "array size"
y := x at: 4. "get array element a
b := x includes: 3. "test if element is
y := x copyFrom: 2 to: 4. "subarray"
y := x indexOf: 3 ifAbsent: [0]. "first position of e
y := x occurrencesOf: 3. "number of times obj
x do: [:a | Transcript show: a printString; cr]. "iterate over the ar
b := x conform: [:a | (a >= 1) & (a <= 4)]. "test if all element
y := x select: [:a | a > 2]. "return collection o
y := x reject: [:a | a < 2]. "return collection o
y := x collect: [:a | a + a]. "transform each elem
y := x detect: [:a | a > 3] ifNone: []. "find position of fi
sum := 0. x do: [:a | sum := sum + a]. sum. "sum array elements"
sum := 0. 1 to: (x size) do: [:a | sum := sum + (x at: a)]. "sum array elements"
sum := x inject: 0 into: [:a :c | a + c]. "sum array elements"
max := x inject: 0 into: [:a :c | (a > c) "find max element in
ifTrue: [a]
ifFalse: [c]].
y := x shuffled. "randomly shuffle co
y := x asArray. "convert to array"
y := x asByteArray. "convert to byte arr
y := x asWordArray. "convert to word arr
y := x asOrderedCollection. "convert to ordered
y := x asSortedCollection. "convert to sorted c
y := x asBag. "convert to bag coll
y := x asSet. "convert to set coll

OrderedCollection
Acts like an expandable array

https://squeak.org/documentation/terse_guide/ 10/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| b x y sum max |
x := OrderedCollection with: 4 with: 3 with: 2 with: 1. "create collection w
x := OrderedCollection new. "allocate collection
x add: 3; add: 2; add: 1; add: 4; yourself. "add element to coll
y := x addFirst: 5. "add element at begi
y := x removeFirst. "remove first elemen
y := x addLast: 6. "add element at end
y := x removeLast. "remove last element
y := x addAll: #(7 8 9). "add multiple elemen
y := x removeAll: #(7 8 9). "remove multiple ele
x at: 2 put: 3. "set element at inde
y := x remove: 5 ifAbsent: []. "remove element from
b := x isEmpty. "test if empty"
y := x size. "number of elements"
y := x at: 2. "retrieve element at
y := x first. "retrieve first elem
y := x last. "retrieve last eleme
b := x includes: 5. "test if element is
y := x copyFrom: 2 to: 3. "subcollection"
y := x indexOf: 3 ifAbsent: [0]. "first position of e
y := x occurrencesOf: 3. "number of times obj
x do: [:a | Transcript show: a printString; cr]. "iterate over the co
b := x conform: [:a | (a >= 1) & (a <= 4)]. "test if all element
y := x select: [:a | a > 2]. "return collection o
y := x reject: [:a | a < 2]. "return collection o
y := x collect: [:a | a + a]. "transform each elem
y := x detect: [:a | a > 3] ifNone: []. "find position of fi
sum := 0. x do: [:a | sum := sum + a]. sum. "sum elements"
sum := 0. 1 to: (x size) do: [:a | sum := sum + (x at: a)]. "sum elements"
sum := x inject: 0 into: [:a :c | a + c]. "sum elements"
max := x inject: 0 into: [:a :c | (a > c) "find max element in
ifTrue: [a]
ifFalse: [c]].
y := x shuffled. "randomly shuffle co
y := x asArray. "convert to array"
y := x asOrderedCollection. "convert to ordered
y := x asSortedCollection. "convert to sorted c
y := x asBag. "convert to bag coll
y := x asSet. "convert to set coll

SortedCollection
Like OrderedCollection except order of elements determined by sorting criteria

https://squeak.org/documentation/terse_guide/ 11/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| b x y sum max |
x := SortedCollection with: 4 with: 3 with: 2 with: 1. "create collection w
x := SortedCollection new. "allocate collection
x := SortedCollection sortBlock: [:a :c | a > c]. "set sort criteria"
x add: 3; add: 2; add: 1; add: 4; yourself. "add element to coll
y := x addFirst: 5. "add element at begi
y := x removeFirst. "remove first elemen
y := x addLast: 6. "add element at end
y := x removeLast. "remove last element
y := x addAll: #(7 8 9). "add multiple elemen
y := x removeAll: #(7 8 9). "remove multiple ele
y := x remove: 5 ifAbsent: []. "remove element from
b := x isEmpty. "test if empty"
y := x size. "number of elements"
y := x at: 2. "retrieve element at
y := x first. "retrieve first elem
y := x last. "retrieve last eleme
b := x includes: 4. "test if element is
y := x copyFrom: 2 to: 3. "subcollection"
y := x indexOf: 3 ifAbsent: [0]. "first position of e
y := x occurrencesOf: 3. "number of times obj
x do: [:a | Transcript show: a printString; cr]. "iterate over the co
b := x conform: [:a | (a >= 1) & (a <= 4)]. "test if all element
y := x select: [:a | a > 2]. "return collection o
y := x reject: [:a | a < 2]. "return collection o
y := x collect: [:a | a + a]. "transform each elem
y := x detect: [:a | a > 3] ifNone: []. "find position of fi
sum := 0. x do: [:a | sum := sum + a]. sum. "sum elements"
sum := 0. 1 to: (x size) do: [:a | sum := sum + (x at: a)]. "sum elements"
sum := x inject: 0 into: [:a :c | a + c]. "sum elements"
max := x inject: 0 into: [:a :c | (a > c) "find max element in
ifTrue: [a]
ifFalse: [c]].
y := x asArray. "convert to array"
y := x asOrderedCollection. "convert to ordered
y := x asSortedCollection. "convert to sorted c
y := x asBag. "convert to bag coll
y := x asSet. "convert to set coll

Bag
Like OrderedCollection except elements are in no particular order

https://squeak.org/documentation/terse_guide/ 12/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| b x y sum max |
x := Bag with: 4 with: 3 with: 2 with: 1. "create collection w
x := Bag new. "allocate collection
x add: 4; add: 3; add: 1; add: 2; yourself. "add element to coll
x add: 3 withOccurrences: 2. "add multiple copies
y := x addAll: #(7 8 9). "add multiple elemen
y := x removeAll: #(7 8 9). "remove multiple ele
y := x remove: 4 ifAbsent: []. "remove element from
b := x isEmpty. "test if empty"
y := x size. "number of elements"
b := x includes: 3. "test if element is
y := x occurrencesOf: 3. "number of times obj
x do: [:a | Transcript show: a printString; cr]. "iterate over the co
b := x conform: [:a | (a >= 1) & (a <= 4)]. "test if all element
y := x select: [:a | a > 2]. "return collection o
y := x reject: [:a | a < 2]. "return collection o
y := x collect: [:a | a + a]. "transform each elem
y := x detect: [:a | a > 3] ifNone: []. "find position of fi
sum := 0. x do: [:a | sum := sum + a]. sum. "sum elements"
sum := x inject: 0 into: [:a :c | a + c]. "sum elements"
max := x inject: 0 into: [:a :c | (a > c) "find max element in
ifTrue: [a]
ifFalse: [c]].
y := x asOrderedCollection. "convert to ordered
y := x asSortedCollection. "convert to sorted c
y := x asBag. "convert to bag coll
y := x asSet. "convert to set coll

Sets
Set : like Bag except duplicates not allowed
IdentitySet : uses identity test ( == rather than = )

https://squeak.org/documentation/terse_guide/ 13/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| b x y sum max |
x := Set with: 4 with: 3 with: 2 with: 1. "create collection w
x := Set new. "allocate collection
x add: 4; add: 3; add: 1; add: 2; yourself. "add element to coll
y := x addAll: #(7 8 9). "add multiple elemen
y := x removeAll: #(7 8 9). "remove multiple ele
y := x remove: 4 ifAbsent: []. "remove element from
b := x isEmpty. "test if empty"
y := x size. "number of elements"
x includes: 4. "test if element is
x do: [:a | Transcript show: a printString; cr]. "iterate over the co
b := x conform: [:a | (a >= 1) & (a <= 4)]. "test if all element
y := x select: [:a | a > 2]. "return collection o
y := x reject: [:a | a < 2]. "return collection o
y := x collect: [:a | a + a]. "transform each elem
y := x detect: [:a | a > 3] ifNone: []. "find position of fi
sum := 0. x do: [:a | sum := sum + a]. sum. "sum elements"
sum := x inject: 0 into: [:a :c | a + c]. "sum elements"
max := x inject: 0 into: [:a :c | (a > c) "find max element in
ifTrue: [a]
ifFalse: [c]].
y := x asArray. "convert to array"
y := x asOrderedCollection. "convert to ordered
y := x asSortedCollection. "convert to sorted c
y := x asBag. "convert to bag coll
y := x asSet. "convert to set coll

Interval

https://squeak.org/documentation/terse_guide/ 14/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| b x y sum max |
x := Interval from: 5 to: 10. "create interval obj
x := 5 to: 10.
x := Interval from: 5 to: 10 by: 2. "create interval obj
x := 5 to: 10 by: 2.
b := x isEmpty. "test if empty"
y := x size. "number of elements"
x includes: 9. "test if element is
x do: [:k | Transcript show: k printString; cr]. "iterate over interv
b := x conform: [:a | (a >= 1) & (a <= 4)]. "test if all element
y := x select: [:a | a > 7]. "return collection o
y := x reject: [:a | a < 2]. "return collection o
y := x collect: [:a | a + a]. "transform each elem
y := x detect: [:a | a > 3] ifNone: []. "find position of fi
sum := 0. x do: [:a | sum := sum + a]. sum. "sum elements"
sum := 0. 1 to: (x size) do: [:a | sum := sum + (x at: a)]. "sum elements"
sum := x inject: 0 into: [:a :c | a + c]. "sum elements"
max := x inject: 0 into: [:a :c | (a > c) "find max element in
ifTrue: [a]
ifFalse: [c]].
y := x asArray. "convert to array"
y := x asOrderedCollection. "convert to ordered
y := x asSortedCollection. "convert to sorted c
y := x asBag. "convert to bag coll
y := x asSet. "convert to set coll

Associations
| x y |
x := #myVar->'hello'.
y := x key.
y := x value.

Dictionaries
Dictionary
IdentityDictionary : uses identity test ( == rather than = )

https://squeak.org/documentation/terse_guide/ 15/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| b x y sum max |
x := Dictionary new. "allocate collection
x add: #a->4; add: #b->3; add: #c->1; add: #d->2; yourself. "add element to coll
x at: #e put: 3. "set element at inde
b := x isEmpty. "test if empty"
y := x size. "number of elements"
y := x at: #a ifAbsent: []. "retrieve element at
y := x keyAtValue: 3 ifAbsent: []. "retrieve key for gi
y := x removeKey: #e ifAbsent: []. "remove element from
b := x includes: 3. "test if element is
b := x includesKey: #a. "test if element is
y := x occurrencesOf: 3. "number of times obj
y := x keys. "set of keys"
y := x values. "bag of values"
x do: [:a | Transcript show: a printString; cr]. "iterate over the va
x keysDo: [:a | Transcript show: a printString; cr]. "iterate over the ke
x associationsDo: [:a | Transcript show: a printString; cr]."iterate over the as
x keysAndValuesDo: [:aKey :aValue | Transcript "iterate over keys a
show: aKey printString; space;
show: aValue printString; cr].
b := x conform: [:a | (a >= 1) & (a <= 4)]. "test if all element
y := x select: [:a | a > 2]. "return collection o
y := x reject: [:a | a < 2]. "return collection o
y := x collect: [:a | a + a]. "transform each elem
y := x detect: [:a | a > 3] ifNone: []. "find position of fi
sum := 0. x do: [:a | sum := sum + a]. sum. "sum elements"
sum := x inject: 0 into: [:a :c | a + c]. "sum elements"
max := x inject: 0 into: [:a :c | (a > c) "find max element in
ifTrue: [a]
ifFalse: [c]].
y := x asArray. "convert to array"
y := x asOrderedCollection. "convert to ordered
y := x asSortedCollection. "convert to sorted c
y := x asBag. "convert to bag coll
y := x asSet. "convert to set coll

Smalltalk at: #CMRGlobal put: 'CMR entry'. "put global in Small


x := Smalltalk at: #CMRGlobal. "read global from Sm
Transcript show: (CMRGlobal printString). "entries are directl
Smalltalk keys do: [ :k | "print out all class
((Smalltalk at: k) isKindOf: Class)
ifFalse: [Transcript show: k printString; cr]].
Smalltalk at: #CMRDictionary put: (Dictionary new). "set up user defined
CMRDictionary at: #MyVar1 put: 'hello1'. "put entry in dictio
CMRDictionary add: #MyVar2->'hello2'. "add entry to dictio
CMRDictionary size. "dictionary size"
CMRDictionary keys do: [ :k | "print out keys in d
Transcript show: k printString; cr].
CMRDictionary values do: [ :k | "print out values in
Transcript show: k printString; cr].
CMRDictionary keysAndValuesDo: [:aKey :aValue | "print out keys and
Transcript
show: aKey printString;
space;
show: aValue printString;
cr].
CMRDictionary associationsDo: [:aKeyValue | "another iterator fo
Transcript show: aKeyValue printString; cr].
https://squeak.org/documentation/terse_guide/ 16/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak
Smalltalk removeKey: #CMRGlobal ifAbsent: []. "remove entry from S
Smalltalk removeKey: #CMRDictionary ifAbsent: []. "remove user diction

Internal Stream
| b x ios |
ios := ReadStream on: 'Hello read stream'.
ios := ReadStream on: 'Hello read stream' from: 1 to: 5.
[(x := ios nextLine) notNil]
whileTrue: [Transcript show: x; cr].
ios position: 3.
ios position.
x := ios next.
x := ios peek.
x := ios contents.
b := ios atEnd.

ios := ReadWriteStream on: 'Hello read stream'.


ios := ReadWriteStream on: 'Hello read stream' from: 1 to: 5.
ios := ReadWriteStream with: 'Hello read stream'.
ios := ReadWriteStream with: 'Hello read stream' from: 1 to: 10.
ios position: 0.
[(x := ios nextLine) notNil]
whileTrue: [Transcript show: x; cr].
ios position: 6.
ios position.
ios nextPutAll: 'Chris'.
x := ios next.
x := ios peek.
x := ios contents.
b := ios atEnd.

FileStream
| b x ios |
ios := FileStream newFileNamed: 'ios.txt'.
ios nextPut: $H; cr.
ios nextPutAll: 'Hello File'; cr.
'Hello File' printOn: ios.
'Hello File' storeOn: ios.
ios close.

ios := FileStream oldFileNamed: 'ios.txt'.


[(x := ios nextLine) notNil]
whileTrue: [Transcript show: x; cr].
ios position: 3.
x := ios position.
x := ios next.
x := ios peek.
b := ios atEnd.
ios close.

https://squeak.org/documentation/terse_guide/ 17/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

Date
| b x y |
x := Date today. "create date for tod
x := Date dateAndTimeNow. "create date from cu
x := Date readFromString: '01/02/1999'. "create date from fo
x := Date newDay: 12 month: #July year: 1999 "create date from pa
x := Date fromDays: 36000. "create date from el
y := Date dayOfWeek: #Monday. "day of week as int
y := Date indexOfMonth: #January. "month of year as in
y := Date daysInMonth: 2 forYear: 1996. "day of month as int
y := Date daysInYear: 1996. "days in year (365|3
y := Date nameOfDay: 1 "weekday name (#Mond
y := Date nameOfMonth: 1. "month name (#Januar
y := Date leapYear: 1996. "1 if leap year; 0 i
y := x weekday. "day of week (#Monda
y := x previous: #Monday. "date for previous d
y := x dayOfMonth. "day of month (1-31)
y := x day. "day of year (1-366)
y := x firstDayOfMonth. "day of year for fir
y := x monthName. "month of year (#Jan
y := x monthIndex. "month of year (1-12
y := x daysInMonth. "days in month (1-31
y := x year. "year (19xx)"
y := x daysInYear. "days in year (365|3
y := x daysLeftInYear. "days left in year (
y := x asSeconds. "seconds elapsed sin
y := x addDays: 10. "add days to date ob
y := x subtractDays: 10. "subtract days to da
y := x subtractDate: (Date today). "subtract date (resu
y := x printFormat: #(2 1 3 $/ 1 1). "print formatted dat
b := (x <= Date today). "comparison"

Time
| b x y |
x := Time now. "create time from cu
x := Time dateAndTimeNow. "create time from cu
x := Time readFromString: '3:47:26 pm'. "create time from fo
x := Time fromSeconds: (60 * 60 * 4). "create time from el
y := Time millisecondClockValue. "milliseconds since
y := Time totalSeconds. "total seconds since
y := x seconds. "seconds past minute
y := x minutes. "minutes past hour (
y := x hours. "hours past midnight
y := x addTime: (Time now). "add time to time ob
y := x subtractTime: (Time now). "subtract time to ti
y := x asSeconds. "convert time to sec
x := Time millisecondsToRun: [ "timing facility"
1 to: 1000 do: [:index | y := 3.14 * index]].
b := (x <= Time now). "comparison"

https://squeak.org/documentation/terse_guide/ 18/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

Point
| x y |
x := 200@100. "obtain a new point"
y := x x. "x coordinate"
y := x y. "y coordinate"
x := 200@100 negated. "negates x and y"
x := (-200@-100) abs. "absolute value of x
x := ([email protected]) rounded. "round x and y"
x := ([email protected]) truncated. "truncate x and y"
x := 200@100 + 100. "add scale to both x
x := 200@100 - 100. "subtract scale from
x := 200@100 * 2. "multiply x and y by
x := 200@100 / 2. "divide x and y by s
x := 200@100 // 2. "divide x and y by s
x := 200@100 \\ 3. "remainder of x and
x := 200@100 + 50@25. "add points"
x := 200@100 - 50@25. "subtract points"
x := 200@100 * 3@4. "multiply points"
x := 200@100 // 3@4. "divide points"
x := 200@100 max: 50@200. "max x and y"
x := 200@100 min: 50@200. "min x and y"
x := 20@5 dotProduct: 10@2. "sum of product (x1*

Rectangle
Rectangle fromUser

Pen

https://squeak.org/documentation/terse_guide/ 19/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| myPen |
Display restoreAfter: [
Display fillWhite.

myPen := Pen new. "get graphic pen"


myPen squareNib: 1.
myPen color: (Color blue). "set pen color"
myPen home. "position pen at cen
myPen up. "makes nib unable to
myPen down. "enable the nib to d
myPen north. "points direction tow
myPen turn: -180. "add specified degre
myPen direction. "get current angle o
myPen go: 50. "move pen specified
myPen location. "get the pen positio
myPen goto: 200@200. "move to specified p
myPen place: 250@250. "move to specified p
myPen print: 'Hello World' withFont: (TextStyle default fontAt: 1).
Display extent. "get display width@h
Display width. "get display width"
Display height. "get display height"

].

Dynamic Message Calling/Compiling

https://squeak.org/documentation/terse_guide/ 20/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| receiver message result argument keyword1 keyword2 argument1 argument2 |


"unary message"
receiver := 5.
message := 'factorial' asSymbol.
result := receiver perform: message.
result := Compiler evaluate: ((receiver storeString), ' ', message).
result := (Message new setSelector: message arguments: #()) sentTo: receiver.

"binary message"
receiver := 1.
message := '+' asSymbol.
argument := 2.
result := receiver perform: message withArguments: (Array with: argument).
result := Compiler evaluate: ((receiver storeString), ' ', message, ' ', (argume
result := (Message new setSelector: message arguments: (Array with: argument)) s

"keyword messages"
receiver := 12.
keyword1 := 'between:' asSymbol.
keyword2 := 'and:' asSymbol.
argument1 := 10.
argument2 := 20.
result := receiver
perform: (keyword1, keyword2) asSymbol
withArguments: (Array with: argument1 with: argument2).
result := Compiler evaluate:
((receiver storeString), ' ', keyword1, (argument1 storeString) , ' ', keywor
result := (Message new
setSelector: (keyword1, keyword2) asSymbol
arguments: (Array with: argument1 with: argument2))
sentTo: receiver.

class/meta-class

https://squeak.org/documentation/terse_guide/ 21/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

| b x |
x := String name. "class name"
x := String category. "organization catego
x := String comment. "class comment"
x := String kindOfSubclass. "subclass type - sub
x := String definition. "class definition"
x := String instVarNames. "immediate instance
x := String allInstVarNames. "accumulated instanc
x := String classVarNames. "immediate class var
x := String allClassVarNames. "accumulated class v
x := String sharedPools. "immediate dictionar
x := String allSharedPools. "accumulated diction
x := String selectors. "message selectors f
x := String sourceCodeAt: #size. "source code for spe
x := String allInstances. "collection of all i
x := String superclass. "immediate superclas
x := String allSuperclasses. "accumulated supercl
x := String withAllSuperclasses. "receiver class and
x := String subclasses. "immediate subclasse
x := String allSubclasses. "accumulated subclas
x := String withAllSubclasses. "receiver class and
b := String instSize. "number of named ins
b := String isFixed. "true if no indexed
b := String isVariable. "true if has indexed
b := String isPointers. "true if index insta
b := String isBits. "true if index insta
b := String isBytes. "true if index insta
b := String isWords. "true if index insta
Object withAllSubclasses size. "get total number of

Debugging
| a b x |
x yourself. "returns receiver"
String browse. "browse specified cl
x inspect. "open object inspect
x confirm: 'Is this correct?'.
x halt. "breakpoint to open
x halt: 'Halt message'.
x notify: 'Notify text'.
x error: 'Error string'. "open up error window
x doesNotUnderstand: #cmrMessage. "flag message is not
x shouldNotImplement. "flag message should
x subclassResponsibility. "flag message as abs
x errorImproperStore. "flag an improper st
x errorNonIntegerIndex. "flag only integers
x errorSubscriptBounds. "flag subscript out
x primitiveFailed. "system primitive fa

a := 'A1'. b := 'B2'. a become: b. "switch two objects"


Transcript show: a, b; cr.

https://squeak.org/documentation/terse_guide/ 22/23
5/4/2020 Squeak/Smalltalk | Terse Guide to Squeak

Misc
| x |
"Smalltalk condenseChanges." "compress the change
x := FillInTheBlank request: 'Prompt Me'. "prompt user for inp
x := UIManager default request: 'Prompt Me'. "prompt user for inp
Utilities openCommandKeyHelp.

© 1996-2020 Squeak.org - Imprint (/imprint/) -  (http://forum.world.st/Squeak-


f45487.html)  (https://twitter.com/squeaksmalltalk) 
(https://www.linkedin.com/groups?gid=98736)  (https://github.com/squeak-smalltalk)

https://squeak.org/documentation/terse_guide/ 23/23

You might also like