Start with
scoreX: anInteger y: anInteger2
| distance |
distance := (anInteger squared + anInteger2 squared) sqrt.
distance > 10
ifTrue: [ ^ 0 ].
distance > 5
ifTrue: [ ^ 1 ].
distance > 1
ifTrue: [ ^ 5 ].
^ 10
(1) Use better argument names.
(2) Forg
What Smalltalk has for full-sized pattern matching?
Is it possible to make in message-only syntax?
--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html
Pharo Smalltalk Users mailing list wrote
> Hello,
>
> Im trying to solve a challenge from exercism where I have to
> calculate the points somehow gets on a very simple darts board.
>
> I solved it like this :
>
>
> scoreX: anInteger y: anInteger2
>
The “Kent Beck approved solutions” here are two:
1) To use a dictionary (or ordered dictionary).
case := OrderedDictionary newFromPairs: {
[ cond ]. [ do ].
[ cond ]. [ do ].
[ true ]. [ otherwise do ] }
case keysAndValuesDo: [ :cond :do |
(cond value: express
It would be an overkill to do it for this particular case, but Smalltalk
makes it possible to implement a case-like construction:
[ expression ]
when: [ :value | condition1 ] do: [-0do :value | ... ];
when: [ :value | condition2 ] do: [ :value | ... ];
otherwiseDo: [ :value | ... ];
--- Begin Message ---
Hello Richard,
Thanks for the feedback. I was using it because the challenge was
talking about it, that yu have the use the formula
a ^2 + b ^2 = c ^2 and the challenge was talking about the
distance and not the distance squared.
I think a better name schould be th
Myself, I see only one issue in your code, and that is the pointless
use of sqrt.
scoreX: anInteger y: anInteger2
| radiusSquared |
radiusSquared := anInteger squared + anInteger2 squared.
radiusSquared > 100
ifTrue: [ ^ 0 ].
radiusSquared > 25
ifTrue: [ ^ 1 ].
r
You could also accomplish this with a simple 'do:' construct by creating the
associations with the the ring limits & points "staggered" (by one level),
so that when (each < key) you can return the 'current' association value...
Which, now that I'm looking closely at your solution, Kasper, seems to
Doing it the way I describe above eliminates repetitive if-then statements
('switch' or 'case' statements in other languages, which make for a
maintenance mess), automatically extends for additional rings, doesn't embed
"magic numbers" for defined rings within the decision logic, and takes
advantag
I would probably set up an array of associations whose keys are the boundary
limits and whose values are the target values (ordered, in this example,
from outer to inner).
Then loop over the array's associations using inject:into:, testing for the
point where (each < key), at which point you ret
--- Begin Message ---
Hello Kasper
Thanks for the feedback.
I also like readable and easy to understand code but I get the
feeling that it;s good practice to use as minimal as possible if
then's
So I wonder if and how I can achieve that here.
Hi
First, I think your code is readable and easy to understand.
I think the problem does not lend itself too much towards having “an object
oriented solution”, as such.
If you are looking for a one-liner using smalltalk libraries, you could do:
({10 -> 0. 5 -> 1. 1 -> 5. 0 -> 10} detect: [ :dist
--- Begin Message ---
Thanks,
But it looks to me I still need the ifTrue because the between gives
also a true or false.
Roelof
Op 27-12-2019 om 20:38 schreef tbrunz:
You might try Magnitude >> between: min and: max
--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.htm
You might try Magnitude >> between: min and: max
--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html
--- Begin Message ---
Hello,
Im trying to solve a challenge from exercism where I have to
calculate the points somehow gets on a very simple darts board.
I solved it like this :
scoreX: anInteger y: anInteger2
| distance |
di
15 matches
Mail list logo