Skip to content

Commit

Permalink
Improvements to code style in the first part
Browse files Browse the repository at this point in the history
  • Loading branch information
codeZeilen committed Dec 2, 2020
1 parent 9d4cd61 commit 29d00a5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 43 deletions.
15 changes: 8 additions & 7 deletions FirstApp/FirstApp.tex
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,11 @@ \section{Adding methods to a class}
Sending \ct{self label: ''}, for example, sets the label of this object to the empty string.
\pvindex{self}

The expression \ct{0@0 corner: 16@16} probably needs some explanation.
\lct{0@0} represents a \clsind{Point} object with $x$ and $y$ coordinates both set to 0.
In fact, \ct{0@0} sends the message \ct{@} to the \clsind{Number} object \ct{0} with argument \ct{0}.
The expression \ct{0 @ 0 corner: 16 @ 16} probably needs some explanation.
\lct{0 @ 0} represents a \clsind{Point} object with $x$ and $y$ coordinates both set to 0.
In fact, \ct{0 @ 0} sends the message \ct{@} to the \clsind{Number} object \ct{0} with argument \ct{0}.
The effect will be that the number \ct{0} will ask the \ct{Point} class to create a new instance with coordinates $(0,0)$.
Now we send this newly created point the message \ct{corner: 16@16}, which causes it to create a \clsind{Rectangle} with corners \ct{0@0} and \ct{16@16}.
Now we send this newly created point the message \ct{corner: 16 @ 16}, which causes it to create a \clsind{Rectangle} with corners \ct{0 @ 0} and \ct{16 @ 16}.
We use the setter method \ct{bounds}, inherited from the superclass, to set the \ct{bounds} of the cell to the newly created rectangle.

Note that the origin of the \sq screen is the \emph{top left}, and the $y$ coordinate increases \emph{downward}.
Expand Down Expand Up @@ -285,7 +285,7 @@ \section{Inspecting an object}
The left-hand pane of the \ind{inspector} shows a list of instance variables; if you select one (try \mbox{\ct{bounds}),} the value of the \ind{instance variable} is shown in the right pane.
You can also use the inspector to change the value of an instance variable.

\dothis{Change the value of \ct{bounds} to \ct{0@0 corner: 50@50} and \menu{accept} it.}
\dothis{Change the value of \ct{bounds} to \ct{0 @ 0 corner: 50 @ 50} and \menu{accept} it.}

The bottom pane of the inspector is a mini-workspace.
It's useful because in this workspace the pseudo-variable \self is bound to the object being inspected.
Expand Down Expand Up @@ -438,7 +438,7 @@ \section{Defining the class SBEGame}
For example, it is pretty clear that \ct{Matrix rows: 5 columns: 2} has 5 rows and 2 columns, and not 2 rows and 5 columns.
\cmindex{Matrix class}{rows:columns:}

\ct{Matrix new: self cellsPerSide tabulate: [ :i :j | self newCellAt: i at: j ]} creates a new \ct{n}{$\times$}\ct{n} matrix and initializes its elements.
\ct{Matrix new: self cellsPerSide tabulate: [:i :j | self newCellAt: i at: j ]} creates a new \ct{n}{$\times$}\ct{n} matrix and initializes its elements.
The initial value of each element will depend on its coordinates.
The {$(i,j)$}\textsuperscript{th} element will be initialized to the result of evaluating \ct{self newCellAt: i at: j}.

Expand Down Expand Up @@ -507,7 +507,8 @@ \section{Organizing methods into Protocols}
\begin{method}[newCellAt:at:]{An initialization helper method}
SBEGame>>>newCellAt: i at: j
"Create a cell for position (i,j) and add it to my on-screen
representation at the appropriate screen position. Answer the new cell"
representation at the appropriate screen position. Answer the new cell."

| cell origin |
cell := SBECell new.
origin := self innerBounds origin.
Expand Down
32 changes: 16 additions & 16 deletions Messages/Messages.tex
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ \subsection{Binary messages}
Note that \ct{--} is not allowed for parsing reasons.

\begin{code}{@TEST}
100@100 --> 100@100 "creates a Point object"
100 @ 100 --> 100@100 "creates a Point object"
3 + 4 --> 7
10 - 1 --> 9
4 <= 3 --> false
(4/3) * 3 = 4 --> true "equality is just a binary message, and Fractions are exact"
(3/4) == (3/4) --> false "two equal Fractions are not the same object"
(4 / 3) * 3 = 4 --> true "equality is just a binary message, and Fractions are exact"
(3 / 4) == (3 / 4) --> false "two equal Fractions are not the same object"
\end{code}

\important{Binary messages are messages that require exactly one argument \emph{and} whose selector is composed of a sequence of characters from: \ct{+}, \ct{-}, \ct{*}, \ct{/}, \ct{\&}, \ct{=}, \ct{>}, \ct{|}, \ct{<}, \ct{\~}, and \ct{@}. \ct{--} is not possible.\\
Expand Down Expand Up @@ -281,7 +281,7 @@ \subsection*{Unary > Binary > Keywords}

The following example, which is a bit more complex (!), offers a nice illustration that even complicated \st expressions can be read naturally:
\begin{code}{@TEST}
[:aClass | aClass methodDict keys sorted select: [:aMethod | (aClass>>aMethod) isAbstract ]] value: Boolean --> #(#& #==> #and: #asBit #ifFalse: #ifFalse:ifTrue: #ifTrue: #ifTrue:ifFalse: #not #or: #|)
[:aClass | aClass methodDict keys sorted select: [:aMethod | (aClass>>aMethod) isAbstract]] value: Boolean --> #(#& #==> #and: #asBit #ifFalse: #ifFalse:ifTrue: #ifTrue: #ifTrue:ifFalse: #not #or: #|)
\end{code}
\noindent
Here we want to know which methods of the \ct{Boolean} class are abstract.
Expand Down Expand Up @@ -412,23 +412,23 @@ \subsection{Parentheses first}
% 'www.altavista.digital.com/av/pix/default/av-adv.gif') display

\paragraph{Example.}
The message \ct{(65@325 extent: 134 @ 100) center} returns the center of a rectangle whose top-left point is $(65, 325)$ and whose size is $134{\times}100$.
The message \ct{(65 @ 325 extent: 134 @ 100) center} returns the center of a rectangle whose top-left point is $(65, 325)$ and whose size is $134{\times}100$.
\Egref{decExtent} shows how the message is decomposed and sent.
First, the message between parentheses is sent:
It contains two binary messages \ct{65@325} and \ct{134@100} that are sent first and return points, and a keyword message \ct{extent:} which is then sent and returns a rectangle.
It contains two binary messages \ct{65 @ 325} and \ct{134 @ 100} that are sent first and return points, and a keyword message \ct{extent:} which is then sent and returns a rectangle.
Finally, the unary message \ct{center} is sent to the rectangle and a point is returned.
Evaluating the message without parentheses would lead to an error because the object \ct{100} does not understand the message \ct{center}.

\begin{example}[decExtent]{Example of Parentheses.}{}
(65@325 extent: 134@100) center
(1) 65@325 "binary"
(65 @ 325 extent: 134 @ 100) center
(1) 65 @ 325 "binary"
--> aPoint
(2) 134@100 "binary"
(2) 134 @ 100 "binary"
--> anotherPoint
(3) aPoint extent: anotherPoint "keyword"
--> aRectangle
(4) aRectangle center "unary"
--> 132@375
--> 132 @ 375
\end{example}

\subsection{From left to right}
Expand Down Expand Up @@ -462,10 +462,10 @@ \subsection{Arithmetic inconsistencies}
\begin{code}{@TEST}
3 + 4 * 5 --> 35 "(not 23) Binary messages sent from left to right"
3 + (4 * 5) --> 23
1 + 1/3 --> (2/3) "and not 4/3"
1 + (1/3) --> (4/3)
1/3 + 2/3 --> (7/9) "and not 1"
(1/3) + (2/3) --> 1
1 + 1 / 3 --> (2 / 3) "and not 4/3"
1 + (1 / 3) --> (4 / 3)
1 / 3 + 2 / 3 --> (7 / 9) "and not 1"
(1 / 3) + (2 / 3) --> 1
\end{code}

\paragraph{Example.}
Expand Down Expand Up @@ -622,8 +622,8 @@ \section{Expression sequences}
\begin{code}{@TEST}
| box |
box := 20@30 corner: 60@90.
box containsPoint: 40@50 --> true
box := 20 @ 30 corner: 60 @ 90.
box containsPoint: 40 @ 50 --> true
\end{code}
%=============================================================
Expand Down
13 changes: 7 additions & 6 deletions QuickTour/QuickTour.tex
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ \section{Workspaces and Transcripts}

\dothis{Type the following text into the workspace:}
\begin{code}{}
Transcript showln: 'hello world'.
Transcript show: 'hello world'; cr.
\end{code}

Try double-clicking in the workspace at various points in the text you have just typed.
Expand All @@ -414,13 +414,13 @@ \section{Workspaces and Transcripts}
%%{SBESqueakPicture writeTo: './figures/Doit.png' frame: [:morphsRect | morphsRect withBottom: morphsRect bottom - 150] using: [:helper |
%% | workspace |
%% ToolBuilder open: Transcript.
%% Transcript show: 'hello world'.
%% Transcript show: 'hello world'; cr.
%% helper foregroundWindow bounds: (0 @ 120 extent: 450 @ 230).
%%
%% workspace := ToolBuilder open: Workspace new.
%% workspace position: 200 @ 100.
%%
%% helper type: 'Transcript show: ''hello world''' into: workspace.
%% helper type: 'Transcript show: ''hello world''; cr.' into: workspace.
%% helper keyStroke: workspace key: $a modifiers: #(cmd).
%%
%% helper click: workspace at: workspace topCenter + (0 @ 30) buttons: #(right).
Expand Down Expand Up @@ -468,7 +468,7 @@ \section{Keyboard shortcuts}
If you want to see the result, you should \menu{print it} instead.
\menu{print it} actually compiles the expression, executes it, sends the message \ct{printString} to the result, and displays the resulting string.

\dothis{Select \ct{3+4} and \menu{print it} (\short{p}).}
\dothis{Select \ct{3 + 4} and \menu{print it} (\short{p}).}
This time we see the result we expect (\figref{printit}).
\index{keyboard shortcut!print it}

Expand All @@ -492,7 +492,7 @@ \section{Keyboard shortcuts}
We use the notation \ct{-->} as a convention in this book to indicate that a particular \sq expression yields a given result when you \menu{print it}.

\dothis{Delete the highlighted text ``\ct{7}'' (\sq should have selected it for you, so you can just press the delete key).
Select \ct{3+4} again and this time \menu{inspect it} (\short{i}).}
Select \ct{3 + 4} again and this time \menu{inspect it} (\short{i}).}
\noindent
Now you should see a new window, called an \emphind{inspector}, with the heading \ct{SmallInteger: 7} (\figref{inspectit}).
The inspector is an extremely useful tool that will allow you to browse and interact with any object in the system.
Expand Down Expand Up @@ -938,7 +938,8 @@ \section{Defining a new method}
\dothis{Select class \clsind{String} in the system browser, select the \menu{converting} category, type the text in \mthref{shout} over the method creation template, and \menu{accept} it.}
\begin{method}[shout]{The shout method}
shout
^ self asUppercase, 'BANG'

^ self asUppercase , 'BANG'
\end{method}

The comma is the string concatenation operation, so the body of this method appends an exclamation mark to an upper-case version of whatever \ct{String} object the \ct{shout} message was sent to.
Expand Down
30 changes: 16 additions & 14 deletions Syntax/Syntax.tex
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@ \section{Syntactic elements}
\lct{\$a} & the character `a' \\
\lct{'Hello'} & the string ``Hello'' \\
\lct{\#Hello} & the symbol \lct{\#Hello} \\
\lct{\{1. 2. 1+2\}} & a dynamic array \\
\lct{\{1 . 2 . 1 + 2\}} & a dynamic array \\
\lct{\#(1 2 3)} & a literal array \\
\lct{\#[255 33 200 1]} & a byte array \\
\midrule
\lct{"a comment"} & a comment \\
\midrule
\lct{| x y |} & declaration of variables \lct{x} and \lct{y} \\
\lct{|x y|} & declaration of variables \lct{x} and \lct{y} \\
\lct{x := 1} & assign 1 to \lct{x} \\
\lct{[x + y]} & a block that evaluates to \lct{x+y} \\
\lct{<primitive: 1>} & primitive pragma to invoke virtual machine \\
\midrule
\lct{3 factorial} & unary message \\
\lct{3+4} & binary messages \\
\lct{3 + 4} & binary messages \\
\lct{2 raisedTo: 6 modulo: 10} & keyword message \\
\midrule
\lct{\textasciicircum true} & return the value true \\
Expand Down Expand Up @@ -151,7 +151,7 @@ \section{Syntactic elements}
\item[Run-time arrays.]
Curly braces \ct|{ }| define a (\subind{Array}{dynamic}) array at run-time.
Elements are expressions separated by periods.
So \ct|{ 1. 2. 1+2 }| defines an array with elements 1, 2, and the result of evaluating 1+2.
So \ct|{1 . 2 . 1 + 2}| defines an array with elements 1, 2, and the result of evaluating 1+2.
(The curly-brace notation is peculiar to the \sq dialect of \st!
In other \st{}s you must build up dynamic arrays explicitly.)

Expand Down Expand Up @@ -205,7 +205,7 @@ \section{Syntactic elements}
\seeindex{unary message}{message, unary}

\item[Binary messages] are operators (like \ct{+}) sent to a receiver and taking a single argument.
In \ct{3+4}, the receiver is \ct{3} and the argument is \ct{4}.
In \ct{3 + 4}, the receiver is \ct{3} and the argument is \ct{4}.
\index{message!binary}
\seeindex{binary message}{message, binary}

Expand Down Expand Up @@ -348,11 +348,12 @@ \section{Method syntax}
String>>>lineCount
"Answer the number of lines represented by the receiver,
where every cr adds one line."

| cr count |
cr := Character cr.
count := 1 min: self size.
self do: [:c |
c = cr ifTrue: [count := count + 1]].
self do: [:eachChar |
eachChar = cr ifTrue: [count := count + 1]].
^ count
\end{method}

Expand Down Expand Up @@ -407,7 +408,7 @@ \section{Block syntax}
\index{variable!declaration}

\begin{code}{@TEST}
[:x :y | | z | z := x+ y. z] value: 1 value: 2 --> 3
[:x :y | | z | z := x + y. z] value: 1 value: 2 --> 3
\end{code}

Blocks are actually lexical \emph{closures} since they can refer to variables of the surrounding environment.
Expand All @@ -431,6 +432,7 @@ \section{Block syntax}
\begin{method}[detectReturnBlock]{The method \ct{detect:ifFound:ifNone:} illustrating how returning inside blocks can be used}
Collection>>>detect: aBlock ifFound: foundBlock ifNone: exceptionBlock
"foundBlock takes one argument, the found object."

self do: [:element |
(aBlock value: element) ifTrue: [^ foundBlock value: element]].
^ exceptionBlock value
Expand Down Expand Up @@ -463,7 +465,7 @@ \section{Conditionals and loops in a nutshell}

\begin{code}{@TEST | n |}
n := 1.
[n < 1000] whileTrue: [n := n*2].
[n < 1000] whileTrue: [n := n * 2].
n --> 1024
\end{code}
\cmindex{BlockClosure}{whileTrue:}
Expand All @@ -472,7 +474,7 @@ \section{Conditionals and loops in a nutshell}
\mthind{BlockClosure}{whileFalse:} reverses the exit condition.
\begin{code}{@TEST | n |}
n := 1.
[n > 1000] whileFalse: [n := n*2].
[n > 1000] whileFalse: [n := n * 2].
n --> 1024
\end{code}

Expand All @@ -481,7 +483,7 @@ \section{Conditionals and loops in a nutshell}

\begin{code}{@TEST | n |}
n := 1.
10 timesRepeat: [n := n*2].
10 timesRepeat: [n := n * 2].
n --> 1024
\end{code}

Expand All @@ -491,7 +493,7 @@ \section{Conditionals and loops in a nutshell}
\needlines{4}
\begin{code}{@TEST | result |}
result := String new.
1 to: 10 do: [:n | result := result, n printString, ' '].
1 to: 10 do: [:n | result := result , n printString , ' '].
result --> '1 2 3 4 5 6 7 8 9 10 '
\end{code}

Expand All @@ -508,7 +510,7 @@ \section{Conditionals and loops in a nutshell}

\begin{code}{@TEST | result |}
result := String new.
(1 to: 10) do: [:n | result := result, n printString, ' '].
(1 to: 10) do: [:n | result := result , n printString , ' '].
result --> '1 2 3 4 5 6 7 8 9 10 '
\end{code}

Expand Down Expand Up @@ -625,7 +627,7 @@ \section{Chapter summary}
\item Local variables are declared with vertical bars.
Use \ct{:=} for assignment.
\ct{| x | x:=1}
\ct{| x | x := 1}
\item Expressions consist of message sends, cascades and assignments, possibly grouped with parentheses.
\emph{Statements} are expressions separated by periods.
Expand Down

0 comments on commit 29d00a5

Please sign in to comment.