how do I send the 2 messsages to the function

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

how do I send the 2 messsages to the function

Roelof
Hello,

I have made this function :

calculateNewFloor: aFloor index: index
     "comment stating purpose of message"
    |action floor |
    action:= SantaAction getActionFor: aFloor. floor := action
doMovementFor: aFloor

now I want to send a message to this function  :

findBasement: input2
     "solution to part1 of this challenge"

      input2 withIndexDo: [ :element :index | |action| action:=
SantaFloorAction getActionFor: element. floor := action
calculateNewFloor: floor ..  ].
      ^ counter

but how do I get the second argument in this

Roelof



Reply | Threaded
Open this post in threaded view
|

Re: how do I send the 2 messsages to the function

dorellang
What you want to do is:

findBasement: input2
     "solution to part1 of this challenge"

      input2 withIndexDo: [ :element :index | |action| action:=
SantaFloorAction getActionFor: element. floor := action
calculateNewFloor: floor index: index  ].
      ^ counter

Notice how I added the 'index: index' bit in the line just above the '^
counter', so you're actually performing:

action calculateNewFloor: floor index: index

If you had a method with three args you would declare it as:

methodWithArg1: arg1 arg2: arg2 arg3: arg3
   "assume this is in Foo class"
   self doSomething.

and then you would call it in another place with

...
"assume obj is some instance of Foo"
obj methodWithArg1: 'some' arg2: 'arguments' arg3: #thatMightChange
...

BTW the way you're framing the question makes me think you are not
understanding how Smalltalk works. In Smalltalk (and by extension, in Pharo)
you don't send messages to functions, you send messages to objects (all
values that you can store in variables are objects in Smalltalk). In this
context 'send message' is what in other languages is  'call the object's
instance method with some arguments'. So you may be wondering: what's a
'message' anyway? A message is a method name (or more properly in the
Smalltalk slang, its 'selector') plus some arguments. The idea is that what
actually happens when a message is received by an object depends on the
concrete implementation that that object has for that message; in other
words, the method the object has; which may vary according the class of the
object that received the message.

Actually it's not unlike other object oriented languages, but the
terminology and the syntax might be a little weird at first.

Cheers,
Diego



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

Reply | Threaded
Open this post in threaded view
|

Re: how do I send the 2 messsages to the function

Nicole de Graaf
In reply to this post by Roelof
Hi Roelof,

is not easy to understand the question , without contents.

#calculateNewFloor:index:
- I assume you implemented the method on an action class (SantaAction). 
- In the method I see no usage from argument index 

but to send the method with a index:

aFoor := aAction calculateNewFloor: aFloor index: anIndex.

the method does not return a  floor instance , as you expect in #findBasement:


I hope you can solve you Santa puzzel,
Nic




On 25 Nov 2018, at 17:42, Roelof Wobben <[hidden email]> wrote:

Hello,

I have made this function :

calculateNewFloor: aFloor index: index
    "comment stating purpose of message"
   |action floor |
   action:= SantaAction getActionFor: aFloor. floor := action doMovementFor: aFloor

now I want to send a message to this function  :

findBasement: input2
    "solution to part1 of this challenge"

     input2 withIndexDo: [ :element :index | |action| action:= SantaFloorAction getActionFor: element. floor := action calculateNewFloor: floor ..  ].
     ^ counter

but how do I get the second argument in this

Roelof




Reply | Threaded
Open this post in threaded view
|

Re: how do I send the 2 messsages to the function

Roelof
In reply to this post by dorellang
Op 25-11-2018 om 19:12 schreef dorellang:

> What you want to do is:
>
> findBasement: input2
>       "solution to part1 of this challenge"
>
>        input2 withIndexDo: [ :element :index | |action| action:=
> SantaFloorAction getActionFor: element. floor := action
> calculateNewFloor: floor index: index  ].
>        ^ counter
>
> Notice how I added the 'index: index' bit in the line just above the '^
> counter', so you're actually performing:
>
> action calculateNewFloor: floor index: index
>
> If you had a method with three args you would declare it as:
>
> methodWithArg1: arg1 arg2: arg2 arg3: arg3
>     "assume this is in Foo class"
>     self doSomething.
>
> and then you would call it in another place with
>
> ...
> "assume obj is some instance of Foo"
> obj methodWithArg1: 'some' arg2: 'arguments' arg3: #thatMightChange
> ...
>
> BTW the way you're framing the question makes me think you are not
> understanding how Smalltalk works. In Smalltalk (and by extension, in Pharo)
> you don't send messages to functions, you send messages to objects (all
> values that you can store in variables are objects in Smalltalk). In this
> context 'send message' is what in other languages is  'call the object's
> instance method with some arguments'. So you may be wondering: what's a
> 'message' anyway? A message is a method name (or more properly in the
> Smalltalk slang, its 'selector') plus some arguments. The idea is that what
> actually happens when a message is received by an object depends on the
> concrete implementation that that object has for that message; in other
> words, the method the object has; which may vary according the class of the
> object that received the message.
>
> Actually it's not unlike other object oriented languages, but the
> terminology and the syntax might be a little weird at first.
>
> Cheers,
> Diego
>
>
>
> --
> Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html
>
>

I know.
Still trying to get a feeling about it
and what takes me some time is that the use of if then is a bad smell
I try to stretch my mind around that

Roelof





Reply | Threaded
Open this post in threaded view
|

Re: how do I send the 2 messsages to the function

Roelof
In reply to this post by Nicole de Graaf
Thanks,

I made a file out with all the code I have right now.
The code can be found here :  https://gist.github.com/RoelofWobben/00829deff93e98523f5090383a656582

But still I cannot make it work.

What I did is make a class SantaFloorAction with 2 subclasses

BasementIsReached and BasementNotReached.

What I try to do it when the basement is reached store the contents of the index into counter because that is the number we are looking for.
When the basement is not reached we need to calculate the next floor.

Hope that makes it more clear and someone can help/teach me where I have done wrong.

Roelof




Op 25-11-2018 om 19:13 schreef Nicole de Graaf:
Hi Roelof,

is not easy to understand the question , without contents.

#calculateNewFloor:index:
- I assume you implemented the method on an action class (SantaAction). 
- In the method I see no usage from argument index 

but to send the method with a index:

aFoor := aAction calculateNewFloor: aFloor index: anIndex.

the method does not return a  floor instance , as you expect in #findBasement:


I hope you can solve you Santa puzzel,
Nic




On 25 Nov 2018, at 17:42, Roelof Wobben <[hidden email]> wrote:

Hello,

I have made this function :

calculateNewFloor: aFloor index: index
    "comment stating purpose of message"
   |action floor |
   action:= SantaAction getActionFor: aFloor. floor := action doMovementFor: aFloor

now I want to send a message to this function  :

findBasement: input2
    "solution to part1 of this challenge"

     input2 withIndexDo: [ :element :index | |action| action:= SantaFloorAction getActionFor: element. floor := action calculateNewFloor: floor ..  ].
     ^ counter

but how do I get the second argument in this

Roelof