Dictionary Literals

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

Dictionary Literals

horrido
According to a Pharoer, there is no literal like #{...} in Pharo. But there is in Amber!

Is this a dictionary literal? I found that this works:

| dict |
dict := #{'foo'->'brown'. 'bar'->'yellow'.
          'qix'->'white'. 'baz'->'red'. 'flub'->'green'} asDictionary.
dict at: 'qix'
If I PrintIt, I get 'white'. If I remove #asDictionary, I still get 'white'!

--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Dictionary Literals

Herby Vojčík
It creates `HashedCollection`, not `Dictionary`, but both are Dictionary-like (except `HashedCollection` can only use strings as keys and is implemented using plain JS object, so you can pass it to JS libraries where key-value object is assumed).

Yes, it is Amber-specific.

Richard Eng wrote:

> According to a Pharoer, there is no literal like *#{...}* in Pharo.
> But there is in Amber!
>
> Is this a dictionary literal? I found that this works:
>
> || dict |
> dict := #{'foo'->'brown'. 'bar'->'yellow'.
>            'qix'->'white'. 'baz'->'red'. 'flub'->'green'} asDictionary.
> dict at: 'qix'|
> If I *PrintIt*, I get 'white'. If I remove #asDictionary, I still get
> 'white'!
>
> --
> You received this message because you are subscribed to the Google
> Groups "amber-lang" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [hidden email]
> <mailto:[hidden email]>.
> For more option
s, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Dictionary Literals

Herby Vojčík


Herby Vojčík wrote:

> It creates `HashedCollection`, not `Dictionary`, but both are
> Dictionary-like (except `HashedCollection` can only use strings as
> keys and is implemented using plain JS object, so you can pass it to
> JS libraries where key-value object is assumed).
>
> Yes, it is Amber-specific.
>
> Richard Eng wrote:
>> According to a Pharoer, there is no literal like *#{...}* in Pharo.
>> But there is in Amber!
>>
>> Is this a dictionary literal? I found that this works:
>>
>> || dict |
>> dict := #{'foo'->'brown'. 'bar'->'yellow'.
>> 'qix'->'white'. 'baz'->'red'. 'flub'->'green'} asDictionary.
>> dict at: 'qix'|
>> If I *PrintIt*, I get 'white'. If I remove #asDictionary, I still get
>> 'white'!

BTW, 'Inspect it' on the literal itself would answer your question on what it is immediately ;-)

--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Dictionary Literals

horrido
Whoa! That's cool!

I think it's best to avoid these literals when discussing Smalltalk in general. Anything Amber-specific will only confuse a novice. So, instead of a literal, just do:

dict := Dictionary new.
dict
    at
: 'foo' put: 'brown';
    at
: 'bar' put: 'yellow';
    at
: 42 put: 'red'.

This way, you aren't limited to just string values or number values; you can mix and match!


On Friday, 5 June 2015 11:49:57 UTC-4, Herby wrote:


Herby Vojčík wrote:

> It creates `HashedCollection`, not `Dictionary`, but both are
> Dictionary-like (except `HashedCollection` can only use strings as
> keys and is implemented using plain JS object, so you can pass it to
> JS libraries where key-value object is assumed).
>
> Yes, it is Amber-specific.
>
> Richard Eng wrote:
>> According to a Pharoer, there is no literal like *#{...}* in Pharo.
>> But there is in Amber!
>>
>> Is this a dictionary literal? I found that this works:
>>
>> || dict |
>> dict := #{'foo'->'brown'. 'bar'->'yellow'.
>> 'qix'->'white'. 'baz'->'red'. 'flub'->'green'} asDictionary.
>> dict at: 'qix'|
>> If I *PrintIt*, I get 'white'. If I remove #asDictionary, I still get
>> 'white'!

BTW, 'Inspect it' on the literal itself would answer your question on what it is immediately ;-)

--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Dictionary Literals

Herby Vojčík


Richard Eng wrote:
> Whoa! That's cool!
>
> I think it's best to avoid these literals when discussing Smalltalk in
> general. Anything Amber-specific will only confuse a novice. So,
> instead of a literal, just do:

I disagree. Smalltalk syntax is so minimal (even with {...} dynamic arrays), one more element does not hurt, and the resulting code is much more readable.

Moreover, Smalltalk encourages explorative programming. You can't escape having #{'key'->value. ...} explained, or the novice just won't understand the code he explores.

A note that #{...} is Amber-specific may be enough.

--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Dictionary Literals

horrido
Okay, I'll buy that.

Btw, this discussion was started because I've been using a dictionary to simulate a Case Statement. However, I've discovered that the major shortcoming of using a dictionary is that you can't include a "default" case. While you can have a default case at the point of selection...

dict at: inputValue ifAbsent: ["default action"].

it's inconvenient that the default case is not directly associated with the dictionary. Any thoughts?


On Friday, 5 June 2015 12:41:19 UTC-4, Herby wrote:


Richard Eng wrote:
> Whoa! That's cool!
>
> I think it's best to avoid these literals when discussing Smalltalk in
> general. Anything Amber-specific will only confuse a novice. So,
> instead of a literal, just do:

I disagree. Smalltalk syntax is so minimal (even with {...} dynamic arrays), one more element does not hurt, and the resulting code is much more readable.

Moreover, Smalltalk encourages explorative programming. You can't escape having #{'key'->value. ...} explained, or the novice just won't understand the code he explores.

A note that #{...} is Amber-specific may be enough.

--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Dictionary Literals

Herby Vojčík


Richard Eng wrote:

> Okay, I'll buy that.
>
> Btw, this discussion was started because I've been using a dictionary
> to simulate a Case Statement. However, I've discovered that the major
> shortcoming of using a dictionary is that you can't include a
> "default" case. While you can have a default case at the point of
> selection...
>
> ||
> dict at:inputValue ifAbsent:["default action"].

There is even a pattern for this (general, not Smalltalk-specific), I just read about it some month ago, don't know where (maybe at martinfowler.com).

The solution is:

  dict at: inputValue ifAbsent: [ dict at: '_default' ifAbsent: [ "real problem" ]]

> it's inconvenient that the default case is not directly associated
> with the dictionary. Any thoughts?

--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Dictionary Literals

horrido
Oooh, I like that! Thanks very much.


On Friday, 5 June 2015 13:58:59 UTC-4, Herby wrote:


Richard Eng wrote:

> Okay, I'll buy that.
>
> Btw, this discussion was started because I've been using a dictionary
> to simulate a Case Statement. However, I've discovered that the major
> shortcoming of using a dictionary is that you can't include a
> "default" case. While you can have a default case at the point of
> selection...
>
> ||
> dict at:inputValue ifAbsent:["default action"].

There is even a pattern for this (general, not Smalltalk-specific), I just read about it some month ago, don't know where (maybe at <a href="http://martinfowler.com" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fmartinfowler.com\46sa\75D\46sntz\0751\46usg\75AFQjCNHTJIx7H0xIeL0CvZUt8WjdL7767A';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fmartinfowler.com\46sa\75D\46sntz\0751\46usg\75AFQjCNHTJIx7H0xIeL0CvZUt8WjdL7767A';return true;">martinfowler.com).

The solution is:

  dict at: inputValue ifAbsent: [ dict at: '_default' ifAbsent: [ "real problem" ]]

> it's inconvenient that the default case is not directly associated
> with the dictionary. Any thoughts?

--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.