Friday 16 October 2015

Starting again, part 2



Basic loops
Loops are high-level collection iterators, implemented as regular methods.  

  to:do:
  to:by:do"

Transcript open.

1 to: 100 do:
  [:i | Transcript show: i asString; cr ]. 

1 to: 100 by: 3 do: [:i | Transcript show: i asString; cr]. 

Iterators
The message do: is sent to a collection of objects (Array, Set, OrderedCollection), evaluating the block for each element.

e.g.
#(11 38 3 -2 10) do: [:each |
     Transcript show: each printString; cr].  "do for each element in the array #(11 38 3 -2 10) and print to the Transcript"

"Some other really nice iterators"

#(11 38 3 -2 10) collect: [:each | each abs].  "collect means do for each and every member ?"

#(11 38 3 -2 10) collect: [:each | each odd].

#(11 38 3 -2 10) select: [:each | each odd].  "add each member that meets the criterion to a new collection?  Or to the subject of the message?"

#(11 38 3 -2 10) select: [:each | each > 10].

#(11 38 3 -2 10) reject: [:each | each > 10]. "remove each member that meets the criterion from the collection?  Or creates a new collection with the subset?"

#(11 38 3 -2 10)
     do: [:each | Transcript show: each printString]
     separatedBy: [Transcript show: '.'].

explore
All objects understand this message.  It opens an information window about the object.  All it's properties.

browse
brings up the Package Browser, which is the Class Browser plus info about packages (groups of classes)






No comments:

Post a Comment