Re: [Pharo-users] can I write this without the three if then;s

2020-02-15 Thread Richard O'Keefe
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

Re: [Pharo-users] can I write this without the three if then;s

2019-12-30 Thread ponyatov
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

Re: [Pharo-users] can I write this without the three if then;s

2019-12-30 Thread Richard Sargent
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 >

Re: [Pharo-users] can I write this without the three if then;s

2019-12-29 Thread Esteban Lorenzano
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

Re: [Pharo-users] can I write this without the three if then;s

2019-12-28 Thread Dennis Schetinin
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 | ... ];

Re: [Pharo-users] can I write this without the three if then;s

2019-12-27 Thread Roelof Wobben via Pharo-users
--- 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

Re: [Pharo-users] can I write this without the three if then;s

2019-12-27 Thread Richard O'Keefe
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

Re: [Pharo-users] can I write this without the three if then;s

2019-12-27 Thread tbrunz
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

Re: [Pharo-users] can I write this without the three if then;s

2019-12-27 Thread tbrunz
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

Re: [Pharo-users] can I write this without the three if then;s

2019-12-27 Thread tbrunz
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

Re: [Pharo-users] can I write this without the three if then;s

2019-12-27 Thread Roelof Wobben via Pharo-users
--- 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.

Re: [Pharo-users] can I write this without the three if then;s

2019-12-27 Thread Kasper Østerbye
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

Re: [Pharo-users] can I write this without the three if then;s

2019-12-27 Thread Roelof Wobben via Pharo-users
--- 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

Re: [Pharo-users] can I write this without the three if then;s

2019-12-27 Thread tbrunz
You might try Magnitude >> between: min and: max -- Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

[Pharo-users] can I write this without the three if then;s

2019-12-27 Thread Roelof Wobben via Pharo-users
--- 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