Basic Objects Conditionals and Loops Booleans Basic Loops

Basic Objects, Conditionals and Loops Booleans • Basic Loops • Overview of the Collection hierarchy— more than 80 classes: (Bag, Array, Ordered. Collection, Sorted. Collection, Set, Dictionary. . . ) • Loops and Iteration abstractions • Common object behavior • Stéphane Ducasse «Chapter. Nr» . 1

Booleans Stéphane Ducasse «Chapter. Nr» . 2

Boolean Objects false and true are objects described by classes Boolean, True and False • Uniform, but optimized and inlined (macro expansion at compile time) • Logical Comparisons &, |, xor: , not a. Boolean. Expr comparison another. Boolean. Expr (1 is. Zero) & false • Stéphane Ducasse «Chapter. Nr» . 3

Boolean Hierarchy Please open your browser and analyse it • How to implement in OO true and false without conditional? • Booleean not if. True: True not if. True: Stéphane Ducasse False not if. True: «Chapter. Nr» . 4

Boolean Lazy Logical Operators • Lazy Logical operators a. Boolean. Expr and: and. Block will only be valued if a. Boolean. Expression is true a. Boolean. Expression or: or. Block will only be valued if a. Boolean. Expression is false and: [1 error: 'crazy'] Pr. It-> false and not an error Stéphane Ducasse «Chapter. Nr» . 5

Conditional: Messages to Boolean • • • a. Boolean if. True: a. True. Block if. False: a. False. Block a. Boolean if. False: a. False. Block if. True: a. True. Block a. Boolean if. False: a. False. Block Hint: Take care — true is the boolean value and True is the class of true, its unique instance! Stéphane Ducasse «Chapter. Nr» . 6

Why Block Use in Conditional • Why do conditional expressions use blocks? • Because, when a message is sent, the receiver and the arguments of the message are evaluated. Blocks are necessary to avoid evaluating both branches. Stéphane Ducasse «Chapter. Nr» . 7

Some Basic Loops a. Block. Test while. True • a. Block. Test while. False • a. Block. Test while. True: a. Block. Body • a. Block. Test while. False: a. Block. Body • an. Integer times. Repeat: a. Block. Body • [x<y] while. True: [x : = x + 3] 10 times. Repeat: [ Transcript show: 'hello'; cr] Stéphane Ducasse «Chapter. Nr» . 8

For the Curious. . . Block. Closure>>while. True: a. Block ^ self value if. True: [a. Block value. self while. True: a. Block] Block. Closure>>while. True ^ [self value] while. True: [] Stéphane Ducasse «Chapter. Nr» . 9

For the Curious… Integer>>times. Repeat: a. Block "Evaluate the argument, a. Block, the number of times represented by the receiver. " | count : = 1. [count <= self] while. True: [a. Block value. count : = count + 1] Stéphane Ducasse «Chapter. Nr» . 10

Collections Stéphane Ducasse «Chapter. Nr» . 11

Collections • Some criteria to identify them – Access: indexed, sequential or key-based. – Size: fixed or dynamic. – Element type: any or well-defined type. – Order: defined, defineable or none. – Duplicates: possible or not Stéphane Ducasse «Chapter. Nr» . 12

Essential Collection Sequenceable Arrayed. Collection Array Character. Array String Integer. Array Interval Linked. List Ordered. Collection Sorted. Collection Bag Set Identity. Set Dictionary Identity. Dictionary Stéphane Ducasse ordered fixed size + key = integer any kind of elements = character arithmetique progression dynamic chaining of the element size dynamic + arrival order explicit order possible duplicate + no order no duplicate + no order identification based on identity element = associations + key based on identity «Chapter. Nr» . 13

Essential Collections: Another View Stéphane Ducasse «Chapter. Nr» . 14

Some Collection Methods • • Will be defined, redefined, optimized or forbidden in the subclasses Accessing: #size, #capacity, #at: an. Integer put: an. Element Testing: #is. Empty, #includes: an. Element, #contains: a. Block, occurences. Of: an. Element Adding: #add: an. Element, #add. All: a. Collection Removing: #remove: an. Element, #remove: an. Element if. Absent: a. Block, #remove. All: a. Collection Enumerating (See generic enumerating): #do: a. Block, #collect: a. Block, #select: a. Block, #reject: a. Block, #detect: a. Block if. None: a. None. Block, #inject: avalue into: a. Binary. Block Converting: #as. Bag, #as. Set, #as. Ordered. Collection, #as. Sorted. Collection, #as. Array, #as. Sorted. Collection: a. Block Creation: #with: an. Element, #with: with: , #with: All: a. Collection Stéphane Ducasse «Chapter. Nr» . 15

Sequenceable Specific (Array) |arr| arr : = #(calvin hates suzie). arr at: 2 put: #loves. arr Pr. It-> #(#calvin #loves #suzie) • • Accessing: #first, #last, #at. All. Put: an. Element, #at. All: an. Index. Collection: put: an. Element Searching (*: + if. Absent: ): #index. Of: an. Element, #index. Of: an. Element if. Absent: a. Block Changing: #replace. All: an. Element with: another. Element Copying: #copy. From: first to: last, copy. With: an. Element, copy. Without: an. Element Stéphane Ducasse «Chapter. Nr» . 16

Keyed. Collection Specific (Dictionary) |dict| dict : = Dictionary new. dict at: 'toto' put: 3. dict at: 'titi' if. Absent: [4]. -> 4 dict at: 'titi' put: 5. dict remove. Key: 'toto'. dict keys -> Set ('titi') • • Accessing: #at: a. Key, #at: a. Key if. Absent: a. Block, #at: a. Key if. Absent. Put: a. Block, #at: a. Key put: a. Value, #keys, #values, #associations Removing: #remove. Key: a. Key, #remove. Key: a. Key if. Absent: a. Block Testing: #include. Key: a. Key Enumerating: #keys. And. Values. Do: a. Block, #associations. Do: a. Block, #keys. Do: a. Block Stéphane Ducasse «Chapter. Nr» . 17

Choose your Camp! To get all the absolute values of numbers you could write: absolute: a. Col |result| result : = a. Col species new: a. Col size. 1 to: a. Collection size do: [ : each | result at: each put: (a. Col at: each) abs]. ^ result Stéphane Ducasse «Chapter. Nr» . 18

Choose your Camp • You could also write: absolute: a. Collection ^ a. Collection collect: [: each| each abs] • Really important: Contrary to the first solution, the second solution works well for indexable collections and also for sets. Stéphane Ducasse «Chapter. Nr» . 19

Iteration Abstraction: do: /collect: • • • a. Collection do: a. One. Parameter. Block a. Collection collect: a. One. Parameter. Block a. Collection with: another. Collection do: a. Binary. Block #(15 10 19 68) do: [: i | Transcript show: i print. String ; cr ] #(15 10 19 68) collect: [: i | i odd ] Pr. It-> #(true false) #(1 2 3) with: #(10 20 30) do: [: x : y| Transcript show: (y ** x) print. String ; cr ] Stéphane Ducasse «Chapter. Nr» . 20

select: /reject: /detect: a. Collection select: a. Predicate. Block a. Collection reject: a. Predicate. Block a. Collection detect: a. One. Parameter. Predicate. Block if. None: a. None. Block • • #(15 10 19 68) select: [: i|i odd] -> #(15 19) #(15 10 19 68) reject: [: i|i odd] -> #(10 68) • • #(12 10 19 68 21) detect: [: i|i odd] Pr. It-> 19 #(12 10 12 68) detect: [: i|i odd] if. None: [1] Pr. It-> 1 Stéphane Ducasse «Chapter. Nr» . 21

inject: into: a. Collection inject: a. Start. Value into: a. Binary. Block |acc| acc : = 0. #(1 2 3 4 5) do: [: element | acc : = acc + element]. acc -> 15 Is equivalent to #(1 2 3 4 5) inject: 0 into: [: acc : element| acc + element] -> 15 • Do not use it if the resulting code is not crystal clear! Stéphane Ducasse «Chapter. Nr» . 22

Other Collections Important Methods a. Collection includes: an. Element • a. Collection size • a. Collection is. Empty • a. Collection contains: a. Boolean. Block • #(1 2 3 4 5) includes: 4 -> true • #(1 2 3 4 5) size -> 5 • #(1 2 3 4 5) is. Empty -> false • #(1 2 3 4 5) contains: [: each | each is. Odd] -> true • Stéphane Ducasse «Chapter. Nr» . 23

Common Shared Behavior Stéphane Ducasse «Chapter. Nr» . 24

Common Shared Behavior Object is the root of the inheritance tree • Defines the common and minimal behavior for all the objects in the system. • • Comparison of objects: #==, #~~, #=~, #is. Nil, #not. Nil Stéphane Ducasse «Chapter. Nr» . 25

Identity vs. Equality • • • = an. Object returns true if the structures are equivalent (the same hash number) (Array with: 1 with: 2) = (Array with: 1 with: 2) Pr. It-> true == an. Object returns true if the receiver and the argument point to the same object. #== should never be overridden. On Object>>= an. Object ^ self == an. Object ~= is not = ~~ is not == (Array with: 1 with: 2 ) == (Array with: 1 with: 2) Pr. It-> false (Array with: 1 with: 2 ) = (Array with: 1 with: 2) Pr. It-> true • Take care when redefining #=. One should override #hash too! Stéphane Ducasse «Chapter. Nr» . 26

Common Behavior: Printing • Print and store objects: #print. String, #print. On: a. Stream. #print. String calls print. On: a. Stream #(123 1 2 3) print. String -> '#(123 1 2 3)' Date today print. String -> 'October 5, 1997' Stéphane Ducasse «Chapter. Nr» . 27

Storing • • #store. String, #store. On: a. Stream. #store. String calls store. On: a. Stream Date today store. String -> '(Date read. From. String: ''10/5/1997'')' Ordered. Collection new add: 4 ; add: 3 ; store. String -> '((Ordered. Collection new) add: 4; add: 3; yourself)’ You need the compiler, so for a deployment image this is not convenient Stéphane Ducasse «Chapter. Nr» . 28

read. From. String: recreating Objects • Create instances from stored objects: class methods read. From: a. Stream, read. From. String: a. String Object read. From. String: '((Ordered. Collection new) add: 4; yourself)' -> Ordered. Collection (4) Stéphane Ducasse «Chapter. Nr» . 29

Notifying the programmer #error: a. String, #does. Not. Understand: a. Message, #halt: a. String, To invoke the debugger Input default. State if. True: [self halt] #should. Not. Implement Bad sign: subclassing #subclass. Responsibility Abstract method Stéphane Ducasse «Chapter. Nr» . 30

Copying of objects: #shallow. Copy, #copy • #shallow. Copy : the copy shares instance variables with the receiver. • default implementation of #copy is #shallow. Copy • Stéphane Ducasse «Chapter. Nr» . 31

Copying in VW Object>>copy ^ self shallow. Copy post. Copy Object>>post. Copy is a hook method • copy is a template method • Stéphane Ducasse «Chapter. Nr» . 32
- Slides: 32