Fixing tabs/spaces as per feedback received & some other minor changes

Fixing tabs/spaces as per feedback received & some other minor changes

```
Hi,
In case you still want feedback, when I open the tutorial in vanilla Chrome, the boxes with the examples wrap badly.  It looks as if a tab stop is too far to the right. It doesn't affect all the boxes equally -- the one about Constants is mostly OK.
In other feedback, that's exactly the level of tutorial I find helpful, so thanks for writing it!
```
This commit is contained in:
Jigyasa Grover 2019-01-04 07:59:16 -08:00 committed by GitHub
parent d08c4a7f79
commit 57524943c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,7 @@
language: Smalltalk
filename: smalltalk.st
contributors:
- ["Jigyasa Grover", "https://github.com/jig08"]
- ["Jigyasa Grover", "https://github.com/jigyasa-grover"]
- ["tim Rowledge", "tim@rowledge.org"]
---
@ -10,18 +10,19 @@ contributors:
- Smalltalk was created as the language to underpin the "new world" of computing exemplified by "humancomputer symbiosis."
- It was designed and created in part for educational use, more so for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan Kay, Dan Ingalls, Adele Goldberg, Ted Kaehler, Scott Wallace, and others during the 1970s.
Feedback highly appreciated! Reach me at [@jigyasa_grover](https://twitter.com/jigyasa_grover) or send me an e-mail at `grover.jigyasa1@gmail.com`.
`Feedback highly appreciated! Reach me at [@jigyasa_grover](https://twitter.com/jigyasa_grover) or send me an e-mail at grover.jigyasa1@gmail.com.`
## The Basics
### Everything is an object
Yes, everything. Integers are instances of one of the numeric classes. Classes are instances of the class Metaclass and are just as manipulable as any other object. All classes are part of a single class tree; no disjoint class trees. Stack frames are objects and can be manipulated, which is how the debugger works. There are no pointers into memory locations that you can dereference and mess with.
### Functions are not called; messages are sent to objects
Work is done by sending messages to objects, which decide how to respond to that message and run a method as a result, which eventually returns some object to the original message sending code.
The system knows the class of the object receiving a message and looks up the message in that class's list of methods. If it is not found, the lookup continues in the super class until either it is found or the root of the classes is reached and there is still no relevant method.
If a suitable method is found the code is run, and the same process keeps on going with all the methods sent by that method and so on forever.
If no suitable method is found an exception is raised, which typically results in a user interface notifier to tell the user that the message was not understood. It is entirely possible to catch the exception and do something to fix the problem, which might range from 'ignore it' to 'load some new packages for this class and try again'.
A method (more strictly an instance of the class CompiledMethod) is a chunk of Smalltalk code that has been compiled into bytecodes. Executing methods start at the beginning and return to the sender when a return is encountered (we use ^ to signify 'return the follwing object') or the end of the code is reached, in which case the current object running the code is returned.
- Work is done by sending messages to objects, which decide how to respond to that message and run a method as a result, which eventually returns some object to the original message sending code.
- The system knows the class of the object receiving a message and looks up the message in that class's list of methods. If it is not found, the lookup continues in the super class until either it is found or the root of the classes is reached and there is still no relevant method.
- If a suitable method is found the code is run, and the same process keeps on going with all the methods sent by that method and so on forever.
- If no suitable method is found an exception is raised, which typically results in a user interface notifier to tell the user that the message was not understood. It is entirely possible to catch the exception and do something to fix the problem, which might range from 'ignore it' to 'load some new packages for this class and try again'.
- A method (more strictly an instance of the class CompiledMethod) is a chunk of Smalltalk code that has been compiled into bytecodes. Executing methods start at the beginning and return to the sender when a return is encountered (we use ^ to signify 'return the follwing object') or the end of the code is reached, in which case the current object running the code is returned.
### Simple syntax
Smalltalk has a simple syntax with very few rules.
@ -55,7 +56,9 @@ self size > 4 ifTrue: [^argumentObject sizeRelatingTo: self].
```
Everything here except the `^` involves sending more messages. Event the `ifTrue:` that you might think is a language control structure is just Smalltalk code.
We start by sending `size` to `self`. `self` is the object currently running the code - so in this case it is the myObject we started with. `size` is a very common message that we might anticipate tells us something about how big an object is; you could look it up with the Smalltalk tools very simply. The result we get is then sent the message `>` with the plain old integer 4 (which is an object too; no strange primitive types to pollute the system here) and nobody should be surprised the `>` is a comparison that answers true or false. That boolean (which is actually a Boolean object in Smalltalk) is sent the message `ifTrue:` with the block of code between the `[]` as its argument; obvioulsy a true boolean might be expected to run that block of code and a false to ignore it.
If the block is run then we do some more message sending to the argument object and noting the `^` we return the answer back to our starting point and it gets assigned to `result`. If the block is ignored we seem to run out of code and so `self` is returned and assigned to `result`.
## Smalltalk quick reference cheat-sheet
@ -218,8 +221,7 @@ x := 28 gcd: 12. "greatest common den
x := 28 lcm: 12. "least common multiple"
x := 100 ln. "natural logarithm"
x := 100 log. "base 10 logarithm"
x := 100 log: 10. "logarithm with specified base"
x := 100 floorLog: 10. "floor of the log"
x := 100 log: 10. "floor of the log"
x := 180 degreesToRadians. "convert degrees to radians"
x := 3.14 radiansToDegrees. "convert radians to degrees"
x := 0.7 sin. "sine"
@ -284,6 +286,7 @@ x := 15 storeStringBase: 16.
x := [ y := 1. z := 2. ]. x value. "simple block usage"
x := [ :argOne :argTwo | argOne, ' and ' , argTwo.]. "set up block with argument passing"
Transcript show: (x value: 'First' value: 'Second'); cr. "use block with argument passing"
"x := [ | z | z := 1.]. *** localvars not available in squeak blocks"
```
@ -320,10 +323,14 @@ x := 3 + 2; * 100. "result=300. Sends m
| x |
x > 10 ifTrue: [Transcript show: 'ifTrue'; cr]. "if then"
x > 10 ifFalse: [Transcript show: 'ifFalse'; cr]. "if else"
x > 10 "if then else"
"if then else"
x > 10
ifTrue: [Transcript show: 'ifTrue'; cr]
ifFalse: [Transcript show: 'ifFalse'; cr].
x > 10 "if else then"
"if else then"
x > 10
ifFalse: [Transcript show: 'ifFalse'; cr]
ifTrue: [Transcript show: 'ifTrue'; cr].
Transcript
@ -332,7 +339,9 @@ Transcript
ifTrue: ['ifTrue']
ifFalse: ['ifFalse']);
cr.
Transcript "nested if then else"
"nested if then else"
Transcript
show:
(x > 10
ifTrue: [x > 5
@ -340,7 +349,9 @@ Transcript "nested if then else
ifFalse: ['B']]
ifFalse: ['C']);
cr.
switch := Dictionary new. "switch functionality"
"switch functionality"
switch := Dictionary new.
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].
@ -433,7 +444,6 @@ y := x shuffled. "randomly shuffle st
#### Array:
Fixed length collection
- ByteArray: Array limited to byte elements (0-255)
- WordArray: Array limited to word elements (0-2^32)
@ -461,7 +471,8 @@ y := x reject: [:a | a < 2]. "return collection o
y := x collect: [:a | a + a]. "transform each element for new collection"
y := x detect: [:a | a > 3] ifNone: []. "find position of first element that passes test"
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 := 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 array"
ifTrue: [a]
@ -481,7 +492,8 @@ acts like an expandable array
```
| b x y sum max |
x := OrderedCollection with: 4 with: 3 with: 2 with: 1. "create collection with up to 4 elements"
x := OrderedCollection
with: 4 with: 3 with: 2 with: 1. "create collection with up to 4 elements"
x := OrderedCollection new. "allocate collection"
x add: 3; add: 2; add: 1; add: 4; yourself. "add element to collection"
y := x addFirst: 5. "add element at beginning of collection"
@ -508,7 +520,8 @@ y := x reject: [:a | a < 2]. "return collection o
y := x collect: [:a | a + a]. "transform each element for new collection"
y := x detect: [:a | a > 3] ifNone: []. "find position of first element that passes test"
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 := 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 collection"
ifTrue: [a]
@ -526,7 +539,8 @@ like OrderedCollection except order of elements determined by sorting criteria
```
| b x y sum max |
x := SortedCollection with: 4 with: 3 with: 2 with: 1. "create collection with up to 4 elements"
x := SortedCollection
with: 4 with: 3 with: 2 with: 1. "create collection with up to 4 elements"
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 collection"
@ -553,7 +567,8 @@ y := x reject: [:a | a < 2]. "return collection o
y := x collect: [:a | a + a]. "transform each element for new collection"
y := x detect: [:a | a > 3] ifNone: []. "find position of first element that passes test"
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 := 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 collection"
ifTrue: [a]
@ -650,7 +665,8 @@ y := x reject: [:a | a < 2]. "return collection o
y := x collect: [:a | a + a]. "transform each element for new collection"
y := x detect: [:a | a > 3] ifNone: []. "find position of first element that passes test"
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 := 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 collection"
ifTrue: [a]
@ -677,7 +693,10 @@ uses identity test (== rather than =)
```
| b x y |
x := Dictionary new. "allocate collection"
x add: #a->4; add: #b->3; add: #c->1; add: #d->2; yourself. "add element to collection"
x add: #a->4;
add: #b->3;
add: #c->1;
add: #d->2; yourself. "add element to collection"
x at: #e put: 3. "set element at index"
b := x isEmpty. "test if empty"
y := x size. "number of elements"
@ -742,8 +761,7 @@ Smalltalk removeKey: #CMRDictionary ifAbsent: []. "remove user diction
| 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].
[(x := ios nextLine) notNil] whileTrue: [Transcript show: x; cr].
ios position: 3.
ios position.
x := ios next.
@ -756,8 +774,7 @@ 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].
[(x := ios nextLine) notNil] whileTrue: [Transcript show: x; cr].
ios position: 6.
ios position.
ios nextPutAll: 'Chris'.
@ -778,8 +795,7 @@ ios nextPutAll: 'Hello File'; cr.
ios close.
ios := FileStream oldFileNamed: 'ios.txt'.
[(x := ios nextLine) notNil]
whileTrue: [Transcript show: x; cr].
[(x := ios nextLine) notNil] whileTrue: [Transcript show: x; cr].
ios position: 3.
x := ios position.
x := ios next.
@ -891,7 +907,8 @@ myPen go: 50. "move pen specified
myPen location. "get the pen position"
myPen goto: 200@200. "move to specified point"
myPen place: 250@250. "move to specified point without drawing"
myPen print: 'Hello World' withFont: (TextStyle default fontAt: 1).
myPen print: 'Hello World'
withFont: (TextStyle default fontAt: 1).
Display extent. "get display width@height"
Display width. "get display width"
Display height. "get display height"
@ -902,6 +919,7 @@ Display height. "get display height"
#### Dynamic Message Calling/Compiling:
```
| receiver message result argument keyword1 keyword2 argument1 argument2 |
"unary message"
receiver := 5.
message := 'factorial' asSymbol.
@ -923,11 +941,14 @@ 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) , ' ', keyword2, (argument2 storeString)).
result := (Message
new
setSelector: (keyword1, keyword2) asSymbol
@ -935,7 +956,7 @@ result := (Message
sentTo: receiver.
```
#### Class/Meta-class:
#### Class/Meta-Class:
```
| b x |
x := String name. "class name"
@ -991,7 +1012,7 @@ a := 'A1'. b := 'B2'. a become: b. "switch two objects"
Transcript show: a, b; cr.
```
#### Misc
#### Miscellaneous
```
| x |
"Smalltalk condenseChanges." "compress the change file"
@ -1016,7 +1037,7 @@ Most Smalltalks are either free as in OSS or have a free downloadable version wi
* [Smalltalk-72 Manual](http://www.bitsavers.org/pdf/xerox/parc/techReports/Smalltalk-72_Instruction_Manual_Mar76.pdf)
* [GNU Smalltalk User's Guide](https://www.gnu.org/software/smalltalk/manual/html_node/Tutorial.html)
#### Historical doc
#### Historical Documentation(s)
* [BYTE: A Special issue on Smalltalk](https://archive.org/details/byte-magazine-1981-08)
* [Smalltalk-72 Manual](http://www.bitsavers.org/pdf/xerox/parc/techReports/Smalltalk-72_Instruction_Manual_Mar76.pdf)
* [Smalltalk, Objects, and Design](https://books.google.co.in/books?id=W8_Une9cbbgC&printsec=frontcover&dq=smalltalk&hl=en&sa=X&ved=0CCIQ6AEwAWoVChMIw63Vo6CpyAIV0HGOCh3S2Alf#v=onepage&q=smalltalk&f=false)