Skip to content

Commit 65c50ad

Browse files
committed
Extract returns from conditionals
1 parent 6a0c2f8 commit 65c50ad

File tree

5 files changed

+145
-145
lines changed

5 files changed

+145
-145
lines changed

src/ExtendedNumberParser/ExtendedNumberParser.class.st

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ An ExtendedNumberParser is extending Squeak number syntax with these rules
1010
Class {
1111
#name : #ExtendedNumberParser,
1212
#superclass : #NumberParser,
13-
#category : 'ExtendedNumberParser'
13+
#category : #ExtendedNumberParser
1414
}
1515

1616
{ #category : #accessing }
@@ -76,43 +76,39 @@ ExtendedNumberParser >> nextFraction [
7676
ExtendedNumberParser >> nextNumber [
7777
"main method for reading a number.
7878
This one can read Float Integer and ScaledDecimal"
79-
79+
8080
| numberOfTrailingZeroInIntegerPart |
8181
base := 10.
8282
neg := self peekSignIsMinus.
8383
integerPart := self nextUnsignedIntegerOrNilBase: base.
84-
integerPart ifNil: [(sourceStream peekFor: $.)
85-
ifTrue: [
86-
"Try .1 syntax"
87-
^self readNumberWithoutIntegerPart]
88-
ifFalse: [
89-
"This is not a regular number beginning with a digit
84+
integerPart ifNil: [
85+
^ (sourceStream peekFor: $.)
86+
ifTrue: [ "Try .1 syntax" self readNumberWithoutIntegerPart ]
87+
ifFalse: [ "This is not a regular number beginning with a digit
9088
It is time to check for exceptional condition NaN and Infinity"
91-
^self readNamedFloatOrFail]].
89+
self readNamedFloatOrFail ] ].
9290
numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero.
93-
(sourceStream peekFor: $r)
94-
ifTrue: ["<base>r<integer>"
95-
| oldNeg pos |
96-
pos := sourceStream position.
97-
(base := integerPart) < 2
98-
ifTrue: ["A radix currently need to be greater than 1, ungobble the r and return the integer part"
99-
sourceStream skip: -1.
100-
^neg
101-
ifTrue: [base negated]
102-
ifFalse: [base]].
103-
oldNeg := neg.
104-
self peekSignIsMinus ifTrue: [neg := neg not].
105-
integerPart := self nextUnsignedIntegerOrNilBase: base.
106-
integerPart ifNil: [
107-
(sourceStream peekFor: $.) ifTrue: [self readNumberWithoutIntegerPartOrNil ifNotNil: [:aNumber | ^aNumber]].
108-
sourceStream position: pos.
109-
^oldNeg
110-
ifTrue: [base negated]
111-
ifFalse: [base]].
112-
numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero].
91+
(sourceStream peekFor: $r) ifTrue: [ "<base>r<integer>"
92+
| oldNeg pos |
93+
pos := sourceStream position.
94+
(base := integerPart) < 2 ifTrue: [ "A radix currently need to be greater than 1, ungobble the r and return the integer part"
95+
sourceStream skip: -1.
96+
^ neg
97+
ifTrue: [ base negated ]
98+
ifFalse: [ base ] ].
99+
oldNeg := neg.
100+
self peekSignIsMinus ifTrue: [ neg := neg not ].
101+
integerPart := self nextUnsignedIntegerOrNilBase: base.
102+
integerPart ifNil: [
103+
(sourceStream peekFor: $.) ifTrue: [ self readNumberWithoutIntegerPartOrNil ifNotNil: [ :aNumber | ^ aNumber ] ].
104+
sourceStream position: pos.
105+
^ oldNeg
106+
ifTrue: [ base negated ]
107+
ifFalse: [ base ] ].
108+
numberOfTrailingZeroInIntegerPart := nDigits - lastNonZero ].
113109
^ (sourceStream peekFor: $.)
114-
ifTrue: [self readNumberWithFractionPartNumberOfTrailingZeroInIntegerPart: numberOfTrailingZeroInIntegerPart]
115-
ifFalse: [self makeIntegerOrScaledInteger]
110+
ifTrue: [ self readNumberWithFractionPartNumberOfTrailingZeroInIntegerPart: numberOfTrailingZeroInIntegerPart ]
111+
ifFalse: [ self makeIntegerOrScaledInteger ]
116112
]
117113

118114
{ #category : #'parsing-private' }

src/Math-ArbitraryPrecisionFloat/PMArbitraryPrecisionFloat.class.st

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Class {
2828
'mantissa',
2929
'biasedExponent'
3030
],
31-
#category : 'Math-ArbitraryPrecisionFloat'
31+
#category : #'Math-ArbitraryPrecisionFloat'
3232
}
3333

3434
{ #category : #examples }
@@ -129,33 +129,38 @@ PMArbitraryPrecisionFloat >> / aNumber [
129129

130130
{ #category : #comparing }
131131
PMArbitraryPrecisionFloat >> < aNumber [
132-
aNumber class = self class ifTrue:
133-
[self negative == aNumber negative
134-
ifTrue: [self negative
135-
ifTrue: [^ (self digitCompare: aNumber) > 0]
136-
ifFalse: [^ (self digitCompare: aNumber) < 0]]
137-
ifFalse: [^ self negative]].
132+
133+
aNumber class = self class ifTrue: [
134+
^ self negative == aNumber negative
135+
ifTrue: [
136+
self negative
137+
ifTrue: [ (self digitCompare: aNumber) > 0 ]
138+
ifFalse: [ (self digitCompare: aNumber) < 0 ] ]
139+
ifFalse: [ self negative ] ].
138140
^ aNumber adaptToArbitraryPrecisionFloat: self andCompare: #<
139141
]
140142

141143
{ #category : #comparing }
142144
PMArbitraryPrecisionFloat >> = aNumber [
143-
aNumber isNumber ifFalse: [^ false].
144-
aNumber class = self class ifTrue:
145-
[aNumber negative == self negative
146-
ifTrue: [^ (self digitCompare: aNumber) = 0]
147-
ifFalse: [^ false]].
145+
146+
aNumber isNumber ifFalse: [ ^ false ].
147+
aNumber class = self class ifTrue: [
148+
^ aNumber negative == self negative
149+
ifTrue: [ (self digitCompare: aNumber) = 0 ]
150+
ifFalse: [ false ] ].
148151
^ aNumber adaptToArbitraryPrecisionFloat: self andCompare: #=
149152
]
150153

151154
{ #category : #comparing }
152155
PMArbitraryPrecisionFloat >> > aNumber [
153-
aNumber class = self class ifTrue:
154-
[self negative == aNumber negative
155-
ifTrue: [self negative
156-
ifTrue: [^(self digitCompare: aNumber) < 0]
157-
ifFalse: [^(self digitCompare: aNumber) > 0]]
158-
ifFalse: [^ aNumber negative]].
156+
157+
aNumber class = self class ifTrue: [
158+
^ self negative == aNumber negative
159+
ifTrue: [
160+
self negative
161+
ifTrue: [ (self digitCompare: aNumber) < 0 ]
162+
ifFalse: [ (self digitCompare: aNumber) > 0 ] ]
163+
ifFalse: [ aNumber negative ] ].
159164
^ aNumber adaptToArbitraryPrecisionFloat: self andCompare: #>
160165
]
161166

@@ -612,21 +617,20 @@ PMArbitraryPrecisionFloat >> decimalPrecision [
612617
]
613618

614619
{ #category : #private }
615-
PMArbitraryPrecisionFloat >> digitCompare: b [
620+
PMArbitraryPrecisionFloat >> digitCompare: b [
616621
"both are positive or negative.
617622
answer +1 if i am of greater magnitude, -1 if i am of smaller magnitude, 0 if equal magnitude"
618-
623+
619624
| compare |
620-
self isZero
621-
ifTrue: [b isZero
622-
ifTrue: [^ 0]
623-
ifFalse: [^ -1]].
624-
b isZero
625-
ifTrue: [^ 1].
625+
self isZero ifTrue: [
626+
^ b isZero
627+
ifTrue: [ 0 ]
628+
ifFalse: [ -1 ] ].
629+
b isZero ifTrue: [ ^ 1 ].
626630
compare := (self exponent - b exponent) sign.
627631
^ compare = 0
628-
ifTrue: [(self abs - b abs) sign]
629-
ifFalse: [compare]
632+
ifTrue: [ (self abs - b abs) sign ]
633+
ifFalse: [ compare ]
630634
]
631635

632636
{ #category : #'mathematical functions' }

src/Math-Complex/PMComplexNumber.class.st

Lines changed: 71 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -124,52 +124,52 @@ PMComplexNumber class >> zero [
124124
{ #category : #arithmetic }
125125
PMComplexNumber >> * anObject [
126126
"Answer the result of multiplying the receiver by aNumber."
127+
127128
| a b c d newReal newImaginary |
128-
anObject isComplexNumber
129-
ifTrue:
130-
[a := self real.
131-
b := self imaginary.
132-
c := anObject real.
133-
d := anObject imaginary.
134-
newReal := (a * c) - (b * d).
135-
newImaginary := (a * d) + (b * c).
136-
^ PMComplexNumber real: newReal imaginary: newImaginary]
137-
ifFalse:
138-
[^ anObject adaptToComplex: self andSend: #*]
129+
^ anObject isComplexNumber
130+
ifTrue: [
131+
a := self real.
132+
b := self imaginary.
133+
c := anObject real.
134+
d := anObject imaginary.
135+
newReal := a * c - (b * d).
136+
newImaginary := a * d + (b * c).
137+
PMComplexNumber real: newReal imaginary: newImaginary ]
138+
ifFalse: [ anObject adaptToComplex: self andSend: #* ]
139139
]
140140

141141
{ #category : #arithmetic }
142142
PMComplexNumber >> + anObject [
143143
"Answer the sum of the receiver and aNumber."
144+
144145
| a b c d newReal newImaginary |
145-
anObject isComplexNumber
146-
ifTrue:
147-
[a := self real.
148-
b := self imaginary.
149-
c := anObject real.
150-
d := anObject imaginary.
151-
newReal := a + c.
152-
newImaginary := b + d.
153-
^ PMComplexNumber real: newReal imaginary: newImaginary]
154-
ifFalse:
155-
[^ anObject adaptToComplex: self andSend: #+]
146+
^ anObject isComplexNumber
147+
ifTrue: [
148+
a := self real.
149+
b := self imaginary.
150+
c := anObject real.
151+
d := anObject imaginary.
152+
newReal := a + c.
153+
newImaginary := b + d.
154+
PMComplexNumber real: newReal imaginary: newImaginary ]
155+
ifFalse: [ anObject adaptToComplex: self andSend: #+ ]
156156
]
157157

158158
{ #category : #arithmetic }
159159
PMComplexNumber >> - anObject [
160160
"Answer the difference between the receiver and aNumber."
161+
161162
| a b c d newReal newImaginary |
162-
anObject isComplexNumber
163-
ifTrue:
164-
[a := self real.
165-
b := self imaginary.
166-
c := anObject real.
167-
d := anObject imaginary.
168-
newReal := a - c.
169-
newImaginary := b - d.
170-
^ PMComplexNumber real: newReal imaginary: newImaginary]
171-
ifFalse:
172-
[^ anObject adaptToComplex: self andSend: #-]
163+
^ anObject isComplexNumber
164+
ifTrue: [
165+
a := self real.
166+
b := self imaginary.
167+
c := anObject real.
168+
d := anObject imaginary.
169+
newReal := a - c.
170+
newImaginary := b - d.
171+
PMComplexNumber real: newReal imaginary: newImaginary ]
172+
ifFalse: [ anObject adaptToComplex: self andSend: #- ]
173173
]
174174

175175
{ #category : #arithmetic }
@@ -189,10 +189,11 @@ PMComplexNumber >> / aNumber [
189189

190190
{ #category : #comparing }
191191
PMComplexNumber >> = anObject [
192-
anObject isNumber ifFalse: [^false].
193-
anObject isComplexNumber
194-
ifTrue: [^ (real = anObject real) & (imaginary = anObject imaginary)]
195-
ifFalse: [^ anObject adaptToComplex: self andSend: #=]
192+
193+
anObject isNumber ifFalse: [ ^ false ].
194+
^ anObject isComplexNumber
195+
ifTrue: [ real = anObject real & (imaginary = anObject imaginary) ]
196+
ifFalse: [ anObject adaptToComplex: self andSend: #= ]
196197
]
197198

198199
{ #category : #arithmetic }
@@ -262,16 +263,17 @@ PMComplexNumber >> arCosh [
262263
^self arcCos i
263264
This implementation provides an answer with a positive real part.
264265
It also avoids creating intermediate Complex."
265-
266+
266267
| x y tmp sh2x shx delta ch2x chx |
267-
imaginary = 0 ifTrue: [real abs > 1
268-
ifTrue:
269-
[y := real < 0
270-
ifTrue: [Float pi]
271-
ifFalse: [0].
272-
x := real abs arCosh.
273-
^self class real: x imaginary: y]
274-
ifFalse: [^self class real: 0 imaginary: real arcCos]].
268+
imaginary = 0 ifTrue: [
269+
^ real abs > 1
270+
ifTrue: [
271+
y := real < 0
272+
ifTrue: [ Float pi ]
273+
ifFalse: [ 0 ].
274+
x := real abs arCosh.
275+
self class real: x imaginary: y ]
276+
ifFalse: [ self class real: 0 imaginary: real arcCos ] ].
275277
tmp := self squaredNorm - 1 / 2.
276278
delta := tmp squared + imaginary squared.
277279
sh2x := tmp + delta sqrt.
@@ -280,7 +282,7 @@ PMComplexNumber >> arCosh [
280282
chx := ch2x sqrt.
281283
x := shx arSinh.
282284
y := imaginary copySignTo: (real / chx) arcCos.
283-
^self class real: x imaginary: y
285+
^ self class real: x imaginary: y
284286
]
285287

286288
{ #category : #'mathematical functions' }
@@ -315,14 +317,15 @@ PMComplexNumber >> arcCos [
315317
This is the inverse function of cos."
316318

317319
| x y tmp sh2y shy delta ch2y chy |
318-
imaginary = 0 ifTrue: [real abs > 1
319-
ifTrue:
320-
[x := real < 0
321-
ifTrue: [Float pi]
322-
ifFalse: [0].
323-
y := real copySignTo: real abs arCosh.
324-
^self class real: x imaginary: y]
325-
ifFalse: [^self class real: real arcCos imaginary: 0]].
320+
imaginary = 0 ifTrue: [
321+
^ real abs > 1
322+
ifTrue: [
323+
x := real < 0
324+
ifTrue: [ Float pi ]
325+
ifFalse: [ 0 ].
326+
y := real copySignTo: real abs arCosh.
327+
self class real: x imaginary: y ]
328+
ifFalse: [ self class real: real arcCos imaginary: 0 ] ].
326329
tmp := self squaredNorm - 1 / 2.
327330
delta := tmp squared + imaginary squared.
328331
sh2y := tmp + delta sqrt.
@@ -331,7 +334,7 @@ PMComplexNumber >> arcCos [
331334
chy := ch2y sqrt.
332335
y := imaginary copySignTo: shy arSinh.
333336
x := (real / chy) arcCos.
334-
^self class real: x imaginary: y negated
337+
^ self class real: x imaginary: y negated
335338
]
336339

337340
{ #category : #'mathematical functions' }
@@ -340,23 +343,22 @@ PMComplexNumber >> arcSin [
340343
This is the inverse function of sin."
341344

342345
| x y tmp delta sh2y shy ch2y chy |
343-
imaginary = 0
344-
ifTrue:
345-
[real abs > 1
346-
ifTrue:
347-
[x := Float pi / 2 * real sign.
348-
y := (real copySignTo: real abs arCosh) negated.
349-
^self class real: x imaginary: y]
350-
ifFalse: [^self class real: real arcSin imaginary: 0]].
351-
tmp := (self squaredNorm - 1) / 2.
346+
imaginary = 0 ifTrue: [
347+
^ real abs > 1
348+
ifTrue: [
349+
x := Float pi / 2 * real sign.
350+
y := (real copySignTo: real abs arCosh) negated.
351+
self class real: x imaginary: y ]
352+
ifFalse: [ self class real: real arcSin imaginary: 0 ] ].
353+
tmp := self squaredNorm - 1 / 2.
352354
delta := tmp squared + imaginary squared.
353355
sh2y := tmp + delta sqrt.
354356
shy := sh2y sqrt.
355357
ch2y := 1 + sh2y.
356358
chy := ch2y sqrt.
357359
y := imaginary copySignTo: shy arSinh.
358360
x := (real / chy) arcSin.
359-
^self class real: x imaginary: y
361+
^ self class real: x imaginary: y
360362
]
361363

362364
{ #category : #'mathematical functions' }
@@ -686,10 +688,9 @@ PMComplexNumber >> reciprocal [
686688
"Answer 1 divided by the receiver. Create an error notification if the
687689
receiver is 0."
688690

689-
self = 0
690-
ifTrue: [^ (ZeroDivide dividend: self) signal]
691-
ifFalse: [^1 / self]
692-
691+
^ self = 0
692+
ifTrue: [ (ZeroDivide dividend: self) signal ]
693+
ifFalse: [ 1 / self ]
693694
]
694695

695696
{ #category : #testing }

0 commit comments

Comments
 (0)