0% found this document useful (0 votes)
102 views

Smalltalk Cheatsheet Triptico PDF

This document provides examples of using streams and collections in Smalltalk. (1) It shows how to use streams to read and write sequences of objects to and from collections. (2) Various collection methods are demonstrated, including querying, modifying, and traversing collections like arrays, sets, bags, and dictionaries. (3) Examples are also given of conditionals, loops, and other language keywords in Smalltalk.

Uploaded by

Juan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Smalltalk Cheatsheet Triptico PDF

This document provides examples of using streams and collections in Smalltalk. (1) It shows how to use streams to read and write sequences of objects to and from collections. (2) Various collection methods are demonstrated, including querying, modifying, and traversing collections like arrays, sets, bags, and dictionaries. (3) Examples are also given of conditionals, loops, and other language keywords in Smalltalk.

Uploaded by

Juan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#(1 2 3 4) collect: [ :each | each * 2 ] −→ #( 2 4 6 8 ) Streams

#(1 2 3 4)
inject: 0 " ReadStream – to read a sequence of objects from a collection
into: [ :each :result | each + result ] −→ 10 "
stream := 'Hello World' readStream. Smalltalk Cheat Sheet
" testing " stream next −→ $H
#( 2 4 ) anySatisfy: [ :each | each odd ] −→ false stream upTo: $o −→ 'ell'
#( 2 4 ) allSatisfy: [ :each | each even ] −→ true stream skip: 2. Software Composition Group
stream peek −→ $o University of Bern
" finding " stream upToEnd −→ 'orld'
'abcdef' includes: $e −→ true May 21, 2008
'abcdef' contains: [ :each | each isUppercase ] −→ false " WriteStream – to write a sequence of objects to a collection "
'abcdef' stream := WriteStream on: Array new.
detect: [ :each | each isVowel ] stream nextPut: 'Hello'.
ifNone: [ $u ] −→ $a stream nextPutAll: #( 1 2 3 ).
stream contents −→ #( 'Hello' 1 2 3 ) 1. The Environment
" String – a collection of characters "
string := 'abc'. File Streams 1. class categories 2. classes 4. method protocols 5. methods
string := string , 'DEF' −→ 'abcDEF'
string beginsWith: 'abc' −→ true fileStream := FileDirectory default newFileNamed: 'tmp.txt'.
string endsWith: 'abc' −→ false fileStream nextPutAll: 'my cool stuff'.
string includesSubString: 'cD' −→ true fileStream close.
string asLowercase −→ 'abcdef
string asUppercase −→ 'ABCDEF' fileStream := FileDirectory default oldFileNamed: 'tmp.txt'.
fileStream contents −→ 'my cool stuff'
" OrderedCollection – an ordered collection of objects "
ordered := OrderedCollection new. Method Definition
ordered addLast: 'world'.
messageSelectorAndArgumentNames
ordered addFirst: 'hello'.
"comment stating purpose of message"
ordered size −→ 2 3. instance-, class-side,
| temporary variable names |
ordered at: 2 −→ 'world' comments
statements
ordered removeLast −→ 'world'
ordered removeFirst −→ 'hello' 6. source code
ordered isEmpty −→ true Class Definition (CMD–S) to save and compile

Object subclass: #NameOfSubclass


" Set – an unordered collection of objects without duplicates " instanceVariableNames: 'instVar1 instVar2'
set := Set new. Figure 1: The Smalltalk Code Browser
classVariableNames: ''
set add 'hello'; add: 'hello'. poolDictionaries: ''
set size −→ 1 category: 'Category--Name' • Do it (CMD – D): Evaluate selected code.
" Bag – an unordered collection of objects with duplicates " • Print it (CMD – P): Display the result of evaluating selected
bag := Bag new. References
code.
bag add: 'this'; add: 'that'; add: 'that'.
1. Andrew Black, Stéphane Ducasse, Oscar Nierstrasz and • Debug it: Evaluate selected code step-by-step with the
bag occurrencesOf: 'that' −→ 2
bag remove: 'that'. Damien Pollet, Squeak by Example, Square Bracket Asso- integrated debugger.
bag occurrencesOf: 'that' −→ 1 ciates, 2007, squeakbyexample.org.
• Inspect it (CMD – I): Show an object inspector on the result
2. Chris Rathman, Terse guide to Squeak, wiki.squeak. of evaluating selected code.
" Dictionary – associates unique keys with objects " org/squeak/5699.
dictionary := Dictionary new. • Explore it (CMD – SHIFT– I): Show an object explorer on the
dictionary at: 'smalltalk' put: 80. 3. Smalltalk, Wikipedia, the free encyclopedia, en. result of evaluating selected code.
dictionary at: 'smalltalk' −→ 80 wikipedia.org/wiki/Smalltalk.
dictionary at: 'squeak' ifAbsent: [ 82 ] −→ 82
dictionary removeKey: 'smalltalk'.
dictionary isEmpty −→ true
2. The Language 3. Keyword messages take one or more arguments. Conditionals
2 raisedTo: 6 modulo: 10 sends the message named
• Everything is an object. 1 = 2 ifTrue: [ Transcript show: '1 is equal to 2' ].
raisedTo:modulo: with arguments 6 and 10 to the object 2.
1 = 2 ifFalse: [ Transcript show: '1 isn''t equal to 2' ].
• Everything happens by sending messages. Unary messages are sent first, then binary messages and
• Single inheritance. finally keyword messages: 100 factorial / 99 factorial = 100
2 raisedTo: 1 + 3 factorial −→ 128 ifTrue: [ Transcript show: 'condition evaluated to true' ]
• Methods are public. ifFalse: [ Beeper beep ].
Messages are sent left to right. Use parentheses to change
• Instance variables are private to objects. the order:
Loops
1 + 2 * 3 −→ 9
Keywords 1 + (2 * 3) −→ 7 " conditional iteration "
[ Sensor anyButtonPressed ]
• self, the receiver. whileFalse: [ "wait" ].
Syntax
• super, the receiver, method lookup starts in superclass.
• Comments pen := Pen newOnForm: Display.
• nil, the unique instance of the class UndefinedObject. "Comments are enclosed in double quotes" pen place: Sensor cursorPoint.
• true, the unique instance of the class True. [ Sensor anyButtonPressed ]
• Temporary Variables whileTrue: [ pen goto: Sensor cursorPoint ].
• false, the unique instance of the class False. | var |
| var1 var2 | " fixed iteration "
• thisContext, the current execution context.
• Assignment 180 timesRepeat: [
var := aStatement pen turn: 88.
Literals pen go: 250 ].
var1 := var2 := aStatement
• Integer • Statements 1 to: 100 do: [ :index |
123 aStatement1. aStatement2 pen go: index * 4.
2r1111011 (123 in binary) aStatement1. aStatement2. aStatement3 bic turn: 89 ].
16r7B (123 in hexadecimal)
• Messages " infinite loop (press CMD+. to break) "
• Float receiver message (unary message) [ pen goto: Sensor cursorPoint ] repeat.
123.4 receiver + argument (binary message)
1.23e--4 receiver message: argument (keyword message) Blocks (anonymous functions)
• Character receiver message: argument1 with: argument2
$a " evaluation "
• Cascade [ 1 + 2 ] value −→ 3
• String receiver message1; message2 [ :x | x + 2 ] value: 1 −→ 3
'abc' receiver message1; message2: arg2; message3: arg3 [ :x :y | x + y ] value: 1 value: 2 −→ 3
• Symbol • Blocks
[ aStatement1. aStatement2 ] " processes "
#abc [ (Delay forDuration: 5 seconds) wait.
[ :argument1 | aStatement1. aStatement2 ]
• Array Transcript show: 'done' ] fork −→ aProcess
[ :argument1 :argument2 | | temp1 temp2 | aStatement1 ]
#(123 123.4 $a 'abc' #abc)
• Return Statement Collections
^ aStatement
Message Sends " iterating "
'abc' do: [ :each | Transcript show: each ].
1. Unary messages take no argument. 'abc'
1 factorial sends the message factorial to the object 1. 3. Standard Classes
do: [ :each | Transcript show: each ]
2. Binary messages take exactly one argument. Logical expressions separatedBy: [ Transcript cr ].
3 + 4 sends message + with argument 4 to the object 3.
true not −→ false " transforming "
#answer --> 42 sends --> with argument 42 to #answer.
1 = 2 or: [ 2 = 1 ] −→ false #(1 2 3 4) select: [ :each | each even ] −→ #( 2 4 )
Binary selectors are built from one or more of the charac- 1 < 2 and: [ 2 > 1 ] −→ true #(1 2 3 4) reject: [ :each | each = 2 ] −→ #( 1 3 4 )
ters +, --, *, =, <, >, ...

You might also like