Dans ces exercices vous devez devinez les résultats des expressions en simulant mentalement l'exécution des expressions.
Examinez les expressions suivantes. Quel est la valeur retournée par l'exécution des expressions suivantes ?
| anArray |
anArray := #('first' 'second' 'third' 'fourth').
anArray at: 2
Solution.
'second'
#(2 3 -10 3) collect: [ :each | each * each]
Solution.
#(4 9 100 9)
6 + 4 / 2
Solution.
5
1 + 3 negated
Solution.
-2
1 + (3 negated)
Solution.
-2
2 raisedTo: 3 + 2
Solution.
32
2 negated raisedTo: 3 + 2
Solution.
-32
#(a b c d e f) includesAll: #(f d b)
Solution.
true
Mettre plus de parenthèses que nécessaire est une bonne manière pour commencer. Cependant une telle pratique rend le code moins lisible. Réécrivez les expressions avec le minimum de parenthèses.
((3 + 4) + (2 * 2) + (2 * 3))
Solution.
3 + 4 + (2 * 2) + (2 * 3)
x between: (pt1 x) and: (pt2 y)
Solution.
x between: pt1 x and: pt2 y
(x isZero)
ifTrue: [....]
(x includes: y)
ifTrue: [....]
Solution.
x isZero
ifTrue: [....]
(x includes: y)
ifTrue: [....]
(Integer primesUpTo: 64) sum
Solution.
(Integer primesUpTo: 64) sum
(OrderedCollection new)
add: 56;
add: 33;
yourself
Solution.
OrderedCollection new
add: 56;
add: 33;
yourself
('http://www.pharo.org' asUrl) retrieveContents
Solution.
('http://www.pharo.org' asUrl) retrieveContents
(('2014-07-01' asDate) - '2013/2/1' asDate) days
Solution.
('2014-07-01' asDate - '2013/2/1' asDate) days
(((ZnEasy getPng: 'http://pharo.org/web/files/pharo.png')
asMorph) openInWindow)
Solution.
(ZnEasy getPng: 'http://pharo.org/web/files/pharo.png')
asMorph openInWindow
((#(a b c d e f) asSet) intersection: (#(f d b) asSet))
Solution.
#(a b c d e f) asSet intersection: #(f d b) asSet
Examinez chacune des expressions suivantes et décrivez la séquence des étapes de leur exécution (quel message est exécuté en premier).
Date today daysInMonth
Solution.
Date today daysInMonth
today
daysInMonth
5@5 extent: 6.2 truncated @ 7
Solution.
5@5 extent: 6.2 truncated @ 7
6.2 truncated
5@5
6@7
extent:
Transcript show: (45 + 9) printString
Solution.
Transcript show: (45 + 9) printString
(45 + 9)
printString
show:
('2014-07-01' asDate - '2013/2/1' asDate) days
Solution.
('2014-07-01' asDate - '2013/2/1' asDate) days
asDate
asDate
-
days
42 factorial decimalDigitLength
Solution.
42 factorial decimalDigitLength
factorial
decimalDigitLength
(ZnServer startDefaultOn: 8080)
onRequestRespond: [ :request | ZnResponse ok: (ZnEntity with: DateAndTime now printString) ]
Solution.
(ZnServer startDefaultOn: 8080)
onRequestRespond: [ :request | ZnResponse ok: (ZnEntity with: DateAndTime now printString) ]
startDefaultOn:
onRequestRespond:
now
printString
with:
ok:
(1914 to: 1945) count: [ :each | Year isLeapYear: each ].
Solution.
(1914 to: 1945) count: [ :each | Year isLeapYear: each ].
to:
count:
isLeapYear:
$/ join: ($- split: '1969-07-20') reverse
Solution.
$/ join: ($- split: '1969-07-20') reverse
split:
reverse
join:
DateAndTime fromUnixTime:
((ByteArray readHexFrom: 'CAFEBABE4422334400FF')
copyFrom: 5 to: 8) asInteger
Solution.
DateAndTime fromUnixTime:
((ByteArray readHexFrom: 'CAFEBABE4422334400FF')
copyFrom: 5 to: 8) asInteger
readHexFrom:
copyFrom:to:
asInteger
fromUnixTime:
(String new: 32) collect: [ :each | 'abcdef' atRandom ]
Solution.
(String new: 32) collect: [ :each | 'abcdef' atRandom ]
new:
collect:
atRandom
'http://www.pharo.org' asUrl saveContentsToFile: 'page.html'
Solution.
'http://www.pharo.org' asUrl saveContentsToFile: 'page.html'
asUrl
saveContentsToFile:
'^.*.jpg' asRegex in: [ :regex |
'/tmp/foo.txt' asFileReference contents lines
select: [ :line | regex matches: line ] ]
Solution.
'^.*.jpg' asRegex in: [ :regex |
'/tmp/foo.txt' asFileReference contents lines
select: [ :line | regex matches: line ] ]
asRegex
asFielReference
contents
line
matches: