how can I this refractor this so its more smalltalk

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

how can I this refractor this so its more smalltalk

Pharo Smalltalk Users mailing list
Hello,

My solution to day2 part1 is right this :

processData: instructions
     | opcode index firstNumber secondNumber placeToPut firstNumberIndex
secondNumberIndex |
     index := 1.
     opcode := instructions at: index.
     [ opcode ~= 99 ]
         whileTrue: [ firstNumberIndex := instructions at: index + 1.
             secondNumberIndex := instructions at: index + 2.
             firstNumber := instructions at: firstNumberIndex + 1.
             secondNumber := instructions at: secondNumberIndex + 1.
             placeToPut := (instructions at: index + 3) + 1.
             opcode == 1
                 ifTrue: [ instructions at: placeToPut put: firstNumber
+ secondNumber ].
             opcode == 2
                 ifTrue: [ instructions at: placeToPut put: firstNumber
* secondNumber ].
             index := index + 4.
             opcode := instructions at: index ].
     ^ instructions at: 1

so its ugly code

is there a way  I can this more the smalltalk way by using streams or
something else.
if so, is there someone who can tell me or can let me see how to make
this cleaner code

Roelof


Reply | Threaded
Open this post in threaded view
|

Re: how can I this refractor this so its more smalltalk

Kasper Osterbye
Hi Roelof

I saw your posts earlier, and will try to take a look. I am not into the advent of code myself, but love a good puzzle. Would it be possible to provide a link to the puzzle you are trying to solve?
Reply | Threaded
Open this post in threaded view
|

Re: how can I this refractor this so its more smalltalk

Pharo Smalltalk Users mailing list
of course but you have to use a githut account to reach it.

All the puzzles can be found here :  https://adventofcode.com/

I try to solve day2 and then part1.

Roelof


Op 17-12-2019 om 18:33 schreef Kasper Osterbye:
Hi Roelof

I saw your posts earlier, and will try to take a look. I am not into the advent of code myself, but love a good puzzle. Would it be possible to provide a link to the puzzle you are trying to solve?

Reply | Threaded
Open this post in threaded view
|

Re: how can I this refractor this so its more smalltalk

Pharo Smalltalk Users mailing list
In reply to this post by Pharo Smalltalk Users mailing list
Well my main run method looks like this:
run
| op | 
ram := self input.
in := ReadStream on: ram.
[ (op := in next) = 99 ] whileFalse: [ self processOpcode: op ].
^self at: 0.

ram is the array of integers.
in is a ReadStream on them which lets me process them in order.
They are both instance variables.

self input provides a clean copy of the program memory (the integer array).  I just used an array literal.  No file IO.

The main dispatch method is

processOpcode: op 

op = 1 ifTrue: [ ^self processAdd ].
op = 2 ifTrue: [ ^self processMultiply ].

and for illustration 

processAdd
| a b c |
a := self at: in next.
b := self at: in next.
self at: in next put: a+b

There are custom versions of at: and at:put: that compensate for zero based vs one based array indexing.

at: idx
^ram at: idx + 1

I hope this helps you out.

On Dec 17, 2019, at 6:59 AM, Roelof Wobben via Pharo-users <[hidden email]> wrote:


From: Roelof Wobben <[hidden email]>
Subject: how can I this refractor this so its more smalltalk
Date: December 17, 2019 at 6:59:28 AM PST
To: Any question about pharo is welcome <[hidden email]>


Hello,

My solution to day2 part1 is right this :

processData: instructions
    | opcode index firstNumber secondNumber placeToPut firstNumberIndex secondNumberIndex |
    index := 1.
    opcode := instructions at: index.
    [ opcode ~= 99 ]
        whileTrue: [ firstNumberIndex := instructions at: index + 1.
            secondNumberIndex := instructions at: index + 2.
            firstNumber := instructions at: firstNumberIndex + 1.
            secondNumber := instructions at: secondNumberIndex + 1.
            placeToPut := (instructions at: index + 3) + 1.
            opcode == 1
                ifTrue: [ instructions at: placeToPut put: firstNumber + secondNumber ].
            opcode == 2
                ifTrue: [ instructions at: placeToPut put: firstNumber * secondNumber ].
            index := index + 4.
            opcode := instructions at: index ].
    ^ instructions at: 1

so its ugly code

is there a way  I can this more the smalltalk way by using streams or something else.
if so, is there someone who can tell me or can let me see how to make this cleaner code

Roelof





Reply | Threaded
Open this post in threaded view
|

Re: how can I this refractor this so its more smalltalk

Ben Coman
In reply to this post by Pharo Smalltalk Users mailing list
Apart from using streams, part of making code less ugly is splitting it up its separate aaspects.
One way it to to do as little as possible within your loop.  See how Todd only does one thing within his loop.
Apart from looking nicer, doing so improves how you "think" about the problem.

cheers -ben

On Tue, 17 Dec 2019 at 23:00, Roelof Wobben via Pharo-users <[hidden email]> wrote:
Hello,

My solution to day2 part1 is right this :

processData: instructions
     | opcode index firstNumber secondNumber placeToPut firstNumberIndex
secondNumberIndex |
     index := 1.
     opcode := instructions at: index.
     [ opcode ~= 99 ]
         whileTrue: [ firstNumberIndex := instructions at: index + 1.
             secondNumberIndex := instructions at: index + 2.
             firstNumber := instructions at: firstNumberIndex + 1.
             secondNumber := instructions at: secondNumberIndex + 1.
             placeToPut := (instructions at: index + 3) + 1.
             opcode == 1
                 ifTrue: [ instructions at: placeToPut put: firstNumber
+ secondNumber ].
             opcode == 2
                 ifTrue: [ instructions at: placeToPut put: firstNumber
* secondNumber ].
             index := index + 4.
             opcode := instructions at: index ].
     ^ instructions at: 1

so its ugly code

is there a way  I can this more the smalltalk way by using streams or
something else.
if so, is there someone who can tell me or can let me see how to make
this cleaner code

Roelof