Newbie question

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

Newbie question

horrido
I want to do something like...
 
#('some text 1'  'some text 2'  'some text 3'  'some text 4')
with: "a corresponding list of classes, say, {class1  class2   class3  class4}"
do:
    [:each1 :each2 |
    "do something with <each1 text> and <each2 class> pair"]
 
 
No matter how I try to code the #with: part, I keep getting thrown into debug. How do you specify a collection of classes for the above example?
 
Thanks,
Richard


Reply | Threaded
Open this post in threaded view
|

RE: Newbie question

michaelperscheid
Hi Richard,

I hope this can help you. Try something like this
#(1 2) with: #(3 4) do: [:a :b | Transcript show: a*b asString; cr]

Attention: Both collections must have the same size.

Regards
Michael Perscheid

PS: Use the beginner list for such questions, too.


________________________________________
Von: [hidden email]
[mailto:[hidden email]] Im Auftrag von
Richard Eng
Gesendet: Montag, 23. Juli 2007 16:08
An: The general-purpose Squeak developers list
Betreff: Newbie question

I want to do something like...
 
#('some text 1'  'some text 2'  'some text 3'  'some text 4')
with: "a corresponding list of classes, say, {class1  class2   class3 
class4}"
do:
    [:each1 :each2 |
    "do something with <each1 text> and <each2 class> pair"]
 
 
No matter how I try to code the #with: part, I keep getting thrown into
debug. How do you specify a collection of classes for the above example?
 
Thanks,
Richard


Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

Bert Freudenberg
In reply to this post by horrido

On Jul 23, 2007, at 16:07 , Richard Eng wrote:

> I want to do something like...
>
> #('some text 1'  'some text 2'  'some text 3'  'some text 4')
> with: "a corresponding list of classes, say, {class1  class2    
> class3  class4}"
> do:
>     [:each1 :each2 |
>     "do something with <each1 text> and <each2 class> pair"]
>
>
> No matter how I try to code the #with: part, I keep getting thrown  
> into debug. How do you specify a collection of classes for the  
> above example?

{...} is a list of expressions separated by dots:

{ Class1. Class2. 3+4. 12@13. 5 class. Zork froble: 7 with: 42 }


- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

Nicolas Cellier-3
In reply to this post by horrido
Richard Eng <horrido.hobbies <at> gmail.com> writes:

>
>
> I want to do something like...
>  
> #('some text 1'  'some text 2'  'some text 3'  'some text 4')
> with: "a corresponding list of classes, say, {class1  class2   class3  class4}"
> do:
>     [:each1 :each2 |
>     "do something with <each1 text> and <each2 class> pair"]
>  


Use a period separator in brace construct.
{Class1. Class2. Class3. Class4}

Otherwise, it is interpreted as send message: class4 to: (result of send
message: class3 to: (result of send message: class2 to: class1))

Nicolas




Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

horrido
Thanks! I didn't know about the period.
 
(I unsubscribed from the Newbie list because I rarely have newbie questions and I didn't want to get all the email "noise" from that list.)
 
Regards,
Richard

 
On 7/23/07, nicolas cellier <[hidden email]> wrote:
Richard Eng <horrido.hobbies <at> gmail.com> writes:

>
>
> I want to do something like...
>
> #('some text 1'  'some text 2'  'some text 3'  'some text 4')
> with: "a corresponding list of classes, say, {class1  class2   class3  class4}"
> do:
>     [:each1 :each2 |
>     "do something with <each1 text> and <each2 class> pair"]
>


Use a period separator in brace construct.
{Class1. Class2. Class3. Class4}

Otherwise, it is interpreted as send message: class4 to: (result of send
message: class3 to: (result of send message: class2 to: class1))

Nicolas







Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

Bert Freudenberg
You rather get the noise of squeak-dev? Brave man!

- Bert -

On Jul 23, 2007, at 16:32 , Richard Eng wrote:

> Thanks! I didn't know about the period.
>
> (I unsubscribed from the Newbie list because I rarely have newbie  
> questions and I didn't want to get all the email "noise" from that  
> list.)
>
> Regards,
> Richard
>
>
> On 7/23/07, nicolas cellier <[hidden email]> wrote: Richard  
> Eng <horrido.hobbies <at> gmail.com> writes:
>
> >
> >
> > I want to do something like...
> >
> > #('some text 1'  'some text 2'  'some text 3'  'some text 4')
> > with: "a corresponding list of classes, say, {class1  class2    
> class3  class4}"
> > do:
> >     [:each1 :each2 |
> >     "do something with <each1 text> and <each2 class> pair"]
> >
>
>
> Use a period separator in brace construct.
> {Class1. Class2. Class3. Class4}
>
> Otherwise, it is interpreted as send message: class4 to: (result of  
> send
> message: class3 to: (result of send message: class2 to: class1))
>
> Nicolas
>
>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

K. K. Subramaniam
In reply to this post by horrido
On Monday 23 July 2007 8:02 pm, Richard Eng wrote:
> Thanks! I didn't know about the period.
It is one of those dark little secrets in Smalltalk :-). Unlike other
Smalltalk expressions, it doesn't follow the "<receiver> <message>" pattern.
 b := { x+1.x+2.x+3}.
is equivalent to:
 b := Array new: 3.
 b at: 1 put: x+1.
 b at: 2 put: x+2.
 b at: 3 put: x+3.

The period is really a separator and not an operator. Since period is also
used as a decimal point, one needs to watch out for gotchas like:
 {1.2.3} = an array with two elements 1.2 and 3
 {1 . 2 . 3} = an array with three elements 1, 2 and 3

I wonder how period came to be used as a separator within braces in Smalltalk
instead of comma or semicolon.

Regards .. Subbu

Reply | Threaded
Open this post in threaded view
|

RE: Newbie question

Ramon Leon-5
> The period is really a separator and not an operator. Since
> period is also used as a decimal point, one needs to watch
> out for gotchas like:
>  {1.2.3} = an array with two elements 1.2 and 3
>  {1 . 2 . 3} = an array with three elements 1, 2 and 3
>
> I wonder how period came to be used as a separator within
> braces in Smalltalk instead of comma or semicolon.
>
> Regards .. Subbu

This isn't common to all Smalltalks, it's a Squeak idiom as far as I know,
though others may have some form of literal array as well.  It also makes
perfect sense to use a period, since the period is "the" statement seperator
for expressions in Smalltalk.

Ramon Leon


Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

Bert Freudenberg

On Jul 23, 2007, at 19:59 , Ramon Leon wrote:

>> The period is really a separator and not an operator. Since
>> period is also used as a decimal point, one needs to watch
>> out for gotchas like:
>>  {1.2.3} = an array with two elements 1.2 and 3
>>  {1 . 2 . 3} = an array with three elements 1, 2 and 3
>>
>> I wonder how period came to be used as a separator within
>> braces in Smalltalk instead of comma or semicolon.
>>
>> Regards .. Subbu
>
> This isn't common to all Smalltalks, it's a Squeak idiom as far as  
> I know,
> though others may have some form of literal array as well.  It also  
> makes
> perfect sense to use a period, since the period is "the" statement  
> seperator
> for expressions in Smalltalk.

Right - just like a block it evaluates all statements in turn,  
separated by periods. But whereas a block returns the result of the  
last statement, the brace construct collects all results and returns  
them as an array.

Now, at one point the compiler even supported this:

        {a. b} := {1. 2}

which I found cool but was considered evil, even by those who  
tolerate the braces ...

- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

Igor Stasenko
{ 'some text1' -> Class1. 'some text2' -> Class2. } do: [:each |
  each key -- for text
  each value  ---- for class
].

On 23/07/07, Bert Freudenberg <[hidden email]> wrote:

>
> On Jul 23, 2007, at 19:59 , Ramon Leon wrote:
>
> >> The period is really a separator and not an operator. Since
> >> period is also used as a decimal point, one needs to watch
> >> out for gotchas like:
> >>  {1.2.3} = an array with two elements 1.2 and 3
> >>  {1 . 2 . 3} = an array with three elements 1, 2 and 3
> >>
> >> I wonder how period came to be used as a separator within
> >> braces in Smalltalk instead of comma or semicolon.
> >>
> >> Regards .. Subbu
> >
> > This isn't common to all Smalltalks, it's a Squeak idiom as far as
> > I know,
> > though others may have some form of literal array as well.  It also
> > makes
> > perfect sense to use a period, since the period is "the" statement
> > seperator
> > for expressions in Smalltalk.
>
> Right - just like a block it evaluates all statements in turn,
> separated by periods. But whereas a block returns the result of the
> last statement, the brace construct collects all results and returns
> them as an array.
>
> Now, at one point the compiler even supported this:
>
>         {a. b} := {1. 2}
>
> which I found cool but was considered evil, even by those who
> tolerate the braces ...
>
> - Bert -
>
>
>
>


--
Best regards,
Igor Stasenko AKA sig.

Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

K. K. Subramaniam
In reply to this post by Bert Freudenberg
On Monday 23 July 2007 11:39 pm, Bert Freudenberg wrote:
> Right - just like a block it evaluates all statements in turn,
> separated by periods. But whereas a block returns the result of the
> last statement, the brace construct collects all results and returns
> them as an array.
Braces differ from blocks in another aspect too. The order of evaluation need
not be specified for a brace construct.

But this still doesn't answer why period was chosen to be a separator in spite
of the parsing ambiguity while other non-ambiguous tokens were available?

> Now, at one point the compiler even supported this:
>
> {a. b} := {1. 2}
>
> which I found cool but was considered evil, even by those who
> tolerate the braces ...
To be really evil, the order of eval should be unspecified :-). Imagine each
expr being evaluated by a separate kernel thread on a multiprocessor machine!

Regards .. Subbu

Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

Blake-5
On Mon, 23 Jul 2007 12:27:21 -0700, subbukk <[hidden email]> wrote:

> On Monday 23 July 2007 11:39 pm, Bert Freudenberg wrote:
>
>> Now, at one point the compiler even supported this:
>>
>> {a. b} := {1. 2}
>>
>> which I found cool but was considered evil, even by those who
>> tolerate the braces ...

What would this do? It looks like you're assigning a literal to another  
literal?

Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

timrowledge

On 23-Jul-07, at 23-Jul;12:58 PM, Blake wrote:

> On Mon, 23 Jul 2007 12:27:21 -0700, subbukk <[hidden email]> wrote:
>
>> On Monday 23 July 2007 11:39 pm, Bert Freudenberg wrote:
>>
>>> Now, at one point the compiler even supported this:
>>>
>>> {a. b} := {1. 2}
>>>
>>> which I found cool but was considered evil, even by those who
>>> tolerate the braces ...
>
> What would this do? It looks like you're assigning a literal to  
> another literal?
You don't want to know. It's evil. Your brain will dissolve. You will  
get spots on your naughty bits.

tim
--
tim Rowledge; [hidden email]; http://www.rowledge.org/tim
Useful random insult:- She doesn't suffer from insanity; she enjoys  
every minute of it.



Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

David Mitchell-10
In reply to this post by Blake-5
For why, you'd have to ask Dan Ingalls (di).

Looks like most of the methods around brace expressions were added on
11/19/1999. I seem to recall something being announced to squeak-dev,
but I couldn't find it in the archives.



On 7/23/07, Blake <[hidden email]> wrote:

> On Mon, 23 Jul 2007 12:27:21 -0700, subbukk <[hidden email]> wrote:
>
> > On Monday 23 July 2007 11:39 pm, Bert Freudenberg wrote:
> >
> >> Now, at one point the compiler even supported this:
> >>
> >>      {a. b} := {1. 2}
> >>
> >> which I found cool but was considered evil, even by those who
> >> tolerate the braces ...
>
> What would this do? It looks like you're assigning a literal to another
> literal?
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

Blake-5
In reply to this post by timrowledge
On Mon, 23 Jul 2007 13:05:07 -0700, tim Rowledge <[hidden email]> wrote:

>
> On 23-Jul-07, at 23-Jul;12:58 PM, Blake wrote:
>
>> On Mon, 23 Jul 2007 12:27:21 -0700, subbukk <[hidden email]> wrote:
>>
>>> On Monday 23 July 2007 11:39 pm, Bert Freudenberg wrote:
>>>
>>>> Now, at one point the compiler even supported this:
>>>>
>>>> {a. b} := {1. 2}
>>>>
>>>> which I found cool but was considered evil, even by those who
>>>> tolerate the braces ...
>>
>> What would this do? It looks like you're assigning a literal to another  
>> literal?
> You don't want to know. It's evil. Your brain will dissolve. You will  
> get spots on your naughty bits.

It's the Ark of the Covenant?

Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

Bert Freudenberg
In reply to this post by Blake-5

On Jul 23, 2007, at 21:58 , Blake wrote:

> On Mon, 23 Jul 2007 12:27:21 -0700, subbukk <[hidden email]> wrote:
>
>> On Monday 23 July 2007 11:39 pm, Bert Freudenberg wrote:
>>
>>> Now, at one point the compiler even supported this:
>>>
>>> {a. b} := {1. 2}
>>>
>>> which I found cool but was considered evil, even by those who
>>> tolerate the braces ...
>
> What would this do? It looks like you're assigning a literal to  
> another literal?

No, it is equivalent to

        a := 1.
        b := 2.

The left-hand side must only be variables separated by dots.

This allows to swap two variables without a temp:

        {a. b} := {b. a}

which admittedly you rarely need to do except in some special  
algorithms.

Or, you could use it for returning multiple objects from a message  
and store immediately in some temps. Which admittedly is bad style,  
you should use an object for that (or pass a block in).

So there are some who think this is a useful construct, also many  
"scripting languages" have it. But it goes against the "spirit" of  
Smalltalk. Assignment itself is not "pure" but this kind of multi-
assignment was considered  to do more harm than good. Whereas the  
brace construct for array construction was too useful to pass.

- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

Blake-5
On Mon, 23 Jul 2007 13:51:33 -0700, Bert Freudenberg  
<[hidden email]> wrote:

> So there are some who think this is a useful construct, also many  
> "scripting languages" have it. But it goes against the "spirit" of  
> Smalltalk. Assignment itself is not "pure" but this kind of multi-
> assignment was considered  to do more harm than good. Whereas the brace  
> construct for array construction was too useful to pass.

Whoa. Reminds me of PL/I.

PL/I also had a "BY NAME" directive, which would allow you to assign two  
not-identical structures by their common fields, regardless of order.

I like it. I guess that makes me evil.

Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

K. K. Subramaniam
In reply to this post by Bert Freudenberg
On Tuesday 24 July 2007 2:21 am, Bert Freudenberg wrote:
> No, it is equivalent to
>
> a := 1.
> b := 2.
This interpretation implies the sequence eval-assign-eval-assign instead of
eval-eval-assign-assign. If a=1 and b=2 just before evaluating the statement
  {a . b } := { 5 . a+2 }
then b=7 per above interpretation while b=3 if braces are evaluated before
assignment. Are brace elements fully evaluated before assignment?

Regards ..  Subbu

Reply | Threaded
Open this post in threaded view
|

Re: Newbie question

Bert Freudenberg

On Jul 24, 2007, at 12:06 , subbukk wrote:

> On Tuesday 24 July 2007 2:21 am, Bert Freudenberg wrote:
>> No, it is equivalent to
>>
>> a := 1.
>> b := 2.
> This interpretation implies the sequence eval-assign-eval-assign  
> instead of
> eval-eval-assign-assign. If a=1 and b=2 just before evaluating the  
> statement
>   {a . b } := { 5 . a+2 }
> then b=7 per above interpretation while b=3 if braces are evaluated  
> before
> assignment. Are brace elements fully evaluated before assignment?

Yes. Right-hand-side was evaluated first.

And mind that this is *not* supported by the compiler anymore.

- Bert -



Reply | Threaded
Open this post in threaded view
|

RE: Newbie question

J J-6
In reply to this post by horrido
Well, it's syntactic sugar.  It looks pretty alien in Smalltalk, but other languages support multiple-assignment (e.g. "swap"  {a. b} := {b. a}).  Of course this gets tricky because if you have multiple assignment it seems natural to return multiple values from a function (e.g. {a. b} := a swapWith: b).  That would probably require a big change to implement and you end up with something Smalltalk can already do other ways, and become (more) incompatible with other dialects.

> To: [hidden email]
> From: [hidden email]
> Date: Mon, 23 Jul 2007 12:58:25 -0700
> Subject: Re: Newbie question
>
> On Mon, 23 Jul 2007 12:27:21 -0700, subbukk <[hidden email]> wrote:
>
> > On Monday 23 July 2007 11:39 pm, Bert Freudenberg wrote:
> >
> >> Now, at one point the compiler even supported this:
> >>
> >> {a. b} := {1. 2}
> >>
> >> which I found cool but was considered evil, even by those who
> >> tolerate the braces ...
>
> What would this do? It looks like you're assigning a literal to another
> literal?
>


Don't get caught with egg on your face.    Play Chicktionary! 

12