Friday 23 October 2015

Pharo By Example, Ch. 3

Compile-time arrays are defined by #( ), surrounding space-separated literals. Everything within the parentheses must be a compile-time constant. For example, #(27 (true false) abc) is a literal array of three elements: the integer 27, the compile-time array containing the two booleans, and the symbol #abc. (Note that this is the same as #(27 #(true false) #abc).)

i.e. Compile-time arrays must contain unique objects - symbols or constants

Only run-time arrays can contain object instances like strings.

Run-time arrays. Curly braces { } define a (dynamic) array at run-time. Elements are expressions separated by periods. So { 1. 2. 1+2 } defines an array with elements 1, 2, and the result of evaluating 1+2.

The curlybrace notation is peculiar to the Pharo and Squeak dialects of Smalltalk! In other Smalltalks you must build up dynamic arrays explicitly.

How do you build up dynamic arrays explicitly?

-----

Local variable definitions. Vertical bars | | enclose the declaration of one or more local variables in a method (and also in a block).

-----

There are 6 reserved keywords, or pseudo-variables: nil, true, false, self, super, and thisContext.

You cannot assign a value to a pseudovariable.

self - the receiver of the currently executing method
super - the superclass of the receiver
(technically, the receiver, but doing method-lookup starting from the receiver's superclass)
nil - the undefined object. It is the unique instance of the class UndefinedObject. Instance variables, class variables and local variables are initialized to nil.
thisContext is a pseudo-variable that represents the top frame of the runtime stack. In other words, it represents the currently executing MethodContext or BlockClosure. thisContext is normally not of interest to most programmers, Message sends 53 but it is essential for implementing development tools like the debugger and it is also used to implement exception handling and continuations.
true and false are the unique instances of the Boolean classes True and False.

-----

Blocks

Code contained between [ and ]

They return the value of the last evaluated expression. (unless there is an explicit return (with ^), in which case it does not answer a value)

Blocks may take parameters. Each parameter is declared with a leading colon.

A vertical bar separates the parameter declaration(s) from the body of the block.

For a block with one parameter, you must send it the message value: with one argument.
For a block with two parameters, you must send it the message value:value:, each with one argument. with two argument.
For a block with three parameters, you must send it the message value:value:value:, each with one argument. 
For a block with four parameters,  you must send it the message value:value:value:value:, each with one argument. 
For a block with more than four parameters, you must send it the message valueWithArguments: and pass the arguments in an array.

A block with a large array of parameters is often the sign of a design issue.

Blocks may also declare local variables, which are surrounded by vertical bars, just like local variable declarations in a method. Locals are declared after any arguments, e.g.
[ :x :y | | z | z := x+ y. z ] value: 1 value: 2
→ 3

Blocks are instances of the class BlockClosure. This means that they are objects, so they can be assigned to variables and passed as arguments just like any other object.

-----

Loops and controls

control constructs are typically expressed by sending messages to booleans, numbers and collections, with blocks as arguments.

(condition)
ifTrue: [  ]
ifFalse: [  ]

[ ] whileTrue: [ ]
[ ] whileFalse: [ ]

nn timesRepeat: [ ]


Iterators

(nn to: nn) do: [ :x | result := result, x printString, ' ' ]

(nn: to nn2:) is a collection - a dynamic array
1 to 10 would give
result -> '1 2 3 4 5 6 7 8 9 10'


= has the same value as

== exactly equal to

(or else it's == and === )

[ n < 1000 ] whileTrue: [ n := n* 2 ].

collect: 

builds a new collection of the same size, transforming each element.

(1 to: 10) collect: [ :each | each * each ]

-> #(1 4 9 16 25 36 49 64 81 100)

select: and reject:
build subset collections

detect:
 returns the first element matching the condition
-----






No comments:

Post a Comment