Re: [Pharo-users] can I divide a string into part of exactly n length

2020-04-27 Thread Richard O'Keefe
|s1 s2|
s1 := 'abcdefghijk'.
s2 := String streamContents: [:s |
  s1 keysAndValuesDo: [:index :each |
(index \\ 3 = 1 and: [1 < index])
  ifTrue: [s space].
s nextPut: each]].

is perhaps a little simpler.  I generally prefer to have Smalltalk
do the counting for me.

On Tue, 28 Apr 2020 at 07:25, Todd Blanchard via Pharo-users
 wrote:
>
> This works
>
> | s1 s2 |
> s1 := 'abcdefghijk'.
> s2 := String streamContents: [:s || in len |
>in := ReadStream on: s1.
>len := 0.
>[in atEnd] whileFalse: [
>   s nextPut: in next.
>   len := len + 1.
>   (in atEnd not and: [ (len \\ 3) = 0]) ifTrue: [ s space ] ] ]
>
> On Apr 27, 2020, at 10:07 AM, Roelof Wobben via Pharo-users 
>  wrote:
>
>
> From: Roelof Wobben 
> Subject: Re: [Pharo-users] can I divide a string into part of exactly n length
> Date: April 27, 2020 at 10:07:41 AM PDT
> To: Richard Sargent , Any question about 
> pharo is welcome 
>
>
> Op 27-4-2020 om 19:05 schreef Richard Sargent:
>
> On Mon, Apr 27, 2020 at 9:27 AM Roelof Wobben via Pharo-users 
>  wrote:
>>
>> Hello,
>>
>> I wonder if it is possible in Pharo to divide a string in lets say part
>> of 3.
>> so this :  'abcdefgh'
>>
>> would be  'abc def gh`
>
>
> Do you really want a single string with spaces inserted or do you want a 
> collection of substrings, each three characters (or less, for the last one)?
>
>
> I need a single string with spaces inserted.
> So maybe I have formulate my question wrong. Very difficult if English is not 
> your mother language and you were bad at school in languages a very very long 
> ago.
>
> Roelof
>
>
>
>



Re: [Pharo-users] how can I test this

2020-04-27 Thread Richard O'Keefe
You wrote "I want to test if the response back to the sending node is
the same string as the sending node is but then all uppercased."  What
is wrong with
received = sent asUppercase
?
If for some reason you don't like that, then
received class = sent class and: [
received size = sent size and: [
received with: sent allSatisfy: [:r :s |
   r = s asUppercase]]]
might do the job, but for two things.
(1)
SequenceableCollection>>
with: another allSatisfy: testBlock
"Extension of Collection>>allSatisfy: to test whether corresponding
 elements of two sequences already known to be the same length all
 satisfy the given test."
self with: another do: [:x :y |
(testBlock value: x value: y)
ifFalse: [^false]].
^true
is not in Pharo 7.
(2) Case conversion in Unicode is *not* a code-point by code-point mapping;
it can change the length of a string.  received = sent asUppercase
is not just the simplest way, it's the safest.

On Tue, 28 Apr 2020 at 05:04, Roelof Wobben via Pharo-users
 wrote:
>
> Op 27-4-2020 om 18:28 schreef Richard Sargent:
>
> On Mon, Apr 27, 2020 at 1:56 AM Roelof Wobben via Pharo-users 
>  wrote:
>>
>> Hello,
>>
>> I have to test the server which should return the string but then in
>> uppercase.
>>
>> So far I have this :
>> https://github.com/RoelofWobben/Network-simulator/blob/master/src/NetworkSimulator-Tests/KANetworkTest.class.st#L79
>>
>> but I fail to see how I can test that the package sending back is a
>> uppercase string.
>
>
> I think you need to rephrase this question. Either the String class hierarchy 
> has a method named something like #isUppercase or Character does. If only 
> Character has it, you would need to iterate over the received string testing 
> each individual character.
>
> Or is your question more about getting the response back to the sending node?
>
>
> yep, I want to test if the response back to the sending node is the same 
> string as the sending node is but then all uppercased.
>
> Roelof
>



Re: [Pharo-users] how can I test this

2020-04-27 Thread Roelof Wobben via Pharo-users
--- Begin Message ---

Op 27-4-2020 om 19:50 schreef Roelof Wobben via Pharo-users:


This seems to be working


testKaNetWorkServer
    | srcNode destNode link packet link2 packet2 recievedPackage |
    srcNode := KANetworkNode withAddress: #src.
    destNode := KANetworkServer withAddress: #dest.
    link := (KANetworkLink from: srcNode to: destNode)
        attach;
        yourself.
    link2 := (KANetworkLink from: destNode to: srcNode)
        attach;
        yourself.
    packet := KANetworkPacket from: #src to: #dest payload: 'test'.
    srcNode send: packet via: link.
    destNode consume: packet.
    packet2 := link2 packetsToTransmit first.
    srcNode receive: packet2 from: destNode.
    recievedPackage := srcNode arrivedPackets first.
    self assert: recievedPackage payload equals: 'TEST'

I hope it is a good one.



--- End Message ---


Re: [Pharo-users] can I divide a string into part of exactly n length

2020-04-27 Thread Todd Blanchard via Pharo-users
--- Begin Message ---
This works

| s1 s2 |
s1 := 'abcdefghijk'.
s2 := String streamContents: [:s || in len | 
   in := ReadStream on: s1.
   len := 0.
   [in atEnd] whileFalse: [ 
  s nextPut: in next.
  len := len + 1.
  (in atEnd not and: [ (len \\ 3) = 0]) ifTrue: [ s space ] ] ]

> On Apr 27, 2020, at 10:07 AM, Roelof Wobben via Pharo-users 
>  wrote:
> 
> 
> From: Roelof Wobben 
> Subject: Re: [Pharo-users] can I divide a string into part of exactly n length
> Date: April 27, 2020 at 10:07:41 AM PDT
> To: Richard Sargent , Any question about 
> pharo is welcome 
> 
> 
> Op 27-4-2020 om 19:05 schreef Richard Sargent:
>> On Mon, Apr 27, 2020 at 9:27 AM Roelof Wobben via Pharo-users 
>> mailto:pharo-users@lists.pharo.org>> wrote:
>> Hello,
>> 
>> I wonder if it is possible in Pharo to divide a string in lets say part 
>> of 3.
>> so this :  'abcdefgh'
>> 
>> would be  'abc def gh`
>> 
>> Do you really want a single string with spaces inserted or do you want a 
>> collection of substrings, each three characters (or less, for the last one)?
>> 
> 
> I need a single string with spaces inserted.
> So maybe I have formulate my question wrong. Very difficult if English is not 
> your mother language and you were bad at school in languages a very very long 
> ago.
> 
> Roelof
> 
> 
> 

--- End Message ---


Re: [Pharo-users] how can I test this

2020-04-27 Thread Roelof Wobben via Pharo-users
--- Begin Message ---

  
  
Op 27-4-2020 om 19:16 schreef Richard
  Sargent:


  
  

  On Mon, Apr 27, 2020 at 9:30
AM Roelof Wobben  wrote:
  
  

  Op 27-4-2020 om 18:28 schreef Richard Sargent:
  
  

  
On Mon, Apr 27,
  2020 at 1:56 AM Roelof Wobben via Pharo-users 
  wrote:

Hello,
  
  I have to test the server which should return the
  string but then in 
  uppercase.
  
  So far I have this : 
  https://github.com/RoelofWobben/Network-simulator/blob/master/src/NetworkSimulator-Tests/KANetworkTest.class.st#L79
  
  but I fail to see how I can test that the package
  sending back is a 
  uppercase string.



I think you need to rephrase this question.
  Either the String class hierarchy has a method
  named something like #isUppercase or Character
  does. If only Character has it, you would need to
  iterate over the received string testing each
  individual character.


Or is your question more about getting the
  response back to the sending node?

  

  
  
  yep, I want to test if the response back to the sending
  node is the same string as the sending node is but then
  all uppercased. 

  
  
  
  Assuming you have the code written that sends a request
packet to a server, then makes the server process it and
send a response packet back to the original node, the
following pseudo-code should show you one way.
  
  
  | requestPacket responsePacket |
  requestPacket := ...
  ... send the request and get a response back ...
  responsePacket := self sendingNode nextReceivedPacket.
  
  
  self assert: responsePacket payload isUppercase
description: 'Response should have been uppercase'.
  self assert: (requestPacket payload equals:
responsePacket payload ignoreCase: true) description:
'Response is not the same string'.
  
  
  Those methods probably exist in Pharo, or something very
similar. e.g. it might be #equalsIgnoreCase:.
  
  

  


oke, 

I think I can move forward. 
I have some ideas how to make this work but have to try some things.


Thanks

Roelof

  


--- End Message ---


Re: [Pharo-users] how can I test this

2020-04-27 Thread Richard Sargent
On Mon, Apr 27, 2020 at 9:30 AM Roelof Wobben  wrote:

> Op 27-4-2020 om 18:28 schreef Richard Sargent:
>
> On Mon, Apr 27, 2020 at 1:56 AM Roelof Wobben via Pharo-users <
> pharo-users@lists.pharo.org> wrote:
>
>> Hello,
>>
>> I have to test the server which should return the string but then in
>> uppercase.
>>
>> So far I have this :
>>
>> https://github.com/RoelofWobben/Network-simulator/blob/master/src/NetworkSimulator-Tests/KANetworkTest.class.st#L79
>>
>> but I fail to see how I can test that the package sending back is a
>> uppercase string.
>>
>
> I think you need to rephrase this question. Either the String class
> hierarchy has a method named something like #isUppercase or Character does.
> If only Character has it, you would need to iterate over the received
> string testing each individual character.
>
> Or is your question more about getting the response back to the sending
> node?
>
>
> yep, I want to test if the response back to the sending node is the same
> string as the sending node is but then all uppercased.
>

Assuming you have the code written that sends a request packet to a server,
then makes the server process it and send a response packet back to the
original node, the following pseudo-code should show you one way.

| requestPacket responsePacket |
requestPacket := ...
... send the request and get a response back ...
responsePacket := self sendingNode nextReceivedPacket.

self assert: responsePacket payload isUppercase description: 'Response
should have been uppercase'.
self assert: (requestPacket payload equals: responsePacket payload
ignoreCase: true) description: 'Response is not the same string'.

Those methods probably exist in Pharo, or something very similar. e.g. it
might be #equalsIgnoreCase:.


> Roelof
>
>


Re: [Pharo-users] can I divide a string into part of exactly n length

2020-04-27 Thread Roelof Wobben via Pharo-users
--- Begin Message ---

  
  
Op 27-4-2020 om 19:05 schreef Richard
  Sargent:


  
  

  On Mon, Apr 27, 2020 at 9:27
AM Roelof Wobben via Pharo-users 
wrote:
  
  Hello,

I wonder if it is possible in Pharo to divide a string in
lets say part 
of 3.
so this :  'abcdefgh'

would be  'abc def gh`
  
  
  
  Do you really want a single string with spaces inserted
or do you want a collection of substrings, each three
characters (or less, for the last one)?
  

  


I need a single string with spaces inserted.
So maybe I have formulate my question wrong. Very difficult if
English is not your mother language and you were bad at school in
languages a very very long ago.

Roelof

  


--- End Message ---


Re: [Pharo-users] can I divide a string into part of exactly n length

2020-04-27 Thread Richard Sargent
On Mon, Apr 27, 2020 at 9:27 AM Roelof Wobben via Pharo-users <
pharo-users@lists.pharo.org> wrote:

> Hello,
>
> I wonder if it is possible in Pharo to divide a string in lets say part
> of 3.
> so this :  'abcdefgh'
>
> would be  'abc def gh`
>

Do you really want a single string with spaces inserted or do you want a
collection of substrings, each three characters (or less, for the last one)?



> Regards,
>
> Roelof
>
>
>


Re: [Pharo-users] mentor question 2.

2020-04-27 Thread Richard Sargent
On Sun, Apr 26, 2020 at 10:11 AM Roelof Wobben via Pharo-users <
pharo-users@lists.pharo.org> wrote:

> Hello,
>
> I have to make some code that convert a binary to a decimal and im not
> allowed to use the convert methods that Pharo has.
>
> So I have written this :
>
>
> decimalFromBinary: aString
>  | result isValid |
>  isValid = self isValidBinary: aString.
>  isValid
>  ifTrue: [ result := 0.
>  aString reverse
>  withIndexDo:
>  [ :digit :index | result := result + (digit
> digitValue * (2 raisedTo: index - 1)) ].
>  ^ result ]
>  ifFalse: [ ^ nil ]
>
> isValidBinary: aString
>  ^ aString allSatisfy: [ :c | c = 0 or: [ c = 1 ] ]
>

Others have answered your question, so I'll limit my response to other
aspects of this exercise.

Answering nil is a bad practice, in general. There will be occasions where
it is correct, if non-existence really is part of the model. Answering nil
may have been a requirement of the exercise and if that's the case, you
have no choice. By answering nil, you force every sender to check for nil
rather than being able to rely on receiving an Integer result. Even with
method selectors that tell the reader a nil can be the result, too many
people will fail to check the result before using it. In general, I prefer
to have the validation throw an exception.
e.g.
validateBinaryString: aString
(aString allSatisfy: [:each | '01' includes: each])
ifFalse: [self error: aString printString, ' is not a valid
representation of a binary number'].

The method simply returns self if it succeeds or throws an exception is the
string is invalid. And likewise, the conversion method itself only answers
the integer corresponding to a valid binary string and does not answer
(anything) for an invlaid one.


There is another pattern that could be used, but this particular problem
seems (to me) best suited to an exception. A common pattern in Smalltalk,
one of its greatest innovations, is the #ifAbsent: keyword and others like
it. When you send #at:ifAbsent:, *you* tell the receiver to give you what
you ask for *and* you tell it what *you* want to do if it cannot. There are
infinitely many possible keywords like #ifAbsent:, as you can make up any
one that suits your needs.

If you look at the implementation of #at: or #detect:, you will see that
they are implemented using #at:ifAbsent: and #detect:ifNone: and that the
block provided for the second argument is one which throws an error. Given
all that, perhaps the "best" implementation of your solution would be to
have "decimalFromBinary: aString" implemented in terms of a
"decimalFromBinary: aString ifUnable: aBlock".


>
> but on the first method I see a message that the temp variables are read
> before written.
> and the second one I see a message that I use a or instead of searching
> literals.
>
> Where did  I think wrong here ?
>
> Roelof
>
>
>


Re: [Pharo-users] Non-blocking IO

2020-04-27 Thread Daniel Turczański
Thanks Sven! Looks good to me.



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



Re: [Pharo-users] how can I test this

2020-04-27 Thread Roelof Wobben via Pharo-users
--- Begin Message ---

  
  
Op 27-4-2020 om 18:28 schreef Richard
  Sargent:


  
  

  On Mon, Apr 27, 2020 at 1:56
AM Roelof Wobben via Pharo-users 
wrote:
  
  Hello,

I have to test the server which should return the string but
then in 
uppercase.

So far I have this : 
https://github.com/RoelofWobben/Network-simulator/blob/master/src/NetworkSimulator-Tests/KANetworkTest.class.st#L79

but I fail to see how I can test that the package sending
back is a 
uppercase string.
  
  
  
  I think you need to rephrase this question. Either the
String class hierarchy has a method named something like
#isUppercase or Character does. If only Character has it,
you would need to iterate over the received string testing
each individual character.
  
  
  Or is your question more about getting the response back
to the sending node?
  

  


yep, I want to test if the response back to the sending node is the
same string as the sending node is but then all uppercased. 

Roelof

  


--- End Message ---


Re: [Pharo-users] how can I test this

2020-04-27 Thread Richard Sargent
On Mon, Apr 27, 2020 at 1:56 AM Roelof Wobben via Pharo-users <
pharo-users@lists.pharo.org> wrote:

> Hello,
>
> I have to test the server which should return the string but then in
> uppercase.
>
> So far I have this :
>
> https://github.com/RoelofWobben/Network-simulator/blob/master/src/NetworkSimulator-Tests/KANetworkTest.class.st#L79
>
> but I fail to see how I can test that the package sending back is a
> uppercase string.
>

I think you need to rephrase this question. Either the String class
hierarchy has a method named something like #isUppercase or Character does.
If only Character has it, you would need to iterate over the received
string testing each individual character.

Or is your question more about getting the response back to the sending
node?


> Regards,
>
> Roelof
>
>
>
>
>


[Pharo-users] can I divide a string into part of exactly n length

2020-04-27 Thread Roelof Wobben via Pharo-users
--- Begin Message ---

Hello,

I wonder if it is possible in Pharo to divide a string in lets say part 
of 3.

so this :  'abcdefgh'

would be  'abc def gh`

Regards,

Roelof


--- End Message ---


Re: [Pharo-users] mentor question 2.

2020-04-27 Thread Roelof Wobben via Pharo-users
--- Begin Message ---

  
  
Not any problems. 
  I take the points out of it and forget the things I do not
  understand.
  
  Roelof
  
  
  Op 27-4-2020 om 13:50 schreef PBKResearch:


  
  
  
  
Roelof
 
Sorry if I introduced unnecessary
  technicalities. Your solution to the problem was in fact using
  a polynomial explicitly; that’s what all the powers of 2 were
  doing. But as long as you can follow how my version works,
  that’s all that matters.
 
Peter
 

  From:
  Pharo-users  On
Behalf Of Roelof Wobben via Pharo-users
  Sent: 27 April 2020 12:23
  To: pharo-users@lists.pharo.org
  Cc: Roelof Wobben 
  Subject: Re: [Pharo-users] mentor question 2.

 
Op 27-4-2020 om 13:16 schreef PBKResearch:
Roelof
 
You maybe have enough mentors already, but
  there are some important features of this problem which can be
  of use in future. Your code solves the problem, but it could
  be improved in two ways, which make it simpler and clearer.
 

  What is the
purpose of the local variable ‘isValid’? It provides
somewhere to remember the result of your validity test. But
you have no need to remember it; all you want to do is
evaluate it, use it to determine the course of the program
and then forget it. If you write:

(self
  isValidBinary: aString)
 ifTrue:
  [….]
  
  ifFalse: [^nil]
you will achieve
  the same result without introducing the local variable. It
  probably reads more clearly, revealing the intention of the
  code. (You can simplify further by replacing the ifFalse:
  clause with an unconditional ^nil; if the code reaches this
  point, we know it’s false.) The general principle is: only
  introduce a named local variable if you need to remember
  something for later re-use.
 

  It is not
necessary to reverse the string in order to evaluate it; it
is in fact easier to evaluate it from left to right. You
have used the withIndexDo: method to supply the index and
know the appropriate power of two to use. Instead you could
multiply by two as you process each digit, which will
automatically use the right power of two without ever
needing to know what it is. You could write:

result := 0.
aString do:
  
[:digit| result := result * 2 + digit digitValue].
^result.
This is a
  special case of a very useful general procedure for evaluating
  a polynomial. The digits of a binary number are the
  coefficients of a polynomial in x which is evaluated at x=2;
  similarly, a decimal number is a polynomial evaluated at x=10.
 
I hope this is helpful.
 
Peter Kenny
 
From: Pharo-users

On Behalf Of Roelof Wobben via Pharo-users
Sent: 26 April 2020 19:52
To: pharo-users@lists.pharo.org
Cc: Roelof Wobben 
Subject: Re: [Pharo-users] mentor question 2.
 
Op 26-4-2020 om 20:17
schreef Sven Van Caekenberghe:
> 
>> On 26 Apr 2020, at 20:10,
  Gabriel Cotelli  wrote:
>> 
>> In the first method you aren't
  doing an assignment, are sending the message = to the temp
  isValid, if you change the method to:
>> decimalFromBinary: aString
>>   | result isValid |
>>   isValid := self
  isValidBinary: aString.
>>   isValid
>>   ifTrue: [ result :=
  0.
>>   aString reverse
>>   withIndexDo:
>>   [ :digit
  :index | result := result + (digit 
>> digitValue * (2 raisedTo: index
  - 1)) ].
>>   ^ result ]
>>   ifFalse: [ ^ nil ]
>> it should work.
> There is a #reverseWithIndexDo:
  method
> 
> Also, #digitValue might be
  considered a builtin method that you are 
> not allowed to use. Since you
  already did the #isValidBinary: test, 
> you could say
> 
>    (digit charCode - $0 charCode)
> 
>> In the second one you're
  comparing characters with numbers, this will always return
  false because the number 0 is not the same as the character 0.
>> Use
   

Re: [Pharo-users] mentor question 2.

2020-04-27 Thread PBKResearch
Roelof

 

Sorry if I introduced unnecessary technicalities. Your solution to the
problem was in fact using a polynomial explicitly; that's what all the
powers of 2 were doing. But as long as you can follow how my version works,
that's all that matters.

 

Peter

 

From: Pharo-users  On Behalf Of Roelof
Wobben via Pharo-users
Sent: 27 April 2020 12:23
To: pharo-users@lists.pharo.org
Cc: Roelof Wobben 
Subject: Re: [Pharo-users] mentor question 2.

 

Op 27-4-2020 om 13:16 schreef PBKResearch:

Roelof

 

You maybe have enough mentors already, but there are some important features
of this problem which can be of use in future. Your code solves the problem,
but it could be improved in two ways, which make it simpler and clearer.

 

1.  What is the purpose of the local variable 'isValid'? It provides
somewhere to remember the result of your validity test. But you have no need
to remember it; all you want to do is evaluate it, use it to determine the
course of the program and then forget it. If you write:

(self isValidBinary: aString)

 ifTrue: [..]

   ifFalse: [^nil]

you will achieve the same result without introducing the local variable. It
probably reads more clearly, revealing the intention of the code. (You can
simplify further by replacing the ifFalse: clause with an unconditional
^nil; if the code reaches this point, we know it's false.) The general
principle is: only introduce a named local variable if you need to remember
something for later re-use.

 

2.  It is not necessary to reverse the string in order to evaluate it;
it is in fact easier to evaluate it from left to right. You have used the
withIndexDo: method to supply the index and know the appropriate power of
two to use. Instead you could multiply by two as you process each digit,
which will automatically use the right power of two without ever needing to
know what it is. You could write:

result := 0.

aString do:

   [:digit| result := result * 2 + digit digitValue].

^result.

This is a special case of a very useful general procedure for evaluating a
polynomial. The digits of a binary number are the coefficients of a
polynomial in x which is evaluated at x=2; similarly, a decimal number is a
polynomial evaluated at x=10.

 

I hope this is helpful.

 

Peter Kenny

 

From: Pharo-users  
 On Behalf Of Roelof Wobben via
Pharo-users
Sent: 26 April 2020 19:52
To: pharo-users@lists.pharo.org  
Cc: Roelof Wobben   
Subject: Re: [Pharo-users] mentor question 2.

 

Op 26-4-2020 om 20:17 schreef Sven Van Caekenberghe:

> 

>> On 26 Apr 2020, at 20:10, Gabriel Cotelli mailto:g.cote...@gmail.com> > wrote:

>> 

>> In the first method you aren't doing an assignment, are sending the
message = to the temp isValid, if you change the method to:

>> decimalFromBinary: aString

>>   | result isValid |

>>   isValid := self isValidBinary: aString.

>>   isValid

>>   ifTrue: [ result := 0.

>>   aString reverse

>>   withIndexDo:

>>   [ :digit :index | result := result + (digit 

>> digitValue * (2 raisedTo: index - 1)) ].

>>   ^ result ]

>>   ifFalse: [ ^ nil ]

>> it should work.

> There is a #reverseWithIndexDo: method

> 

> Also, #digitValue might be considered a builtin method that you are 

> not allowed to use. Since you already did the #isValidBinary: test, 

> you could say

> 

>(digit charCode - $0 charCode)

> 

>> In the second one you're comparing characters with numbers, this will
always return false because the number 0 is not the same as the character 0.

>> Use

>> isValidBinary: aString

>>   ^ aString allSatisfy: [ :c | c = $0 or: [ c = $1 ] ] or 

>> something like

>> 

>> isValidBinary: aString

>>   ^ aString allSatisfy: [ :c | '01' includes: c ]

>> 

>> On Sun, Apr 26, 2020 at 2:52 PM Roelof Wobben via Pharo-users
mailto:pharo-users@lists.pharo.org> > wrote:

>> Hello,

>> 

>> I have to make some code that convert a binary to a decimal and im 

>> not allowed to use the convert methods that Pharo has.

>> 

>> So I have written this :

>> 

>> 

>> decimalFromBinary: aString

>>   | result isValid |

>>   isValid = self isValidBinary: aString.

>>   isValid

>>   ifTrue: [ result := 0.

>>   aString reverse

>>   withIndexDo:

>>   [ :digit :index | result := result + (digit 

>> digitValue * (2 raisedTo: index - 1)) ].

>>   ^ result ]

>>   ifFalse: [ ^ nil ]

>> 

>> isValidBinary: aString

>>   ^ aString allSatisfy: [ :c | c = 0 or: [ c = 1 ] ]

>> 

>> 

>> but on the first method I see a message that the temp variables are 

>> read before written.

>> and the second one I see a message that I use a or instead of 

>> searching literals.

>> 

>> Where did  I think wrong here ?

>> 

Re: [Pharo-users] mentor question 2.

2020-04-27 Thread Roelof Wobben via Pharo-users
--- Begin Message ---

  
  
Op 27-4-2020 om 13:16 schreef
  PBKResearch:


  
  
  
  
Roelof
 
You maybe have enough mentors already, but
  there are some important features of this problem which can be
  of use in future. Your code solves the problem, but it could
  be improved in two ways, which make it simpler and clearer.
 

  What is the
purpose of the local variable ‘isValid’? It provides
somewhere to remember the result of your validity test. But
you have no need to remember it; all you want to do is
evaluate it, use it to determine the course of the program
and then forget it. If you write:

(self
  isValidBinary: aString)
 ifTrue:
  [….]
  
  ifFalse: [^nil]
you will achieve
  the same result without introducing the local variable. It
  probably reads more clearly, revealing the intention of the
  code. (You can simplify further by replacing the ifFalse:
  clause with an unconditional ^nil; if the code reaches this
  point, we know it’s false.) The general principle is: only
  introduce a named local variable if you need to remember
  something for later re-use.
 

  It is not
necessary to reverse the string in order to evaluate it; it
is in fact easier to evaluate it from left to right. You
have used the withIndexDo: method to supply the index and
know the appropriate power of two to use. Instead you could
multiply by two as you process each digit, which will
automatically use the right power of two without ever
needing to know what it is. You could write:

result := 0.
aString do:
   [:digit| result := result * 2 + digit digitValue].
^result.
This is a
  special case of a very useful general procedure for evaluating
  a polynomial. The digits of a binary number are the
  coefficients of a polynomial in x which is evaluated at x=2;
  similarly, a decimal number is a polynomial evaluated at x=10.
 
I hope this is helpful.
 
Peter Kenny
 

  From:
  Pharo-users  On
Behalf Of Roelof Wobben via Pharo-users
  Sent: 26 April 2020 19:52
  To: pharo-users@lists.pharo.org
  Cc: Roelof Wobben 
  Subject: Re: [Pharo-users] mentor question 2.
   
  Op 26-4-2020 om 20:17
  schreef Sven Van Caekenberghe:
  > 
  >> On 26 Apr 2020, at 20:10,
Gabriel Cotelli  wrote:
  >> 
  >> In the first method you
aren't doing an assignment, are sending the message = to the
temp isValid, if you change the method to:
  >> decimalFromBinary: aString
  >>   | result isValid |
  >>   isValid := self
isValidBinary: aString.
  >>   isValid
  >>   ifTrue: [ result :=
0.
  >>   aString reverse
  >>  
withIndexDo:
  >>   [
:digit :index | result := result + (digit 
  >> digitValue * (2 raisedTo:
index - 1)) ].
  >>   ^ result ]
  >>   ifFalse: [ ^ nil ]
  >> it should work.
  > There is a #reverseWithIndexDo:
method
  > 
  > Also, #digitValue might be
considered a builtin method that you are 
  > not allowed to use. Since you
already did the #isValidBinary: test, 
  > you could say
  > 
  >    (digit charCode - $0 charCode)
  > 
  >> In the second one you're
comparing characters with numbers, this will always return
false because the number 0 is not the same as the character
0.
  >> Use
  >> isValidBinary: aString
  >>   ^ aString allSatisfy: [
:c | c = $0 or: [ c = $1 ] ] or 
  >> something like
  >> 
  >> isValidBinary: aString
  >>   ^ aString allSatisfy: [
:c | '01' includes: c ]
  >> 
  >> On Sun, Apr 26, 2020 at 2:52
PM Roelof Wobben via Pharo-users 
wrote:
  >> Hello,
  >> 
  >> I have to make some code that
convert a binary to a decimal and im 
  >> not allowed to use the
convert methods that Pharo has.
  >> 
  >> So I have written this :
  >> 
  >> 
  >> dec

Re: [Pharo-users] mentor question 2.

2020-04-27 Thread PBKResearch
Roelof

 

You maybe have enough mentors already, but there are some important features
of this problem which can be of use in future. Your code solves the problem,
but it could be improved in two ways, which make it simpler and clearer.

 

1.  What is the purpose of the local variable 'isValid'? It provides
somewhere to remember the result of your validity test. But you have no need
to remember it; all you want to do is evaluate it, use it to determine the
course of the program and then forget it. If you write:

(self isValidBinary: aString)

 ifTrue: [..]

   ifFalse: [^nil]

you will achieve the same result without introducing the local variable. It
probably reads more clearly, revealing the intention of the code. (You can
simplify further by replacing the ifFalse: clause with an unconditional
^nil; if the code reaches this point, we know it's false.) The general
principle is: only introduce a named local variable if you need to remember
something for later re-use.

 

2.  It is not necessary to reverse the string in order to evaluate it;
it is in fact easier to evaluate it from left to right. You have used the
withIndexDo: method to supply the index and know the appropriate power of
two to use. Instead you could multiply by two as you process each digit,
which will automatically use the right power of two without ever needing to
know what it is. You could write:

result := 0.

aString do:

   [:digit| result := result * 2 + digit digitValue].

^result.

This is a special case of a very useful general procedure for evaluating a
polynomial. The digits of a binary number are the coefficients of a
polynomial in x which is evaluated at x=2; similarly, a decimal number is a
polynomial evaluated at x=10.

 

I hope this is helpful.

 

Peter Kenny

 

From: Pharo-users  On Behalf Of Roelof
Wobben via Pharo-users
Sent: 26 April 2020 19:52
To: pharo-users@lists.pharo.org
Cc: Roelof Wobben 
Subject: Re: [Pharo-users] mentor question 2.

 

Op 26-4-2020 om 20:17 schreef Sven Van Caekenberghe:

> 

>> On 26 Apr 2020, at 20:10, Gabriel Cotelli mailto:g.cote...@gmail.com> > wrote:

>> 

>> In the first method you aren't doing an assignment, are sending the
message = to the temp isValid, if you change the method to:

>> decimalFromBinary: aString

>>   | result isValid |

>>   isValid := self isValidBinary: aString.

>>   isValid

>>   ifTrue: [ result := 0.

>>   aString reverse

>>   withIndexDo:

>>   [ :digit :index | result := result + (digit 

>> digitValue * (2 raisedTo: index - 1)) ].

>>   ^ result ]

>>   ifFalse: [ ^ nil ]

>> it should work.

> There is a #reverseWithIndexDo: method

> 

> Also, #digitValue might be considered a builtin method that you are 

> not allowed to use. Since you already did the #isValidBinary: test, 

> you could say

> 

>(digit charCode - $0 charCode)

> 

>> In the second one you're comparing characters with numbers, this will
always return false because the number 0 is not the same as the character 0.

>> Use

>> isValidBinary: aString

>>   ^ aString allSatisfy: [ :c | c = $0 or: [ c = $1 ] ] or 

>> something like

>> 

>> isValidBinary: aString

>>   ^ aString allSatisfy: [ :c | '01' includes: c ]

>> 

>> On Sun, Apr 26, 2020 at 2:52 PM Roelof Wobben via Pharo-users
mailto:pharo-users@lists.pharo.org> > wrote:

>> Hello,

>> 

>> I have to make some code that convert a binary to a decimal and im 

>> not allowed to use the convert methods that Pharo has.

>> 

>> So I have written this :

>> 

>> 

>> decimalFromBinary: aString

>>   | result isValid |

>>   isValid = self isValidBinary: aString.

>>   isValid

>>   ifTrue: [ result := 0.

>>   aString reverse

>>   withIndexDo:

>>   [ :digit :index | result := result + (digit 

>> digitValue * (2 raisedTo: index - 1)) ].

>>   ^ result ]

>>   ifFalse: [ ^ nil ]

>> 

>> isValidBinary: aString

>>   ^ aString allSatisfy: [ :c | c = 0 or: [ c = 1 ] ]

>> 

>> 

>> but on the first method I see a message that the temp variables are 

>> read before written.

>> and the second one I see a message that I use a or instead of 

>> searching literals.

>> 

>> Where did  I think wrong here ?

>> 

>> Roelof

>> 

>> 

> 

 

 

Thanks,  this problem is solved.

 

Roelof

 

 

 



[Pharo-users] how can I test this

2020-04-27 Thread Roelof Wobben via Pharo-users
--- Begin Message ---

Hello,

I have to test the server which should return the string but then in 
uppercase.


So far I have this : 
https://github.com/RoelofWobben/Network-simulator/blob/master/src/NetworkSimulator-Tests/KANetworkTest.class.st#L79


but I fail to see how I can test that the package sending back is a 
uppercase string.


Regards,

Roelof




--- End Message ---