[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 |
      distance := (anInteger squared + anInteger2 squared) sqrt.
      distance > 10
          ifTrue: [ ^ 0 ].
      distance > 5
          ifTrue: [ ^ 1 ].
      distance > 1
          ifTrue: [ ^ 5 ].
      ^ 10
  
  
  but now I use three if then and I think it's ugly code. 
  
  Is there a way I can make it more the smalltalk way ? 
  
  
  Regards, 
  
  Roelof

  


--- End Message ---


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



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.html





--- End Message ---


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 | dist key < distance
]) value

but I really think your code is easier to read, which is what I appreciate
the most.

Best,

Kasper

On 27 December 2019 at 20.18.38, Roelof Wobben via Pharo-users (
pharo-users@lists.pharo.org) 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
| distance |
distance := (anInteger squared + anInteger2 squared) sqrt.
distance > 10
ifTrue: [ ^ 0 ].
distance > 5
ifTrue: [ ^ 1 ].
distance > 1
ifTrue: [ ^ 5 ].
^ 10


but now I use three if then and I think it's ugly code.

Is there a way I can make it more the smalltalk way ?


Regards,

Roelof


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.
  
  Roelof
  
  
  Op 27-12-2019 om 20:47 schreef 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 | dist key
< distance ]) value
  
  
  but I
really think your code is easier to read, which is what I
appreciate the most.
  
  
  Best,
  
  
  Kasper

On 27 December 2019 at 20.18.38, Roelof
  Wobben via Pharo-users (pharo-users@lists.pharo.org)
  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
    | distance |
    distance := (anInteger squared + anInteger2
squared) sqrt.
    distance > 10
        ifTrue: [ ^ 0 ].
    distance > 5
        ifTrue: [ ^ 1 ].
    distance > 1
        ifTrue: [ ^ 5 ].
    ^ 10


but now I use three if then and I think it's ugly
code.

Is there a way I can make it more the smalltalk way
?


Regards,

Roelof
  

  

  
  


  


--- End Message ---


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 return the previous
association's value.

-t




--
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-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
advantage of the naturally recursive nature of this kind of graduated
comparison test.

-t



--
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-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 be
what you already proposed...  Either should work.



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html



[Pharo-users] uses or instead of a searching literal

2019-12-27 Thread Roelof Wobben via Pharo-users
--- Begin Message ---

  
  
Hello, 


How can I get rid of the above error message with this code : 


abbreviatePhrase: aString
| splitted |
splitted := aString
splitOn: [ :char | char = Character space or: [ char = $- or: [ char = $_ ] ] ].
^ String
streamContents: [ :stream | 
splitted
reject: [ :word | word isEmpty ]
    thenDo: [ :word | stream nextPut: word first uppercase ] ]


Regards, 

Roelof



  


--- End Message ---


Re: [Pharo-users] uses or instead of a searching literal

2019-12-27 Thread tbrunz
Since you're splitting a String, why not use a Regex to do this?



--
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-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 ].
radiusSquared > 1
ifTrue: [ ^ 5 ].
^ 10

Make that two issues.  The names anInteger and anInteger2.
How about
scoreHorizontal: horizontal vertical: vertical
  ...

The OO dogma about "if' that you refer to is about not using
"if" to make decisions based on TYPE.  Making decisions based
on NUMERIC RANGES is perfectly OK.

You could introduce a 'HalfOpenRangeDictionary" class
[K1,K2) -> V1
[K2,K3) -> V2
...
[Kn,infinity) -> Vn
But "You Ain't Gonna Need It", so don't.

On Sat, 28 Dec 2019 at 08:18, Roelof Wobben via Pharo-users
 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
> | distance |
> distance := (anInteger squared + anInteger2 squared) sqrt.
> distance > 10
> ifTrue: [ ^ 0 ].
> distance > 5
> ifTrue: [ ^ 1 ].
> distance > 1
> ifTrue: [ ^ 5 ].
> ^ 10
>
>
> but now I use three if then and I think it's ugly code.
>
> Is there a way I can make it more the smalltalk way ?
>
>
> Regards,
>
> Roelof
>



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 then x or xCoordinate and y or 
Y_Coordinate.


Roelof



Op 27-12-2019 om 22:48 schreef 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 ].
 radiusSquared > 1
 ifTrue: [ ^ 5 ].
 ^ 10

Make that two issues.  The names anInteger and anInteger2.
How about
scoreHorizontal: horizontal vertical: vertical
   ...

The OO dogma about "if' that you refer to is about not using
"if" to make decisions based on TYPE.  Making decisions based
on NUMERIC RANGES is perfectly OK.

You could introduce a 'HalfOpenRangeDictionary" class
[K1,K2) -> V1
[K2,K3) -> V2
...
[Kn,infinity) -> Vn
But "You Ain't Gonna Need It", so don't.

On Sat, 28 Dec 2019 at 08:18, Roelof Wobben via Pharo-users
 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
 | distance |
 distance := (anInteger squared + anInteger2 squared) sqrt.
 distance > 10
 ifTrue: [ ^ 0 ].
 distance > 5
 ifTrue: [ ^ 1 ].
 distance > 1
 ifTrue: [ ^ 5 ].
 ^ 10


but now I use three if then and I think it's ugly code.

Is there a way I can make it more the smalltalk way ?


Regards,

Roelof




--- End Message ---


Re: [Pharo-users] uses or instead of a searching literal

2019-12-27 Thread Richard O'Keefe
You know, this is an area where style opinions differ a lot.
What the message is suggesting you try is
aString splitOn: [:each | ' -_' includes: each]

That's all.  No need for anything more complicated.

But there is a trade-off.  Clarity vs efficiency.  You should make your
code clear and correct before you worry about efficiency, true.  But
you should not go out of your way to make it *less* efficient.
The test you have is perfectly readable and is as fast as it gets.
Using #includes: would make the code shorter, but not clearer, and
certainly slower.

You could also use
aString splitOn: '[-_ ]' asRegex
because #splitOn: is documented as treating strings as regular
expressions.  This is the briefest, the most confusing to read, and
the slowest.  It is also by far the most error-prone:
- forget "asRegex" and you are in trouble
- forget the square brackets and you are in trouble
- put the hyphen-minus second instead of first and ...

If only #split:indicesDo: were defined for Set.  I cannot imagine
why it is not.  Here is a method definition that goes in the (new)
'splitting' category of Set:

split: aSequenceableCollection indicesDo: aBlock
"Perform an action specified as aBlock (with a start and end
argument) to each of the indices of aSequenceableCollection that have
been identified by taking the receiver as a splitter."
| position |

position := 1.
aSequenceableCollection withIndexDo: [:element :idx |
(self includes: element)
ifTrue: [
aBlock value: position value: idx - 1.
position := idx + 1 ]].

aBlock value: position value: aSequenceableCollection size

With that in place you can use
  aString splitOn: ' -_' asSet

On Sat, 28 Dec 2019 at 10:24, Roelof Wobben via Pharo-users
 wrote:
>
> Hello,
>
>
> How can I get rid of the above error message with this code :
>
>
> abbreviatePhrase: aString
> | splitted |
> splitted := aString
> splitOn: [ :char | char = Character space or: [ char = $- or: [ char 
> = $_ ] ] ].
> ^ String
> streamContents: [ :stream |
> splitted
> reject: [ :word | word isEmpty ]
> thenDo: [ :word | stream nextPut: word first uppercase ] ]
>
>
> Regards,
>
> Roelof
>
'From Pharo7.0.2 of 15 March 2019 [Build information: Pharo-7.0.2+build.154.sha.9f17218676db0c1a0dd5d1b03226e660dbd674b6 (32 Bit)] on 28 December 2019 at 11:33:01.066244 am'!

!Set methodsFor: 'splitting' stamp: 'RichardOKeefe 12/28/2019 11:29'!
split: aSequenceableCollection indicesDo: aBlock
	"Perform an action specified as aBlock (with a start and end argument) to each of the indices of aSequenceableCollection that have been identified by taking the receiver as a splitter." 
	| position |
	
	position := 1.
	aSequenceableCollection withIndexDo: [:element :idx |
		(self includes: element) 
			ifTrue: [
aBlock value: position value: idx - 1.
position := idx + 1 ]].
		
	aBlock value: position value: aSequenceableCollection size! !


!Set reorganize!
(#'*OpalCompiler-Core' parseOptions:)
(#'*Monticello-Storing' comeFullyUpOnReload:)
(#copying postCopy)
(#adding add:)
(#accessing like:ifAbsent: like:)
(#converting asSet)
(#private rehash fixCollisionsFrom: noCheckAdd: scanFor: grow withArray: noCheckNoGrowFillFrom:)
(#'*Random-Core' atRandom:)
(#enumerating difference: collect: intersection: do:)
(#'*Reflectivity' metaLinkOptions)
(#'*ston-core' stonPostReferenceResolution)
(#comparing =)
(#testing isHealthy occurrencesOf: includes:)
(#removing remove:ifAbsent: copyWithout:)
(#'*GT-InspectorExtensions-Core' gtInspectorItemsIn:)
(#splitting split:indicesDo:)
(#'*Fuel-Core' fuelAfterMaterialization fuelAccept: addIfNotPresent:ifPresentDo:)
!



[Pharo-users] Hi Community

2019-12-27 Thread Pablo Navarro
Hi everyone, my name's Pablo and I'm from Argentina. I'm taking my first steps 
in Pharo and created this tool (https://github.com/pablo1n7/Smallbook) to share 
with my colleagues and show them the power of Pharo.

It's a simple library to create slide presentations and show them in a web 
browser.

I used Zinc HTTP for the server, JS for the presentation controls and CSS for 
the styles.

It's not 100% complete, I'm still working on it. I hope you find it useful and 
any suggestion is welcomed


Best Regards, Pablo.


Re: [Pharo-users] Hi Community

2019-12-27 Thread Ben Coman
Looks cool. Thanks for sharing.

cheers -ben

On Sat, 28 Dec 2019 at 09:55, Pablo Navarro  wrote:

> Hi everyone, my name's Pablo and I'm from Argentina. I'm taking my first
> steps in Pharo and created this tool (
> https://github.com/pablo1n7/Smallbook) to share with my colleagues and
> show them the power of Pharo.
>
>
> It's a simple library to create slide presentations and show them in a web
> browser.
>
>
> I used Zinc HTTP for the server, JS for the presentation controls and CSS
> for the styles.
>
>
> It's not 100% complete, I'm still working on it. I hope you find it useful
> and any suggestion is welcomed
>
>
>
> Best Regards, Pablo.
>


Re: [Pharo-users] uses or instead of a searching literal

2019-12-27 Thread Roelof Wobben via Pharo-users
--- Begin Message ---

Op 27-12-2019 om 23:33 schreef Richard O'Keefe:

  aString splitOn: ' -_' asSet


Hello Richard,

Thanks again , I find this  "aString splitOn: '_- `  asSet "   much 
cleaner but on some way I does not  split for example  'Portable Network 
Graphics' into  " #(Portable, Network, Graphics) ".  When I debug  it , 
it seems there is no splitting at all.


So today time to dive into it why it does not work and how to solve it.

Roelof


--- End Message ---